UNPKG

4.6 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 type { 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 errorMessage?: string
28) => void;
29
30export type HealthListener = (healthy: boolean) => void;
31
32/**
33 * This is an interface for load balancing policies to use to interact with
34 * subchannels. This allows load balancing policies to wrap and unwrap
35 * subchannels.
36 *
37 * Any load balancing policy that wraps subchannels must unwrap the subchannel
38 * in the picker, so that other load balancing policies consistently have
39 * access to their own wrapper objects.
40 */
41export 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 * If this is a wrapper, return the wrapped subchannel, otherwise return this
56 */
57 getRealSubchannel(): Subchannel;
58 /**
59 * Returns true if this and other both proxy the same underlying subchannel.
60 * Can be used instead of directly accessing getRealSubchannel to allow mocks
61 * to avoid implementing getRealSubchannel
62 */
63 realSubchannelEquals(other: SubchannelInterface): boolean;
64}
65
66export 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 /* A change to the child health state only affects this wrapper's overall
72 * health state if this wrapper is reporting healthy. */
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 /* A change to this wrapper's health state only affects the overall
125 * reported health state if the child is healthy. */
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}