resolver.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. import { MethodConfig, ServiceConfig } from './service-config';
  18. import { StatusObject } from './call-stream';
  19. import { SubchannelAddress } from './subchannel-address';
  20. import { GrpcUri, uriToString } from './uri-parser';
  21. import { ChannelOptions } from './channel-options';
  22. import { Metadata } from './metadata';
  23. import { Status } from './constants';
  24. import { Filter, FilterFactory } from './filter';
  25. export interface CallConfig {
  26. methodConfig: MethodConfig;
  27. onCommitted?: () => void;
  28. pickInformation: { [key: string]: string };
  29. status: Status;
  30. dynamicFilterFactories: FilterFactory<Filter>[];
  31. }
  32. /**
  33. * Selects a configuration for a method given the name and metadata. Defined in
  34. * https://github.com/grpc/proposal/blob/master/A31-xds-timeout-support-and-config-selector.md#new-functionality-in-grpc
  35. */
  36. export interface ConfigSelector {
  37. (methodName: string, metadata: Metadata): CallConfig;
  38. }
  39. /**
  40. * A listener object passed to the resolver's constructor that provides name
  41. * resolution updates back to the resolver's owner.
  42. */
  43. export interface ResolverListener {
  44. /**
  45. * Called whenever the resolver has new name resolution results to report
  46. * @param addressList The new list of backend addresses
  47. * @param serviceConfig The new service configuration corresponding to the
  48. * `addressList`. Will be `null` if no service configuration was
  49. * retrieved or if the service configuration was invalid
  50. * @param serviceConfigError If non-`null`, indicates that the retrieved
  51. * service configuration was invalid
  52. */
  53. onSuccessfulResolution(
  54. addressList: SubchannelAddress[],
  55. serviceConfig: ServiceConfig | null,
  56. serviceConfigError: StatusObject | null,
  57. configSelector: ConfigSelector | null,
  58. attributes: { [key: string]: unknown }
  59. ): void;
  60. /**
  61. * Called whenever a name resolution attempt fails.
  62. * @param error Describes how resolution failed
  63. */
  64. onError(error: StatusObject): void;
  65. }
  66. /**
  67. * A resolver class that handles one or more of the name syntax schemes defined
  68. * in the [gRPC Name Resolution document](https://github.com/grpc/grpc/blob/master/doc/naming.md)
  69. */
  70. export interface Resolver {
  71. /**
  72. * Indicates that the caller wants new name resolution data. Calling this
  73. * function may eventually result in calling one of the `ResolverListener`
  74. * functions, but that is not guaranteed. Those functions will never be
  75. * called synchronously with the constructor or updateResolution.
  76. */
  77. updateResolution(): void;
  78. /**
  79. * Destroy the resolver. Should be called when the owning channel shuts down.
  80. */
  81. destroy(): void;
  82. }
  83. export interface ResolverConstructor {
  84. new (
  85. target: GrpcUri,
  86. listener: ResolverListener,
  87. channelOptions: ChannelOptions
  88. ): Resolver;
  89. /**
  90. * Get the default authority for a target. This loosely corresponds to that
  91. * target's hostname. Throws an error if this resolver class cannot parse the
  92. * `target`.
  93. * @param target
  94. */
  95. getDefaultAuthority(target: GrpcUri): string;
  96. }
  97. const registeredResolvers: { [scheme: string]: ResolverConstructor } = {};
  98. let defaultScheme: string | null = null;
  99. /**
  100. * Register a resolver class to handle target names prefixed with the `prefix`
  101. * string. This prefix should correspond to a URI scheme name listed in the
  102. * [gRPC Name Resolution document](https://github.com/grpc/grpc/blob/master/doc/naming.md)
  103. * @param prefix
  104. * @param resolverClass
  105. */
  106. export function registerResolver(
  107. scheme: string,
  108. resolverClass: ResolverConstructor
  109. ) {
  110. registeredResolvers[scheme] = resolverClass;
  111. }
  112. /**
  113. * Register a default resolver to handle target names that do not start with
  114. * any registered prefix.
  115. * @param resolverClass
  116. */
  117. export function registerDefaultScheme(scheme: string) {
  118. defaultScheme = scheme;
  119. }
  120. /**
  121. * Create a name resolver for the specified target, if possible. Throws an
  122. * error if no such name resolver can be created.
  123. * @param target
  124. * @param listener
  125. */
  126. export function createResolver(
  127. target: GrpcUri,
  128. listener: ResolverListener,
  129. options: ChannelOptions
  130. ): Resolver {
  131. if (target.scheme !== undefined && target.scheme in registeredResolvers) {
  132. return new registeredResolvers[target.scheme](target, listener, options);
  133. } else {
  134. throw new Error(
  135. `No resolver could be created for target ${uriToString(target)}`
  136. );
  137. }
  138. }
  139. /**
  140. * Get the default authority for the specified target, if possible. Throws an
  141. * error if no registered name resolver can parse that target string.
  142. * @param target
  143. */
  144. export function getDefaultAuthority(target: GrpcUri): string {
  145. if (target.scheme !== undefined && target.scheme in registeredResolvers) {
  146. return registeredResolvers[target.scheme].getDefaultAuthority(target);
  147. } else {
  148. throw new Error(`Invalid target ${uriToString(target)}`);
  149. }
  150. }
  151. export function mapUriDefaultScheme(target: GrpcUri): GrpcUri | null {
  152. if (target.scheme === undefined || !(target.scheme in registeredResolvers)) {
  153. if (defaultScheme !== null) {
  154. return {
  155. scheme: defaultScheme,
  156. authority: undefined,
  157. path: uriToString(target),
  158. };
  159. } else {
  160. return null;
  161. }
  162. }
  163. return target;
  164. }