-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwsConnectorPool.ts
More file actions
171 lines (159 loc) · 6.03 KB
/
wsConnectorPool.ts
File metadata and controls
171 lines (159 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Mutex } from "async-mutex";
import { WebSocketConnector } from "./wsConnector";
import { ErrorCode, TDWebSocketClientError } from "../common/wsError";
import logger from "../common/log";
import { w3cwebsocket } from "websocket";
const mutex = new Mutex();
export class WebSocketConnectionPool {
private static _instance?: WebSocketConnectionPool;
private pool: Map<string, WebSocketConnector[]> = new Map();
private readonly _maxConnections: number;
private static sharedBuffer: SharedArrayBuffer;
private static sharedArray: Int32Array;
private constructor(maxConnections: number = -1) {
this._maxConnections = maxConnections;
WebSocketConnectionPool.sharedBuffer = new SharedArrayBuffer(4);
WebSocketConnectionPool.sharedArray = new Int32Array(
WebSocketConnectionPool.sharedBuffer
);
Atomics.store(WebSocketConnectionPool.sharedArray, 0, 0);
}
public static instance(
maxConnections: number = -1
): WebSocketConnectionPool {
if (!WebSocketConnectionPool._instance) {
WebSocketConnectionPool._instance = new WebSocketConnectionPool(
maxConnections
);
}
return WebSocketConnectionPool._instance;
}
async getConnection(
url: URL,
timeout: number | undefined | null
): Promise<WebSocketConnector> {
let connectAddr = url.origin.concat(url.pathname).concat(url.search);
let connector: WebSocketConnector | undefined;
const unlock = await mutex.acquire();
try {
if (this.pool.has(connectAddr)) {
const connectors = this.pool.get(connectAddr);
while (connectors && connectors.length > 0) {
const candidate = connectors.pop();
if (!candidate) {
continue;
}
if (
candidate &&
candidate.readyState() === w3cwebsocket.OPEN
) {
connector = candidate;
break;
} else if (candidate) {
Atomics.add(WebSocketConnectionPool.sharedArray, 0, -1);
candidate.close();
logger.error(
`getConnection, current connection status fail, url: ${connectAddr}`
);
}
}
}
if (connector) {
logger.debug(
"get connection success:" +
Atomics.load(WebSocketConnectionPool.sharedArray, 0)
);
return connector;
}
if (
this._maxConnections != -1 &&
Atomics.load(WebSocketConnectionPool.sharedArray, 0) >
this._maxConnections
) {
throw new TDWebSocketClientError(
ErrorCode.ERR_WEBSOCKET_CONNECTION_ARRIVED_LIMIT,
"websocket connect arrived limited:" +
Atomics.load(WebSocketConnectionPool.sharedArray, 0)
);
}
Atomics.add(WebSocketConnectionPool.sharedArray, 0, 1);
logger.info(
"getConnection, new connection count:" +
Atomics.load(WebSocketConnectionPool.sharedArray, 0) +
", connectAddr:" +
connectAddr
);
return new WebSocketConnector(url, timeout);
} finally {
unlock();
}
}
async releaseConnection(connector: WebSocketConnector): Promise<void> {
if (connector) {
const unlock = await mutex.acquire();
try {
if (connector.readyState() === w3cwebsocket.OPEN) {
let url = connector.getWsURL();
let connectAddr = url.origin
.concat(url.pathname)
.concat(url.search);
let connectors = this.pool.get(connectAddr);
if (!connectors) {
connectors = new Array();
connectors.push(connector);
this.pool.set(connectAddr, connectors);
} else {
connectors.push(connector);
}
logger.info(
"releaseConnection, current connection count:" +
connectors.length
);
} else {
Atomics.add(WebSocketConnectionPool.sharedArray, 0, -1);
connector.close();
logger.info(
"releaseConnection, current connection status fail:" +
Atomics.load(WebSocketConnectionPool.sharedArray, 0)
);
}
} finally {
unlock();
}
}
}
destroyed() {
let num = 0;
if (this.pool) {
for (let values of this.pool.values()) {
for (let i in values) {
num++;
values[i].close();
}
}
}
logger.info(
"destroyed connect:" +
Atomics.load(WebSocketConnectionPool.sharedArray, 0) +
" current count:" +
num
);
Atomics.store(WebSocketConnectionPool.sharedArray, 0, 0);
this.pool = new Map();
}
}
process.on("exit", (code) => {
logger.info("begin destroy connect");
WebSocketConnectionPool.instance().destroyed();
process.exit();
});
process.on("SIGINT", () => {
logger.info("Received SIGINT. Press Control-D to exit, begin destroy connect...");
WebSocketConnectionPool.instance().destroyed();
process.exit();
});
process.on("SIGTERM", () => {
logger.info("Received SIGINT. Press Control-D to exit, begin destroy connect");
WebSocketConnectionPool.instance().destroyed();
process.exit();
});