@clonegod/ttd-bsc-common 1.0.8 → 1.0.9
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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/redis/index.d.ts +16 -0
- package/dist/redis/index.js +114 -0
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RedisClientType } from "redis";
|
|
2
|
+
export declare class SimpleRedisClient {
|
|
3
|
+
private lock_prefix;
|
|
4
|
+
private redisClient;
|
|
5
|
+
private lockMaxRetries;
|
|
6
|
+
private lockRetryDelayMs;
|
|
7
|
+
private lockExpireSeconds;
|
|
8
|
+
constructor(lock_prefix: string);
|
|
9
|
+
getRedisClient(): Promise<RedisClientType>;
|
|
10
|
+
private getLockKey;
|
|
11
|
+
private acquireLock;
|
|
12
|
+
private releaseLock;
|
|
13
|
+
withLock<T>(lock_identifier: string, callback: () => Promise<T>): Promise<T>;
|
|
14
|
+
getLockValue(lock_key: string): Promise<string>;
|
|
15
|
+
setLockValue(lock_key: string, lock_value: string): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SimpleRedisClient = void 0;
|
|
13
|
+
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
14
|
+
class SimpleRedisClient {
|
|
15
|
+
constructor(lock_prefix) {
|
|
16
|
+
this.lock_prefix = lock_prefix;
|
|
17
|
+
this.redisClient = null;
|
|
18
|
+
this.lockMaxRetries = 10;
|
|
19
|
+
this.lockRetryDelayMs = 300;
|
|
20
|
+
this.lockExpireSeconds = 10;
|
|
21
|
+
}
|
|
22
|
+
getRedisClient() {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
if (!this.redisClient) {
|
|
25
|
+
this.redisClient = yield (0, dist_1.getRedisCache)();
|
|
26
|
+
}
|
|
27
|
+
return this.redisClient;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
getLockKey(lock_identifier) {
|
|
31
|
+
return `${this.lock_prefix}:lock:${lock_identifier}`;
|
|
32
|
+
}
|
|
33
|
+
acquireLock(lock_key_1, lock_value_1) {
|
|
34
|
+
return __awaiter(this, arguments, void 0, function* (lock_key, lock_value, expireSeconds = this.lockExpireSeconds) {
|
|
35
|
+
const redisClient = yield this.getRedisClient();
|
|
36
|
+
const result = yield redisClient.set(lock_key, lock_value, {
|
|
37
|
+
NX: true,
|
|
38
|
+
EX: expireSeconds
|
|
39
|
+
});
|
|
40
|
+
(0, dist_1.log_info)(`try acquireLock: lock_key=${lock_key}, lock_value=${lock_value}, expireSeconds=${expireSeconds}, result=${result}`);
|
|
41
|
+
const success = result === 'OK';
|
|
42
|
+
if (success) {
|
|
43
|
+
(0, dist_1.log_info)(`acquire lock success: lock_key=${lock_key}, lock_value=${lock_value}`);
|
|
44
|
+
}
|
|
45
|
+
return success;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
releaseLock(lock_key, lock_value) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const redisClient = yield this.getRedisClient();
|
|
51
|
+
const script = `
|
|
52
|
+
if redis.call('get', KEYS[1]) == ARGV[1] then
|
|
53
|
+
return redis.call('del', KEYS[1])
|
|
54
|
+
else
|
|
55
|
+
return 0
|
|
56
|
+
end
|
|
57
|
+
`;
|
|
58
|
+
const result = yield redisClient.eval(script, {
|
|
59
|
+
keys: [lock_key],
|
|
60
|
+
arguments: [lock_value]
|
|
61
|
+
});
|
|
62
|
+
const success = Number(result) === 1;
|
|
63
|
+
if (success) {
|
|
64
|
+
(0, dist_1.log_info)(`release lock success: lock_key=${lock_key}, lock_value=${lock_value}`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
(0, dist_1.log_info)(`release lock failed: lock_key=${lock_key}, lock_value=${lock_value}, maybe expired or locked by other process`);
|
|
68
|
+
}
|
|
69
|
+
return success;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
withLock(lock_identifier, callback) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
const lock_key = this.getLockKey(lock_identifier);
|
|
75
|
+
const lock_value = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
76
|
+
let retries = 0;
|
|
77
|
+
let acquired = false;
|
|
78
|
+
try {
|
|
79
|
+
while (retries < this.lockMaxRetries) {
|
|
80
|
+
acquired = yield this.acquireLock(lock_key, lock_value);
|
|
81
|
+
if (acquired)
|
|
82
|
+
break;
|
|
83
|
+
yield new Promise(resolve => setTimeout(resolve, this.lockRetryDelayMs));
|
|
84
|
+
retries++;
|
|
85
|
+
}
|
|
86
|
+
if (!acquired) {
|
|
87
|
+
throw new Error(`acquire lock failed: lock_key=${lock_key}, lock_value=${lock_value}, after ${this.lockMaxRetries} retries, maybe locked by other process`);
|
|
88
|
+
}
|
|
89
|
+
return yield callback();
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
if (acquired) {
|
|
93
|
+
yield (0, dist_1.sleep)(parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || '2000'));
|
|
94
|
+
yield this.releaseLock(lock_key, lock_value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
getLockValue(lock_key) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const redisClient = yield this.getRedisClient();
|
|
102
|
+
return yield redisClient.get(lock_key);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
setLockValue(lock_key, lock_value) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const redisClient = yield this.getRedisClient();
|
|
108
|
+
yield redisClient.set(lock_key, lock_value, {
|
|
109
|
+
EX: this.lockExpireSeconds
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.SimpleRedisClient = SimpleRedisClient;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ethers } from "ethers";
|
|
2
2
|
import Decimal from "decimal.js";
|
|
3
3
|
export * from './gas_helper';
|
|
4
|
-
export * from './redis_lock';
|
|
5
4
|
export declare const sleep: (ms: number) => Promise<void>;
|
|
6
5
|
export declare const formatPrice: (price: number, precision?: number) => string;
|
|
7
6
|
export declare const formatUnits: (value: ethers.BigNumber, decimals: number) => Decimal;
|
package/dist/utils/index.js
CHANGED
|
@@ -21,7 +21,6 @@ exports.formatNumberHighPrecision = exports.formatUnits = exports.formatPrice =
|
|
|
21
21
|
const ethers_1 = require("ethers");
|
|
22
22
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
23
23
|
__exportStar(require("./gas_helper"), exports);
|
|
24
|
-
__exportStar(require("./redis_lock"), exports);
|
|
25
24
|
const sleep = (ms) => {
|
|
26
25
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
27
26
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clonegod/ttd-bsc-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "BSC common library",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"push": "npm run build && npm publish"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@clonegod/ttd-core": "2.0.
|
|
17
|
+
"@clonegod/ttd-core": "2.0.27",
|
|
18
18
|
"axios": "^1.8.4",
|
|
19
19
|
"dotenv": "^16.4.7",
|
|
20
20
|
"ethers": "^5.8.0"
|