egos-transfer 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -11,6 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ConnectionManager = void 0;
|
|
13
13
|
const HEALTH_CHECK_INTERVAL = 15000;
|
|
14
|
+
/** 连接最大存活时间:超过 15 分钟的连接在 get 时自动丢弃,强制重建 */
|
|
15
|
+
const MAX_CONNECTION_AGE = 15 * 60 * 1000;
|
|
14
16
|
/**
|
|
15
17
|
* DataConnection 连接池管理器
|
|
16
18
|
* - 管理 DataConnection 的生命周期(注册/移除/有效性检查)
|
|
@@ -20,6 +22,8 @@ const HEALTH_CHECK_INTERVAL = 15000;
|
|
|
20
22
|
class ConnectionManager {
|
|
21
23
|
constructor(deps) {
|
|
22
24
|
this.pool = new Map();
|
|
25
|
+
/** peerId → 连接入池时间戳 */
|
|
26
|
+
this.createdAt = new Map();
|
|
23
27
|
this.healthCheckTimer = null;
|
|
24
28
|
this.pingRequests = new Map();
|
|
25
29
|
this.destroyed = false;
|
|
@@ -61,6 +65,7 @@ class ConnectionManager {
|
|
|
61
65
|
*/
|
|
62
66
|
setup(conn, onData) {
|
|
63
67
|
this.pool.set(conn.peer, conn);
|
|
68
|
+
this.createdAt.set(conn.peer, Date.now());
|
|
64
69
|
conn.on('data', onData);
|
|
65
70
|
conn.on('close', () => this.handleConnClosed(conn));
|
|
66
71
|
conn.on('error', () => this.handleConnClosed(conn));
|
|
@@ -87,6 +92,7 @@ class ConnectionManager {
|
|
|
87
92
|
if (connectionId && conn.connectionId !== connectionId)
|
|
88
93
|
return;
|
|
89
94
|
this.pool.delete(peerId);
|
|
95
|
+
this.createdAt.delete(peerId);
|
|
90
96
|
// 先 close 触发 close 事件(handleConnClosed 会结算 in-flight 请求),
|
|
91
97
|
// 再清理监听器与 PeerJS 内部引用,避免僵尸连接堆积导致无法建立新连接
|
|
92
98
|
conn.close();
|
|
@@ -204,6 +210,7 @@ class ConnectionManager {
|
|
|
204
210
|
this.remove(peerId);
|
|
205
211
|
}
|
|
206
212
|
this.pool.clear();
|
|
213
|
+
this.createdAt.clear();
|
|
207
214
|
this.pingRequests.clear();
|
|
208
215
|
}
|
|
209
216
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { DataConnection, Peer } from 'peerjs';
|
|
2
|
-
import { PeerAction, PeerRoutes, PeerScope } from './entity';
|
|
3
|
-
|
|
4
|
-
import { PING_TIMEOUT } from './constant';
|
|
5
2
|
|
|
6
3
|
type ResponseCb = (args: any) => void;
|
|
7
4
|
|
|
8
5
|
const HEALTH_CHECK_INTERVAL = 15000;
|
|
6
|
+
/** 连接最大存活时间:超过 15 分钟的连接在 get 时自动丢弃,强制重建 */
|
|
7
|
+
const MAX_CONNECTION_AGE = 15 * 60 * 1000;
|
|
9
8
|
|
|
10
9
|
export interface ConnectionManagerDeps {
|
|
11
10
|
/** 获取当前 Peer 实例(PeerTransfer 可能异步重建) */
|
|
@@ -74,7 +73,11 @@ export class ConnectionManager {
|
|
|
74
73
|
* @param conn - 已打开的 DataConnection
|
|
75
74
|
* @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
|
|
76
75
|
*/
|
|
77
|
-
setup(
|
|
76
|
+
setup(
|
|
77
|
+
conn: DataConnection & { timestamp?: number },
|
|
78
|
+
onData: (message: any) => void,
|
|
79
|
+
): DataConnection {
|
|
80
|
+
conn.timestamp = Date.now();
|
|
78
81
|
this.pool.set(conn.peer, conn);
|
|
79
82
|
conn.on('data', onData);
|
|
80
83
|
conn.on('close', () => this.handleConnClosed(conn));
|
|
@@ -120,9 +123,14 @@ export class ConnectionManager {
|
|
|
120
123
|
}
|
|
121
124
|
}
|
|
122
125
|
|
|
123
|
-
isValid(conn?: DataConnection): boolean {
|
|
124
|
-
if (!conn?.open)
|
|
125
|
-
|
|
126
|
+
isValid(conn?: DataConnection & { timestamp?: number }): boolean {
|
|
127
|
+
if (!conn?.open || !conn?.peerConnection) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
// 检查连接是否过期
|
|
131
|
+
if (conn.timestamp && Date.now() - conn.timestamp > MAX_CONNECTION_AGE) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
126
134
|
return ['connected', 'completed'].includes(conn.peerConnection.connectionState);
|
|
127
135
|
}
|
|
128
136
|
|