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 |
|
18 | import { ChannelCredentials } from './channel-credentials';
|
19 | import { ChannelOptions } from './channel-options';
|
20 | import { ServerSurfaceCall } from './server-call';
|
21 |
|
22 | import { ConnectivityState } from './connectivity-state';
|
23 | import { ChannelRef } from './channelz';
|
24 | import { Call } from './call-interface';
|
25 | import { InternalChannel } from './internal-channel';
|
26 | import { Deadline } from './deadline';
|
27 |
|
28 | /**
|
29 | * An interface that represents a communication channel to a server specified
|
30 | * by a given address.
|
31 | */
|
32 | export 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 |
|
93 | export class ChannelImplementation implements Channel {
|
94 |
|
95 | private internalChannel: InternalChannel;
|
96 |
|
97 | constructor(
|
98 | target: string,
|
99 | credentials: ChannelCredentials,
|
100 | options: ChannelOptions
|
101 | ) {
|
102 | if (typeof target !== 'string') {
|
103 | throw new TypeError('Channel target must be a string');
|
104 | }
|
105 | if (!(credentials instanceof ChannelCredentials)) {
|
106 | throw new TypeError(
|
107 | 'Channel credentials must be a ChannelCredentials object'
|
108 | );
|
109 | }
|
110 | if (options) {
|
111 | if (typeof options !== 'object') {
|
112 | throw new TypeError('Channel options must be an object');
|
113 | }
|
114 | }
|
115 |
|
116 | this.internalChannel = new InternalChannel(target, credentials, options);
|
117 | }
|
118 |
|
119 | close() {
|
120 | this.internalChannel.close();
|
121 | }
|
122 |
|
123 | getTarget() {
|
124 | return this.internalChannel.getTarget();
|
125 | }
|
126 |
|
127 | getConnectivityState(tryToConnect: boolean) {
|
128 | return this.internalChannel.getConnectivityState(tryToConnect);
|
129 | }
|
130 |
|
131 | watchConnectivityState(
|
132 | currentState: ConnectivityState,
|
133 | deadline: Date | number,
|
134 | callback: (error?: Error) => void
|
135 | ): void {
|
136 | this.internalChannel.watchConnectivityState(currentState, deadline, callback);
|
137 | }
|
138 |
|
139 | /**
|
140 | * Get the channelz reference object for this channel. The returned value is
|
141 | * garbage if channelz is disabled for this channel.
|
142 | * @returns
|
143 | */
|
144 | getChannelzRef() {
|
145 | return this.internalChannel.getChannelzRef();
|
146 | }
|
147 |
|
148 | createCall(
|
149 | method: string,
|
150 | deadline: Deadline,
|
151 | host: string | null | undefined,
|
152 | parentCall: ServerSurfaceCall | null,
|
153 | propagateFlags: number | null | undefined
|
154 | ): Call {
|
155 | if (typeof method !== 'string') {
|
156 | throw new TypeError('Channel#createCall: method must be a string');
|
157 | }
|
158 | if (!(typeof deadline === 'number' || deadline instanceof Date)) {
|
159 | throw new TypeError(
|
160 | 'Channel#createCall: deadline must be a number or Date'
|
161 | );
|
162 | }
|
163 | return this.internalChannel.createCall(method, deadline, host, parentCall, propagateFlags);
|
164 | }
|
165 | }
|