UNPKG

6.67 kBTypeScriptView Raw
1import { ChannelCredentials } from './channel-credentials';
2import { Metadata } from './metadata';
3import { Http2CallStream } from './call-stream';
4import { ChannelOptions } from './channel-options';
5import { ConnectivityState } from './channel';
6import { GrpcUri } from './uri-parser';
7import { FilterFactory, Filter } from './filter';
8export declare type ConnectivityStateListener = (subchannel: Subchannel, previousState: ConnectivityState, newState: ConnectivityState) => void;
9export interface TcpSubchannelAddress {
10 port: number;
11 host: string;
12}
13export interface IpcSubchannelAddress {
14 path: string;
15}
16/**
17 * This represents a single backend address to connect to. This interface is a
18 * subset of net.SocketConnectOpts, i.e. the options described at
19 * https://nodejs.org/api/net.html#net_socket_connect_options_connectlistener.
20 * Those are in turn a subset of the options that can be passed to http2.connect.
21 */
22export declare type SubchannelAddress = TcpSubchannelAddress | IpcSubchannelAddress;
23export declare function isTcpSubchannelAddress(address: SubchannelAddress): address is TcpSubchannelAddress;
24export declare function subchannelAddressEqual(address1: SubchannelAddress, address2: SubchannelAddress): boolean;
25export declare function subchannelAddressToString(address: SubchannelAddress): string;
26export declare class Subchannel {
27 private channelTarget;
28 private subchannelAddress;
29 private options;
30 private credentials;
31 /**
32 * The subchannel's current connectivity state. Invariant: `session` === `null`
33 * if and only if `connectivityState` is IDLE or TRANSIENT_FAILURE.
34 */
35 private connectivityState;
36 /**
37 * The underlying http2 session used to make requests.
38 */
39 private session;
40 /**
41 * Indicates that the subchannel should transition from TRANSIENT_FAILURE to
42 * CONNECTING instead of IDLE when the backoff timeout ends.
43 */
44 private continueConnecting;
45 /**
46 * A list of listener functions that will be called whenever the connectivity
47 * state changes. Will be modified by `addConnectivityStateListener` and
48 * `removeConnectivityStateListener`
49 */
50 private stateListeners;
51 /**
52 * A list of listener functions that will be called when the underlying
53 * socket disconnects. Used for ending active calls with an UNAVAILABLE
54 * status.
55 */
56 private disconnectListeners;
57 private backoffTimeout;
58 /**
59 * The complete user agent string constructed using channel args.
60 */
61 private userAgent;
62 /**
63 * The amount of time in between sending pings
64 */
65 private keepaliveTimeMs;
66 /**
67 * The amount of time to wait for an acknowledgement after sending a ping
68 */
69 private keepaliveTimeoutMs;
70 /**
71 * Timer reference for timeout that indicates when to send the next ping
72 */
73 private keepaliveIntervalId;
74 /**
75 * Timer reference tracking when the most recent ping will be considered lost
76 */
77 private keepaliveTimeoutId;
78 /**
79 * Indicates whether keepalive pings should be sent without any active calls
80 */
81 private keepaliveWithoutCalls;
82 /**
83 * Tracks calls with references to this subchannel
84 */
85 private callRefcount;
86 /**
87 * Tracks channels and subchannel pools with references to this subchannel
88 */
89 private refcount;
90 /**
91 * A string representation of the subchannel address, for logging/tracing
92 */
93 private subchannelAddressString;
94 /**
95 * A class representing a connection to a single backend.
96 * @param channelTarget The target string for the channel as a whole
97 * @param subchannelAddress The address for the backend that this subchannel
98 * will connect to
99 * @param options The channel options, plus any specific subchannel options
100 * for this subchannel
101 * @param credentials The channel credentials used to establish this
102 * connection
103 */
104 constructor(channelTarget: GrpcUri, subchannelAddress: SubchannelAddress, options: ChannelOptions, credentials: ChannelCredentials);
105 private handleBackoffTimer;
106 /**
107 * Start a backoff timer with the current nextBackoff timeout
108 */
109 private startBackoff;
110 private stopBackoff;
111 private sendPing;
112 private startKeepalivePings;
113 private stopKeepalivePings;
114 private createSession;
115 private startConnectingInternal;
116 /**
117 * Initiate a state transition from any element of oldStates to the new
118 * state. If the current connectivityState is not in oldStates, do nothing.
119 * @param oldStates The set of states to transition from
120 * @param newState The state to transition to
121 * @returns True if the state changed, false otherwise
122 */
123 private transitionToState;
124 /**
125 * Check if the subchannel associated with zero calls and with zero channels.
126 * If so, shut it down.
127 */
128 private checkBothRefcounts;
129 callRef(): void;
130 callUnref(): void;
131 ref(): void;
132 unref(): void;
133 unrefIfOneRef(): boolean;
134 /**
135 * Start a stream on the current session with the given `metadata` as headers
136 * and then attach it to the `callStream`. Must only be called if the
137 * subchannel's current connectivity state is READY.
138 * @param metadata
139 * @param callStream
140 */
141 startCallStream(metadata: Metadata, callStream: Http2CallStream, extraFilterFactory?: FilterFactory<Filter>): void;
142 /**
143 * If the subchannel is currently IDLE, start connecting and switch to the
144 * CONNECTING state. If the subchannel is current in TRANSIENT_FAILURE,
145 * the next time it would transition to IDLE, start connecting again instead.
146 * Otherwise, do nothing.
147 */
148 startConnecting(): void;
149 /**
150 * Get the subchannel's current connectivity state.
151 */
152 getConnectivityState(): ConnectivityState;
153 /**
154 * Add a listener function to be called whenever the subchannel's
155 * connectivity state changes.
156 * @param listener
157 */
158 addConnectivityStateListener(listener: ConnectivityStateListener): void;
159 /**
160 * Remove a listener previously added with `addConnectivityStateListener`
161 * @param listener A reference to a function previously passed to
162 * `addConnectivityStateListener`
163 */
164 removeConnectivityStateListener(listener: ConnectivityStateListener): void;
165 addDisconnectListener(listener: () => void): void;
166 removeDisconnectListener(listener: () => void): void;
167 /**
168 * Reset the backoff timeout, and immediately start connecting if in backoff.
169 */
170 resetBackoff(): void;
171 getAddress(): string;
172}