UNPKG

5.61 kBPlain TextView Raw
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
18import { ChannelCredentials } from './channel-credentials';
19import { ChannelOptions } from './channel-options';
20import { ServerSurfaceCall } from './server-call';
21
22import { ConnectivityState } from './connectivity-state';
23import type { ChannelRef } from './channelz';
24import { Call } from './call-interface';
25import { InternalChannel } from './internal-channel';
26import { Deadline } from './deadline';
27
28/**
29 * An interface that represents a communication channel to a server specified
30 * by a given address.
31 */
32export interface Channel {
33 /**
34 * Close the channel. This has the same functionality as the existing
35 * grpc.Client.prototype.close
36 */
37 close(): void;
38 /**
39 * Return the target that this channel connects to
40 */
41 getTarget(): string;
42 /**
43 * Get the channel's current connectivity state. This method is here mainly
44 * because it is in the existing internal Channel class, and there isn't
45 * another good place to put it.
46 * @param tryToConnect If true, the channel will start connecting if it is
47 * idle. Otherwise, idle channels will only start connecting when a
48 * call starts.
49 */
50 getConnectivityState(tryToConnect: boolean): ConnectivityState;
51 /**
52 * Watch for connectivity state changes. This is also here mainly because
53 * it is in the existing external Channel class.
54 * @param currentState The state to watch for transitions from. This should
55 * always be populated by calling getConnectivityState immediately
56 * before.
57 * @param deadline A deadline for waiting for a state change
58 * @param callback Called with no error when a state change, or with an
59 * error if the deadline passes without a state change.
60 */
61 watchConnectivityState(
62 currentState: ConnectivityState,
63 deadline: Date | number,
64 callback: (error?: Error) => void
65 ): void;
66 /**
67 * Get the channelz reference object for this channel. A request to the
68 * channelz service for the id in this object will provide information
69 * about this channel.
70 */
71 getChannelzRef(): ChannelRef;
72 /**
73 * Create a call object. Call is an opaque type that is used by the Client
74 * class. This function is called by the gRPC library when starting a
75 * request. Implementers should return an instance of Call that is returned
76 * from calling createCall on an instance of the provided Channel class.
77 * @param method The full method string to request.
78 * @param deadline The call deadline
79 * @param host A host string override for making the request
80 * @param parentCall A server call to propagate some information from
81 * @param propagateFlags A bitwise combination of elements of grpc.propagate
82 * that indicates what information to propagate from parentCall.
83 */
84 createCall(
85 method: string,
86 deadline: Deadline,
87 host: string | null | undefined,
88 parentCall: ServerSurfaceCall | null,
89 propagateFlags: number | null | undefined
90 ): Call;
91}
92
93export class ChannelImplementation implements Channel {
94 private internalChannel: InternalChannel;
95
96 constructor(
97 target: string,
98 credentials: ChannelCredentials,
99 options: ChannelOptions
100 ) {
101 if (typeof target !== 'string') {
102 throw new TypeError('Channel target must be a string');
103 }
104 if (!(credentials instanceof ChannelCredentials)) {
105 throw new TypeError(
106 'Channel credentials must be a ChannelCredentials object'
107 );
108 }
109 if (options) {
110 if (typeof options !== 'object') {
111 throw new TypeError('Channel options must be an object');
112 }
113 }
114
115 this.internalChannel = new InternalChannel(target, credentials, options);
116 }
117
118 close() {
119 this.internalChannel.close();
120 }
121
122 getTarget() {
123 return this.internalChannel.getTarget();
124 }
125
126 getConnectivityState(tryToConnect: boolean) {
127 return this.internalChannel.getConnectivityState(tryToConnect);
128 }
129
130 watchConnectivityState(
131 currentState: ConnectivityState,
132 deadline: Date | number,
133 callback: (error?: Error) => void
134 ): void {
135 this.internalChannel.watchConnectivityState(
136 currentState,
137 deadline,
138 callback
139 );
140 }
141
142 /**
143 * Get the channelz reference object for this channel. The returned value is
144 * garbage if channelz is disabled for this channel.
145 * @returns
146 */
147 getChannelzRef() {
148 return this.internalChannel.getChannelzRef();
149 }
150
151 createCall(
152 method: string,
153 deadline: Deadline,
154 host: string | null | undefined,
155 parentCall: ServerSurfaceCall | null,
156 propagateFlags: number | null | undefined
157 ): Call {
158 if (typeof method !== 'string') {
159 throw new TypeError('Channel#createCall: method must be a string');
160 }
161 if (!(typeof deadline === 'number' || deadline instanceof Date)) {
162 throw new TypeError(
163 'Channel#createCall: deadline must be a number or Date'
164 );
165 }
166 return this.internalChannel.createCall(
167 method,
168 deadline,
169 host,
170 parentCall,
171 propagateFlags
172 );
173 }
174}