UNPKG

2.64 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) => void;
27
28/**
29 * This is an interface for load balancing policies to use to interact with
30 * subchannels. This allows load balancing policies to wrap and unwrap
31 * subchannels.
32 *
33 * Any load balancing policy that wraps subchannels must unwrap the subchannel
34 * in the picker, so that other load balancing policies consistently have
35 * access to their own wrapper objects.
36 */
37export interface SubchannelInterface {
38 getConnectivityState(): ConnectivityState;
39 addConnectivityStateListener(listener: ConnectivityStateListener): void;
40 removeConnectivityStateListener(listener: ConnectivityStateListener): void;
41 startConnecting(): void;
42 getAddress(): string;
43 ref(): void;
44 unref(): void;
45 getChannelzRef(): SubchannelRef;
46 /**
47 * If this is a wrapper, return the wrapped subchannel, otherwise return this
48 */
49 getRealSubchannel(): Subchannel;
50}
51
52export abstract class BaseSubchannelWrapper implements SubchannelInterface {
53 constructor(protected child: SubchannelInterface) {}
54
55 getConnectivityState(): ConnectivityState {
56 return this.child.getConnectivityState();
57 }
58 addConnectivityStateListener(listener: ConnectivityStateListener): void {
59 this.child.addConnectivityStateListener(listener);
60 }
61 removeConnectivityStateListener(listener: ConnectivityStateListener): void {
62 this.child.removeConnectivityStateListener(listener);
63 }
64 startConnecting(): void {
65 this.child.startConnecting();
66 }
67 getAddress(): string {
68 return this.child.getAddress();
69 }
70 ref(): void {
71 this.child.ref();
72 }
73 unref(): void {
74 this.child.unref();
75 }
76 getChannelzRef(): SubchannelRef {
77 return this.child.getChannelzRef();
78 }
79 getRealSubchannel(): Subchannel {
80 return this.child.getRealSubchannel();
81 }
82}
\No newline at end of file