1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import { ChannelOptions, channelOptionsEqual } from './channel-options';
|
19 | import { Subchannel } from './subchannel';
|
20 | import {
|
21 | SubchannelAddress,
|
22 | subchannelAddressEqual,
|
23 | } from './subchannel-address';
|
24 | import { ChannelCredentials } from './channel-credentials';
|
25 | import { GrpcUri, uriToString } from './uri-parser';
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const REF_CHECK_INTERVAL = 10_000;
|
33 |
|
34 | export class SubchannelPool {
|
35 | private pool: {
|
36 | [channelTarget: string]: Array<{
|
37 | subchannelAddress: SubchannelAddress;
|
38 | channelArguments: ChannelOptions;
|
39 | channelCredentials: ChannelCredentials;
|
40 | subchannel: Subchannel;
|
41 | }>;
|
42 | } = Object.create(null);
|
43 |
|
44 | |
45 |
|
46 |
|
47 | private cleanupTimer: NodeJS.Timer | null = null;
|
48 |
|
49 | |
50 |
|
51 |
|
52 |
|
53 | constructor() {}
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 | unrefUnusedSubchannels(): void {
|
60 | let allSubchannelsUnrefed = true;
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 | for (const channelTarget in this.pool) {
|
67 | const subchannelObjArray = this.pool[channelTarget];
|
68 |
|
69 | const refedSubchannels = subchannelObjArray.filter(
|
70 | (value) => !value.subchannel.unrefIfOneRef()
|
71 | );
|
72 |
|
73 | if (refedSubchannels.length > 0) {
|
74 | allSubchannelsUnrefed = false;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 | this.pool[channelTarget] = refedSubchannels;
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 | if (allSubchannelsUnrefed && this.cleanupTimer !== null) {
|
87 | clearInterval(this.cleanupTimer);
|
88 | this.cleanupTimer = null;
|
89 | }
|
90 | }
|
91 |
|
92 | |
93 |
|
94 |
|
95 | ensureCleanupTask(): void {
|
96 | if (this.cleanupTimer === null) {
|
97 | this.cleanupTimer = setInterval(() => {
|
98 | this.unrefUnusedSubchannels();
|
99 | }, REF_CHECK_INTERVAL);
|
100 |
|
101 |
|
102 |
|
103 | this.cleanupTimer.unref?.();
|
104 | }
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | getOrCreateSubchannel(
|
116 | channelTargetUri: GrpcUri,
|
117 | subchannelTarget: SubchannelAddress,
|
118 | channelArguments: ChannelOptions,
|
119 | channelCredentials: ChannelCredentials
|
120 | ): Subchannel {
|
121 | this.ensureCleanupTask();
|
122 | const channelTarget = uriToString(channelTargetUri);
|
123 | if (channelTarget in this.pool) {
|
124 | const subchannelObjArray = this.pool[channelTarget];
|
125 | for (const subchannelObj of subchannelObjArray) {
|
126 | if (
|
127 | subchannelAddressEqual(
|
128 | subchannelTarget,
|
129 | subchannelObj.subchannelAddress
|
130 | ) &&
|
131 | channelOptionsEqual(
|
132 | channelArguments,
|
133 | subchannelObj.channelArguments
|
134 | ) &&
|
135 | channelCredentials._equals(subchannelObj.channelCredentials)
|
136 | ) {
|
137 | return subchannelObj.subchannel;
|
138 | }
|
139 | }
|
140 | }
|
141 |
|
142 | const subchannel = new Subchannel(
|
143 | channelTargetUri,
|
144 | subchannelTarget,
|
145 | channelArguments,
|
146 | channelCredentials
|
147 | );
|
148 | if (!(channelTarget in this.pool)) {
|
149 | this.pool[channelTarget] = [];
|
150 | }
|
151 | this.pool[channelTarget].push({
|
152 | subchannelAddress: subchannelTarget,
|
153 | channelArguments,
|
154 | channelCredentials,
|
155 | subchannel,
|
156 | });
|
157 | subchannel.ref();
|
158 | return subchannel;
|
159 | }
|
160 | }
|
161 |
|
162 | const globalSubchannelPool = new SubchannelPool();
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | export function getSubchannelPool(global: boolean): SubchannelPool {
|
169 | if (global) {
|
170 | return globalSubchannelPool;
|
171 | } else {
|
172 | return new SubchannelPool();
|
173 | }
|
174 | }
|