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