@clonegod/ttd-bsc-common 1.0.18 → 1.0.20
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.
|
@@ -23,7 +23,7 @@ export declare class PoolEventListener {
|
|
|
23
23
|
init(poolList: StandardPoolInfoType[]): Promise<void>;
|
|
24
24
|
start(): Promise<void>;
|
|
25
25
|
stop(): Promise<void>;
|
|
26
|
-
|
|
26
|
+
connect(): Promise<void>;
|
|
27
27
|
private handleConnectionIssue;
|
|
28
28
|
private startBlockListener;
|
|
29
29
|
private stopBlockListener;
|
|
@@ -61,37 +61,41 @@ class PoolEventListener {
|
|
|
61
61
|
}
|
|
62
62
|
connect() {
|
|
63
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.
|
|
64
|
+
const maxRetries = 3;
|
|
65
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
66
|
+
try {
|
|
67
|
+
(0, dist_1.log_info)(`正在连接 WebSocket: ${this.ws_endpoint} (尝试 ${i + 1}/${maxRetries})`);
|
|
68
|
+
this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(this.ws_endpoint);
|
|
69
|
+
const timeout = 10000;
|
|
70
|
+
const wsPromise = this.wsProvider.ready;
|
|
71
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
72
|
+
setTimeout(() => reject(new Error('WebSocket connection timeout')), timeout);
|
|
73
|
+
});
|
|
74
|
+
yield Promise.race([wsPromise, timeoutPromise]);
|
|
75
|
+
this.isConnected = true;
|
|
76
|
+
this.reconnectAttempts = 0;
|
|
77
|
+
(0, dist_1.log_info)(`WebSocket 已连接: ${this.ws_endpoint}`);
|
|
78
|
+
this.wsProvider.on('error', (err) => {
|
|
79
|
+
(0, dist_1.log_error)(`WebSocket 错误:`, err, '');
|
|
80
|
+
this.handleConnectionIssue();
|
|
81
|
+
});
|
|
82
|
+
this.wsProvider._websocket.on('close', (code, reason) => {
|
|
83
|
+
(0, dist_1.log_warn)(`WebSocket 连接关闭, 代码: ${code}, 原因: ${reason || '未知'}`);
|
|
84
|
+
this.handleConnectionIssue();
|
|
85
|
+
});
|
|
86
|
+
if (this.isStarted) {
|
|
87
|
+
yield this.startBlockListener();
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
87
90
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
catch (error) {
|
|
92
|
+
(0, dist_1.log_error)(`WebSocket 连接失败 (尝试 ${i + 1}/${maxRetries}):`, error);
|
|
93
|
+
if (i === maxRetries - 1) {
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
const delay = 1000 * (i + 1);
|
|
97
|
+
(0, dist_1.log_info)(`等待 ${delay / 1000} 秒后重试...`);
|
|
98
|
+
yield new Promise(resolve => setTimeout(resolve, delay));
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
});
|
|
@@ -85,21 +85,20 @@ class SimpleRedisClient {
|
|
|
85
85
|
yield new Promise(resolve => setTimeout(resolve, this.lockRetryDelayMs));
|
|
86
86
|
retries++;
|
|
87
87
|
}
|
|
88
|
-
if (
|
|
89
|
-
|
|
88
|
+
if (acquired) {
|
|
89
|
+
get_lock_time = Date.now();
|
|
90
|
+
return yield callback();
|
|
90
91
|
}
|
|
91
|
-
get_lock_time = Date.now();
|
|
92
|
-
return yield callback();
|
|
93
92
|
}
|
|
94
93
|
finally {
|
|
95
94
|
if (acquired) {
|
|
96
95
|
const release_delay = parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || String(release_lock_delay_ms));
|
|
97
96
|
yield (0, dist_1.sleep)(release_delay);
|
|
98
97
|
yield this.releaseLock(lock_key, lock_value);
|
|
99
|
-
(0, dist_1.log_info)(`withLock success: lock_key=${lock_key}, lock_value=${lock_value},
|
|
98
|
+
(0, dist_1.log_info)(`withLock success: lock_key=${lock_key}, lock_value=${lock_value}, retry times=${retries}, get lock take ${get_lock_time - first_try_time}ms, release_delay=${release_delay}ms, hold lock ${Date.now() - get_lock_time}ms`);
|
|
100
99
|
}
|
|
101
100
|
else {
|
|
102
|
-
(0, dist_1.log_warn)(`withLock failed: lock_key=${lock_key}, lock_value=${lock_value},
|
|
101
|
+
(0, dist_1.log_warn)(`withLock failed: lock_key=${lock_key}, lock_value=${lock_value}, retry times=${retries}, took ${Date.now() - first_try_time}ms`);
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
104
|
});
|