UNPKG

2.83 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
54export abstract class BaseSubchannelWrapper implements SubchannelInterface {
55 constructor(protected child: SubchannelInterface) {}
56
57 getConnectivityState(): ConnectivityState {
58 return this.child.getConnectivityState();
59 }
60 addConnectivityStateListener(listener: ConnectivityStateListener): void {
61 this.child.addConnectivityStateListener(listener);
62 }
63 removeConnectivityStateListener(listener: ConnectivityStateListener): void {
64 this.child.removeConnectivityStateListener(listener);
65 }
66 startConnecting(): void {
67 this.child.startConnecting();
68 }
69 getAddress(): string {
70 return this.child.getAddress();
71 }
72 throttleKeepalive(newKeepaliveTime: number): void {
73 this.child.throttleKeepalive(newKeepaliveTime);
74 }
75 ref(): void {
76 this.child.ref();
77 }
78 unref(): void {
79 this.child.unref();
80 }
81 getChannelzRef(): SubchannelRef {
82 return this.child.getChannelzRef();
83 }
84 getRealSubchannel(): Subchannel {
85 return this.child.getRealSubchannel();
86 }
87}
\No newline at end of file