1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import type { SubchannelRef } from './channelz';
|
19 | import { ConnectivityState } from './connectivity-state';
|
20 | import { Subchannel } from './subchannel';
|
21 |
|
22 | export type ConnectivityStateListener = (
|
23 | subchannel: SubchannelInterface,
|
24 | previousState: ConnectivityState,
|
25 | newState: ConnectivityState,
|
26 | keepaliveTime: number,
|
27 | errorMessage?: string
|
28 | ) => void;
|
29 |
|
30 | export type HealthListener = (healthy: boolean) => void;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export interface SubchannelInterface {
|
42 | getConnectivityState(): ConnectivityState;
|
43 | addConnectivityStateListener(listener: ConnectivityStateListener): void;
|
44 | removeConnectivityStateListener(listener: ConnectivityStateListener): void;
|
45 | startConnecting(): void;
|
46 | getAddress(): string;
|
47 | throttleKeepalive(newKeepaliveTime: number): void;
|
48 | ref(): void;
|
49 | unref(): void;
|
50 | getChannelzRef(): SubchannelRef;
|
51 | isHealthy(): boolean;
|
52 | addHealthStateWatcher(listener: HealthListener): void;
|
53 | removeHealthStateWatcher(listener: HealthListener): void;
|
54 | |
55 |
|
56 |
|
57 | getRealSubchannel(): Subchannel;
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | realSubchannelEquals(other: SubchannelInterface): boolean;
|
64 | }
|
65 |
|
66 | export abstract class BaseSubchannelWrapper implements SubchannelInterface {
|
67 | private healthy = true;
|
68 | private healthListeners: Set<HealthListener> = new Set();
|
69 | constructor(protected child: SubchannelInterface) {
|
70 | child.addHealthStateWatcher(childHealthy => {
|
71 | |
72 |
|
73 | if (this.healthy) {
|
74 | this.updateHealthListeners();
|
75 | }
|
76 | });
|
77 | }
|
78 |
|
79 | private updateHealthListeners(): void {
|
80 | for (const listener of this.healthListeners) {
|
81 | listener(this.isHealthy());
|
82 | }
|
83 | }
|
84 |
|
85 | getConnectivityState(): ConnectivityState {
|
86 | return this.child.getConnectivityState();
|
87 | }
|
88 | addConnectivityStateListener(listener: ConnectivityStateListener): void {
|
89 | this.child.addConnectivityStateListener(listener);
|
90 | }
|
91 | removeConnectivityStateListener(listener: ConnectivityStateListener): void {
|
92 | this.child.removeConnectivityStateListener(listener);
|
93 | }
|
94 | startConnecting(): void {
|
95 | this.child.startConnecting();
|
96 | }
|
97 | getAddress(): string {
|
98 | return this.child.getAddress();
|
99 | }
|
100 | throttleKeepalive(newKeepaliveTime: number): void {
|
101 | this.child.throttleKeepalive(newKeepaliveTime);
|
102 | }
|
103 | ref(): void {
|
104 | this.child.ref();
|
105 | }
|
106 | unref(): void {
|
107 | this.child.unref();
|
108 | }
|
109 | getChannelzRef(): SubchannelRef {
|
110 | return this.child.getChannelzRef();
|
111 | }
|
112 | isHealthy(): boolean {
|
113 | return this.healthy && this.child.isHealthy();
|
114 | }
|
115 | addHealthStateWatcher(listener: HealthListener): void {
|
116 | this.healthListeners.add(listener);
|
117 | }
|
118 | removeHealthStateWatcher(listener: HealthListener): void {
|
119 | this.healthListeners.delete(listener);
|
120 | }
|
121 | protected setHealthy(healthy: boolean): void {
|
122 | if (healthy !== this.healthy) {
|
123 | this.healthy = healthy;
|
124 | |
125 |
|
126 | if (this.child.isHealthy()) {
|
127 | this.updateHealthListeners();
|
128 | }
|
129 | }
|
130 | }
|
131 | getRealSubchannel(): Subchannel {
|
132 | return this.child.getRealSubchannel();
|
133 | }
|
134 | realSubchannelEquals(other: SubchannelInterface): boolean {
|
135 | return this.getRealSubchannel() === other.getRealSubchannel();
|
136 | }
|
137 | }
|