UNPKG

3.24 kBPlain TextView Raw
1/*
2 * Copyright 2022 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 { SubchannelRef } from './channelz';
19import { ConnectivityState } from './connectivity-state';
20import { Subchannel } from './subchannel';
21
22export type ConnectivityStateListener = (
23 subchannel: SubchannelInterface,
24 previousState: ConnectivityState,
25 newState: ConnectivityState,
26 keepaliveTime: number
27) => void;
28
29/**
30 * This is an interface for load balancing policies to use to interact with
31 * subchannels. This allows load balancing policies to wrap and unwrap
32 * subchannels.
33 *
34 * Any load balancing policy that wraps subchannels must unwrap the subchannel
35 * in the picker, so that other load balancing policies consistently have
36 * access to their own wrapper objects.
37 */
38export interface SubchannelInterface {
39 getConnectivityState(): ConnectivityState;
40 addConnectivityStateListener(listener: ConnectivityStateListener): void;
41 removeConnectivityStateListener(listener: ConnectivityStateListener): void;
42 startConnecting(): void;
43 getAddress(): string;
44 throttleKeepalive(newKeepaliveTime: number): void;
45 ref(): void;
46 unref(): void;
47 getChannelzRef(): SubchannelRef;
48 /**
49 * If this is a wrapper, return the wrapped subchannel, otherwise return this
50 */
51 getRealSubchannel(): Subchannel;
52 /**
53 * Returns true if this and other both proxy the same underlying subchannel.
54 * Can be used instead of directly accessing getRealSubchannel to allow mocks
55 * to avoid implementing getRealSubchannel
56 */
57 realSubchannelEquals(other: SubchannelInterface): boolean;
58}
59
60export abstract class BaseSubchannelWrapper implements SubchannelInterface {
61 constructor(protected child: SubchannelInterface) {}
62
63 getConnectivityState(): ConnectivityState {
64 return this.child.getConnectivityState();
65 }
66 addConnectivityStateListener(listener: ConnectivityStateListener): void {
67 this.child.addConnectivityStateListener(listener);
68 }
69 removeConnectivityStateListener(listener: ConnectivityStateListener): void {
70 this.child.removeConnectivityStateListener(listener);
71 }
72 startConnecting(): void {
73 this.child.startConnecting();
74 }
75 getAddress(): string {
76 return this.child.getAddress();
77 }
78 throttleKeepalive(newKeepaliveTime: number): void {
79 this.child.throttleKeepalive(newKeepaliveTime);
80 }
81 ref(): void {
82 this.child.ref();
83 }
84 unref(): void {
85 this.child.unref();
86 }
87 getChannelzRef(): SubchannelRef {
88 return this.child.getChannelzRef();
89 }
90 getRealSubchannel(): Subchannel {
91 return this.child.getRealSubchannel();
92 }
93 realSubchannelEquals(other: SubchannelInterface): boolean {
94 return this.getRealSubchannel() === other.getRealSubchannel();
95 }
96}