@clonegod/ttd-core 3.1.8 → 3.1.10

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.
@@ -1,5 +1,3 @@
1
- export * from './redis_client';
2
- export * from './redis_cmd';
3
1
  export * from './loading_cache';
4
2
  export * from './arb_cache';
5
3
  export * from './arb_event_pub';
@@ -14,8 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./redis_client"), exports);
18
- __exportStar(require("./redis_cmd"), exports);
19
17
  __exportStar(require("./loading_cache"), exports);
20
18
  __exportStar(require("./arb_cache"), exports);
21
19
  __exportStar(require("./arb_event_pub"), exports);
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.LoadingCache = void 0;
13
13
  const index_1 = require("../index");
14
- const redis_client_1 = require("./redis_client");
14
+ const redis_connection_1 = require("../redis/redis_connection");
15
15
  class LoadingCache {
16
16
  constructor() {
17
17
  }
@@ -19,7 +19,7 @@ class LoadingCache {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
20
  this.local_cache = new Map();
21
21
  this.refresh_cache_timer = new Map();
22
- this.redis_client = yield (0, redis_client_1.getRedisClient)('LoadingCache');
22
+ this.redis_client = yield (0, redis_connection_1.getRedisConnection)('LoadingCache');
23
23
  (0, index_1.log_info)('[LoadingCache] init done');
24
24
  });
25
25
  }
package/dist/index.d.ts CHANGED
@@ -2,9 +2,11 @@ import BN from 'bn.js';
2
2
  import Decimal from 'decimal.js';
3
3
  import fs from "fs";
4
4
  import { ArbCache, StandardPoolInfoType, StandardTokenInfoType, TokenPriceWithAmountType, TradeCommandLineArgs, TradeResponseType, UniqueOrderbookIdType, WalletInfoType } from '../types';
5
+ import * as redis from './redis';
5
6
  import * as cache from './cache';
6
7
  export * from './analyze';
7
8
  export * from './app_config';
9
+ export * from './redis';
8
10
  export * from './cache';
9
11
  export * from './constants';
10
12
  export * from './market_price';
@@ -133,11 +135,9 @@ export declare const format_unique_orderbook_id: (chain_id: CHAIN_ID, dex_id: DE
133
135
  export declare const parse_unique_orderbook_id: (unique_orderbook_id: string) => UniqueOrderbookIdType;
134
136
  export declare const format_unique_order_msg_id: (group_id: string, price_id: string, order_trace_id: string) => string;
135
137
  export declare const format_order_lock_key: (chain_id: string, group_id: string, price_id: string, order_trace_id: string) => string;
136
- export declare const getRedisClient: () => Promise<import("redis").RedisClientType>;
137
- export declare const getRedisCache: () => Promise<import("redis").RedisClientType>;
138
+ export declare const getRedisClient: (lockPrefix?: string) => redis.RedisClient;
139
+ export declare const getRedisCommamd: () => redis.RedisClient;
138
140
  export declare const getLoadingCache: () => cache.LoadingCache;
139
- export declare const getRedisCommamd: () => cache.RedisClient;
140
- export declare const createRedisClient: (lockPrefix?: string) => cache.RedisClient;
141
141
  export declare const getArbCache: (env_args: any) => cache.ArbCache;
142
142
  export declare const getArbEventPublisher: (arb_cache: any) => cache.ArbEventPublisher;
143
143
  export declare const getArbEventSubscriber: (arb_cache: any) => cache.ArbEventSubscriber;
package/dist/index.js CHANGED
@@ -48,8 +48,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
48
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
49
  };
50
50
  Object.defineProperty(exports, "__esModule", { value: true });
51
- exports.DecimalUtil = exports.generateRandomNumber = exports.LOG = exports._active_log_level = exports.LOG_LEVEL = exports.REDIS_EVENT_TYPE_TRADE_INSTANCE_CHANGE = exports.REDIS_EVENT_TYPE_ORDER = exports.REDIS_EVENT_TYPE_QUOTE = exports.REDIS_EVENT_TYPE_CONFIG_CHANGE = exports.REDIS_EVENT_CHANNEL = exports.get_new_block_channel_name = exports.get_tx_result_channel_name = exports.get_wallet_raw_tx_channel_name = exports.get_order_channel_name = exports.get_quote_channel_name = exports.get_config_channel_name = exports.get_cache_key = exports.getArbEventSubscriber = exports.getArbEventPublisher = exports.getArbCache = exports.createRedisClient = exports.getRedisCommamd = exports.getLoadingCache = exports.getRedisCache = exports.getRedisClient = exports.format_order_lock_key = exports.format_unique_order_msg_id = exports.parse_unique_orderbook_id = exports.format_unique_orderbook_id = exports.format_symbol_name = exports.LOCAL_EVENT_NAME = exports.CACHE_KEY_TYPE = exports.OnChainDataSubscribeType = exports.TradeErrorCodeType = exports.SystemErrorCodeType = exports.TRADE_TYPE = exports.GROUP_ID = exports.DEX_ID = exports.CHAIN_ID = exports.STREAMING_TRANSACTION_CONFIRMED = exports.TRANSACTION_STATE_FAILED = exports.TRANSACTION_STATE_SUCCESS = exports.TRANSACTION_STATE_PROCESSING = exports.QUOTE_AMOUNT_USD_MAX = exports.QUOTE_AMOUNT_USD_MIN = exports.REGEX_HEX_PREFIX = exports.PROCESSING = exports.NOT_FOUND = exports.SUCCESS = exports.FAILED = void 0;
52
- exports.execute_bash = exports.inspect_arb_local_cache = exports.save_trade_execution_logs = exports.parse_trade_cmdline_args = exports.get_input_out_token_fix = exports.get_input_out_token = exports.calc_amount_in_token = exports.estimate_amount_in_token = exports.failed = exports.success = exports.home_dir = exports.postJSON = exports.deep_merge_object = exports.isArray = exports.isTrue = exports.deep_clone = exports.uuid = exports.DATE_TIME_FORMAT_NO_WHITESPACE = exports.DATE_TIME_FORMAT_DEFAULT = exports.to_json_str = void 0;
51
+ exports.DATE_TIME_FORMAT_DEFAULT = exports.to_json_str = exports.DecimalUtil = exports.generateRandomNumber = exports.LOG = exports._active_log_level = exports.LOG_LEVEL = exports.REDIS_EVENT_TYPE_TRADE_INSTANCE_CHANGE = exports.REDIS_EVENT_TYPE_ORDER = exports.REDIS_EVENT_TYPE_QUOTE = exports.REDIS_EVENT_TYPE_CONFIG_CHANGE = exports.REDIS_EVENT_CHANNEL = exports.get_new_block_channel_name = exports.get_tx_result_channel_name = exports.get_wallet_raw_tx_channel_name = exports.get_order_channel_name = exports.get_quote_channel_name = exports.get_config_channel_name = exports.get_cache_key = exports.getArbEventSubscriber = exports.getArbEventPublisher = exports.getArbCache = exports.getLoadingCache = exports.getRedisCommamd = exports.getRedisClient = exports.format_order_lock_key = exports.format_unique_order_msg_id = exports.parse_unique_orderbook_id = exports.format_unique_orderbook_id = exports.format_symbol_name = exports.LOCAL_EVENT_NAME = exports.CACHE_KEY_TYPE = exports.OnChainDataSubscribeType = exports.TradeErrorCodeType = exports.SystemErrorCodeType = exports.TRADE_TYPE = exports.GROUP_ID = exports.DEX_ID = exports.CHAIN_ID = exports.STREAMING_TRANSACTION_CONFIRMED = exports.TRANSACTION_STATE_FAILED = exports.TRANSACTION_STATE_SUCCESS = exports.TRANSACTION_STATE_PROCESSING = exports.QUOTE_AMOUNT_USD_MAX = exports.QUOTE_AMOUNT_USD_MIN = exports.REGEX_HEX_PREFIX = exports.PROCESSING = exports.NOT_FOUND = exports.SUCCESS = exports.FAILED = void 0;
52
+ exports.execute_bash = exports.inspect_arb_local_cache = exports.save_trade_execution_logs = exports.parse_trade_cmdline_args = exports.get_input_out_token_fix = exports.get_input_out_token = exports.calc_amount_in_token = exports.estimate_amount_in_token = exports.failed = exports.success = exports.home_dir = exports.postJSON = exports.deep_merge_object = exports.isArray = exports.isTrue = exports.deep_clone = exports.uuid = exports.DATE_TIME_FORMAT_NO_WHITESPACE = void 0;
53
53
  exports._caller = _caller;
54
54
  exports.log_trace = log_trace;
55
55
  exports.log_debug = log_debug;
@@ -82,11 +82,13 @@ const fs_1 = __importDefault(require("fs"));
82
82
  const moment_1 = __importDefault(require("moment"));
83
83
  const os_1 = __importDefault(require("os"));
84
84
  const path_1 = __importDefault(require("path"));
85
+ const redis = __importStar(require("./redis"));
85
86
  const cache = __importStar(require("./cache"));
86
87
  const market_price_1 = require("./market_price");
87
88
  const crypto_1 = require("./util/crypto");
88
89
  __exportStar(require("./analyze"), exports);
89
90
  __exportStar(require("./app_config"), exports);
91
+ __exportStar(require("./redis"), exports);
90
92
  __exportStar(require("./cache"), exports);
91
93
  __exportStar(require("./constants"), exports);
92
94
  __exportStar(require("./market_price"), exports);
@@ -254,23 +256,18 @@ const format_order_lock_key = (chain_id, group_id, price_id, order_trace_id) =>
254
256
  return `${chain_id}:lock:${group_id}:${price_id}:${order_trace_id}`;
255
257
  };
256
258
  exports.format_order_lock_key = format_order_lock_key;
257
- const getRedisClient = () => __awaiter(void 0, void 0, void 0, function* () {
258
- return cache.getRedisClient();
259
- });
260
- exports.getRedisClient = getRedisClient;
261
- exports.getRedisCache = exports.getRedisClient;
262
- const getLoadingCache = () => {
263
- return new cache.LoadingCache();
259
+ const getRedisClient = (lockPrefix = '') => {
260
+ return new redis.RedisClient(lockPrefix);
264
261
  };
265
- exports.getLoadingCache = getLoadingCache;
262
+ exports.getRedisClient = getRedisClient;
266
263
  const getRedisCommamd = () => {
267
- return new cache.RedisClient();
264
+ return new redis.RedisClient();
268
265
  };
269
266
  exports.getRedisCommamd = getRedisCommamd;
270
- const createRedisClient = (lockPrefix = '') => {
271
- return new cache.RedisClient(lockPrefix);
267
+ const getLoadingCache = () => {
268
+ return new cache.LoadingCache();
272
269
  };
273
- exports.createRedisClient = createRedisClient;
270
+ exports.getLoadingCache = getLoadingCache;
274
271
  const getArbCache = (env_args) => {
275
272
  return new cache.ArbCache(env_args);
276
273
  };
@@ -0,0 +1 @@
1
+ export { RedisClient } from './redis_client';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisClient = void 0;
4
+ var redis_client_1 = require("./redis_client");
5
+ Object.defineProperty(exports, "RedisClient", { enumerable: true, get: function () { return redis_client_1.RedisClient; } });
@@ -0,0 +1,328 @@
1
+ import { RedisClientType } from "redis";
2
+ export declare class RedisClient {
3
+ redis_client: RedisClientType;
4
+ private lockPrefix;
5
+ private lockMaxRetries;
6
+ private lockRetryDelayMs;
7
+ private lockExpireSeconds;
8
+ constructor(lockPrefix?: string);
9
+ init(): Promise<void>;
10
+ private ensureClient;
11
+ exists(key: string): Promise<number>;
12
+ ttl(key: string): Promise<number>;
13
+ expire(key: string, ttl: number): Promise<boolean>;
14
+ del(key: string, field?: string): Promise<number>;
15
+ set_nx(key: string, value: string): Promise<boolean>;
16
+ get(key: string): Promise<string | null>;
17
+ set(key: string, value: string, expireSeconds?: number): Promise<string | null>;
18
+ hset(key: string, field: string, value: string, expireSeconds?: number): Promise<number>;
19
+ hget(key: string, field: string): Promise<string | undefined>;
20
+ hgetall(key: string): Promise<Record<string, string>>;
21
+ hmget(key: string, fields: string[]): Promise<(string | null)[]>;
22
+ hkeys(key: string): Promise<string[]>;
23
+ hdel(key: string, field: string): Promise<number>;
24
+ hlen(key: string): Promise<number>;
25
+ hinc(key: string, field: string, ttl?: number): Promise<number[]>;
26
+ lrange(key: string, start?: number, stop?: number): Promise<string[]>;
27
+ lpush(key: string, value: string): Promise<number>;
28
+ ltrim(key: string, start: number, stop: number): Promise<string>;
29
+ ping(): Promise<string>;
30
+ type(key: string): Promise<string>;
31
+ multi(): import("@redis/client/dist/lib/client/multi-command").RedisClientMultiCommandType<{
32
+ graph: {
33
+ CONFIG_GET: typeof import("@redis/graph/dist/commands/CONFIG_GET");
34
+ configGet: typeof import("@redis/graph/dist/commands/CONFIG_GET");
35
+ CONFIG_SET: typeof import("@redis/graph/dist/commands/CONFIG_SET");
36
+ configSet: typeof import("@redis/graph/dist/commands/CONFIG_SET");
37
+ DELETE: typeof import("@redis/graph/dist/commands/DELETE");
38
+ delete: typeof import("@redis/graph/dist/commands/DELETE");
39
+ EXPLAIN: typeof import("@redis/graph/dist/commands/EXPLAIN");
40
+ explain: typeof import("@redis/graph/dist/commands/EXPLAIN");
41
+ LIST: typeof import("@redis/graph/dist/commands/LIST");
42
+ list: typeof import("@redis/graph/dist/commands/LIST");
43
+ PROFILE: typeof import("@redis/graph/dist/commands/PROFILE");
44
+ profile: typeof import("@redis/graph/dist/commands/PROFILE");
45
+ QUERY: typeof import("@redis/graph/dist/commands/QUERY");
46
+ query: typeof import("@redis/graph/dist/commands/QUERY");
47
+ RO_QUERY: typeof import("@redis/graph/dist/commands/RO_QUERY");
48
+ roQuery: typeof import("@redis/graph/dist/commands/RO_QUERY");
49
+ SLOWLOG: typeof import("@redis/graph/dist/commands/SLOWLOG");
50
+ slowLog: typeof import("@redis/graph/dist/commands/SLOWLOG");
51
+ };
52
+ json: {
53
+ ARRAPPEND: typeof import("@redis/json/dist/commands/ARRAPPEND");
54
+ arrAppend: typeof import("@redis/json/dist/commands/ARRAPPEND");
55
+ ARRINDEX: typeof import("@redis/json/dist/commands/ARRINDEX");
56
+ arrIndex: typeof import("@redis/json/dist/commands/ARRINDEX");
57
+ ARRINSERT: typeof import("@redis/json/dist/commands/ARRINSERT");
58
+ arrInsert: typeof import("@redis/json/dist/commands/ARRINSERT");
59
+ ARRLEN: typeof import("@redis/json/dist/commands/ARRLEN");
60
+ arrLen: typeof import("@redis/json/dist/commands/ARRLEN");
61
+ ARRPOP: typeof import("@redis/json/dist/commands/ARRPOP");
62
+ arrPop: typeof import("@redis/json/dist/commands/ARRPOP");
63
+ ARRTRIM: typeof import("@redis/json/dist/commands/ARRTRIM");
64
+ arrTrim: typeof import("@redis/json/dist/commands/ARRTRIM");
65
+ DEBUG_MEMORY: typeof import("@redis/json/dist/commands/DEBUG_MEMORY");
66
+ debugMemory: typeof import("@redis/json/dist/commands/DEBUG_MEMORY");
67
+ DEL: typeof import("@redis/json/dist/commands/DEL");
68
+ del: typeof import("@redis/json/dist/commands/DEL");
69
+ FORGET: typeof import("@redis/json/dist/commands/FORGET");
70
+ forget: typeof import("@redis/json/dist/commands/FORGET");
71
+ GET: typeof import("@redis/json/dist/commands/GET");
72
+ get: typeof import("@redis/json/dist/commands/GET");
73
+ MERGE: typeof import("@redis/json/dist/commands/MERGE");
74
+ merge: typeof import("@redis/json/dist/commands/MERGE");
75
+ MGET: typeof import("@redis/json/dist/commands/MGET");
76
+ mGet: typeof import("@redis/json/dist/commands/MGET");
77
+ MSET: typeof import("@redis/json/dist/commands/MSET");
78
+ mSet: typeof import("@redis/json/dist/commands/MSET");
79
+ NUMINCRBY: typeof import("@redis/json/dist/commands/NUMINCRBY");
80
+ numIncrBy: typeof import("@redis/json/dist/commands/NUMINCRBY");
81
+ NUMMULTBY: typeof import("@redis/json/dist/commands/NUMMULTBY");
82
+ numMultBy: typeof import("@redis/json/dist/commands/NUMMULTBY");
83
+ OBJKEYS: typeof import("@redis/json/dist/commands/OBJKEYS");
84
+ objKeys: typeof import("@redis/json/dist/commands/OBJKEYS");
85
+ OBJLEN: typeof import("@redis/json/dist/commands/OBJLEN");
86
+ objLen: typeof import("@redis/json/dist/commands/OBJLEN");
87
+ RESP: typeof import("@redis/json/dist/commands/RESP");
88
+ resp: typeof import("@redis/json/dist/commands/RESP");
89
+ SET: typeof import("@redis/json/dist/commands/SET");
90
+ set: typeof import("@redis/json/dist/commands/SET");
91
+ STRAPPEND: typeof import("@redis/json/dist/commands/STRAPPEND");
92
+ strAppend: typeof import("@redis/json/dist/commands/STRAPPEND");
93
+ STRLEN: typeof import("@redis/json/dist/commands/STRLEN");
94
+ strLen: typeof import("@redis/json/dist/commands/STRLEN");
95
+ TYPE: typeof import("@redis/json/dist/commands/TYPE");
96
+ type: typeof import("@redis/json/dist/commands/TYPE");
97
+ };
98
+ ft: {
99
+ _LIST: typeof import("@redis/search/dist/commands/_LIST");
100
+ _list: typeof import("@redis/search/dist/commands/_LIST");
101
+ ALTER: typeof import("@redis/search/dist/commands/ALTER");
102
+ alter: typeof import("@redis/search/dist/commands/ALTER");
103
+ AGGREGATE_WITHCURSOR: typeof import("@redis/search/dist/commands/AGGREGATE_WITHCURSOR");
104
+ aggregateWithCursor: typeof import("@redis/search/dist/commands/AGGREGATE_WITHCURSOR");
105
+ AGGREGATE: typeof import("@redis/search/dist/commands/AGGREGATE");
106
+ aggregate: typeof import("@redis/search/dist/commands/AGGREGATE");
107
+ ALIASADD: typeof import("@redis/search/dist/commands/ALIASADD");
108
+ aliasAdd: typeof import("@redis/search/dist/commands/ALIASADD");
109
+ ALIASDEL: typeof import("@redis/search/dist/commands/ALIASDEL");
110
+ aliasDel: typeof import("@redis/search/dist/commands/ALIASDEL");
111
+ ALIASUPDATE: typeof import("@redis/search/dist/commands/ALIASUPDATE");
112
+ aliasUpdate: typeof import("@redis/search/dist/commands/ALIASUPDATE");
113
+ CONFIG_GET: typeof import("@redis/search/dist/commands/CONFIG_GET");
114
+ configGet: typeof import("@redis/search/dist/commands/CONFIG_GET");
115
+ CONFIG_SET: typeof import("@redis/search/dist/commands/CONFIG_SET");
116
+ configSet: typeof import("@redis/search/dist/commands/CONFIG_SET");
117
+ CREATE: typeof import("@redis/search/dist/commands/CREATE");
118
+ create: typeof import("@redis/search/dist/commands/CREATE");
119
+ CURSOR_DEL: typeof import("@redis/search/dist/commands/CURSOR_DEL");
120
+ cursorDel: typeof import("@redis/search/dist/commands/CURSOR_DEL");
121
+ CURSOR_READ: typeof import("@redis/search/dist/commands/CURSOR_READ");
122
+ cursorRead: typeof import("@redis/search/dist/commands/CURSOR_READ");
123
+ DICTADD: typeof import("@redis/search/dist/commands/DICTADD");
124
+ dictAdd: typeof import("@redis/search/dist/commands/DICTADD");
125
+ DICTDEL: typeof import("@redis/search/dist/commands/DICTDEL");
126
+ dictDel: typeof import("@redis/search/dist/commands/DICTDEL");
127
+ DICTDUMP: typeof import("@redis/search/dist/commands/DICTDUMP");
128
+ dictDump: typeof import("@redis/search/dist/commands/DICTDUMP");
129
+ DROPINDEX: typeof import("@redis/search/dist/commands/DROPINDEX");
130
+ dropIndex: typeof import("@redis/search/dist/commands/DROPINDEX");
131
+ EXPLAIN: typeof import("@redis/search/dist/commands/EXPLAIN");
132
+ explain: typeof import("@redis/search/dist/commands/EXPLAIN");
133
+ EXPLAINCLI: typeof import("@redis/search/dist/commands/EXPLAINCLI");
134
+ explainCli: typeof import("@redis/search/dist/commands/EXPLAINCLI");
135
+ INFO: typeof import("@redis/search/dist/commands/INFO");
136
+ info: typeof import("@redis/search/dist/commands/INFO");
137
+ PROFILESEARCH: typeof import("@redis/search/dist/commands/PROFILE_SEARCH");
138
+ profileSearch: typeof import("@redis/search/dist/commands/PROFILE_SEARCH");
139
+ PROFILEAGGREGATE: typeof import("@redis/search/dist/commands/PROFILE_AGGREGATE");
140
+ profileAggregate: typeof import("@redis/search/dist/commands/PROFILE_AGGREGATE");
141
+ SEARCH: typeof import("@redis/search/dist/commands/SEARCH");
142
+ search: typeof import("@redis/search/dist/commands/SEARCH");
143
+ SEARCH_NOCONTENT: typeof import("@redis/search/dist/commands/SEARCH_NOCONTENT");
144
+ searchNoContent: typeof import("@redis/search/dist/commands/SEARCH_NOCONTENT");
145
+ SPELLCHECK: typeof import("@redis/search/dist/commands/SPELLCHECK");
146
+ spellCheck: typeof import("@redis/search/dist/commands/SPELLCHECK");
147
+ SUGADD: typeof import("@redis/search/dist/commands/SUGADD");
148
+ sugAdd: typeof import("@redis/search/dist/commands/SUGADD");
149
+ SUGDEL: typeof import("@redis/search/dist/commands/SUGDEL");
150
+ sugDel: typeof import("@redis/search/dist/commands/SUGDEL");
151
+ SUGGET_WITHPAYLOADS: typeof import("@redis/search/dist/commands/SUGGET_WITHPAYLOADS");
152
+ sugGetWithPayloads: typeof import("@redis/search/dist/commands/SUGGET_WITHPAYLOADS");
153
+ SUGGET_WITHSCORES_WITHPAYLOADS: typeof import("@redis/search/dist/commands/SUGGET_WITHSCORES_WITHPAYLOADS");
154
+ sugGetWithScoresWithPayloads: typeof import("@redis/search/dist/commands/SUGGET_WITHSCORES_WITHPAYLOADS");
155
+ SUGGET_WITHSCORES: typeof import("@redis/search/dist/commands/SUGGET_WITHSCORES");
156
+ sugGetWithScores: typeof import("@redis/search/dist/commands/SUGGET_WITHSCORES");
157
+ SUGGET: typeof import("@redis/search/dist/commands/SUGGET");
158
+ sugGet: typeof import("@redis/search/dist/commands/SUGGET");
159
+ SUGLEN: typeof import("@redis/search/dist/commands/SUGLEN");
160
+ sugLen: typeof import("@redis/search/dist/commands/SUGLEN");
161
+ SYNDUMP: typeof import("@redis/search/dist/commands/SYNDUMP");
162
+ synDump: typeof import("@redis/search/dist/commands/SYNDUMP");
163
+ SYNUPDATE: typeof import("@redis/search/dist/commands/SYNUPDATE");
164
+ synUpdate: typeof import("@redis/search/dist/commands/SYNUPDATE");
165
+ TAGVALS: typeof import("@redis/search/dist/commands/TAGVALS");
166
+ tagVals: typeof import("@redis/search/dist/commands/TAGVALS");
167
+ };
168
+ ts: {
169
+ ADD: typeof import("@redis/time-series/dist/commands/ADD");
170
+ add: typeof import("@redis/time-series/dist/commands/ADD");
171
+ ALTER: typeof import("@redis/time-series/dist/commands/ALTER");
172
+ alter: typeof import("@redis/time-series/dist/commands/ALTER");
173
+ CREATE: typeof import("@redis/time-series/dist/commands/CREATE");
174
+ create: typeof import("@redis/time-series/dist/commands/CREATE");
175
+ CREATERULE: typeof import("@redis/time-series/dist/commands/CREATERULE");
176
+ createRule: typeof import("@redis/time-series/dist/commands/CREATERULE");
177
+ DECRBY: typeof import("@redis/time-series/dist/commands/DECRBY");
178
+ decrBy: typeof import("@redis/time-series/dist/commands/DECRBY");
179
+ DEL: typeof import("@redis/time-series/dist/commands/DEL");
180
+ del: typeof import("@redis/time-series/dist/commands/DEL");
181
+ DELETERULE: typeof import("@redis/time-series/dist/commands/DELETERULE");
182
+ deleteRule: typeof import("@redis/time-series/dist/commands/DELETERULE");
183
+ GET: typeof import("@redis/time-series/dist/commands/GET");
184
+ get: typeof import("@redis/time-series/dist/commands/GET");
185
+ INCRBY: typeof import("@redis/time-series/dist/commands/INCRBY");
186
+ incrBy: typeof import("@redis/time-series/dist/commands/INCRBY");
187
+ INFO_DEBUG: typeof import("@redis/time-series/dist/commands/INFO_DEBUG");
188
+ infoDebug: typeof import("@redis/time-series/dist/commands/INFO_DEBUG");
189
+ INFO: typeof import("@redis/time-series/dist/commands/INFO");
190
+ info: typeof import("@redis/time-series/dist/commands/INFO");
191
+ MADD: typeof import("@redis/time-series/dist/commands/MADD");
192
+ mAdd: typeof import("@redis/time-series/dist/commands/MADD");
193
+ MGET: typeof import("@redis/time-series/dist/commands/MGET");
194
+ mGet: typeof import("@redis/time-series/dist/commands/MGET");
195
+ MGET_WITHLABELS: typeof import("@redis/time-series/dist/commands/MGET_WITHLABELS");
196
+ mGetWithLabels: typeof import("@redis/time-series/dist/commands/MGET_WITHLABELS");
197
+ QUERYINDEX: typeof import("@redis/time-series/dist/commands/QUERYINDEX");
198
+ queryIndex: typeof import("@redis/time-series/dist/commands/QUERYINDEX");
199
+ RANGE: typeof import("@redis/time-series/dist/commands/RANGE");
200
+ range: typeof import("@redis/time-series/dist/commands/RANGE");
201
+ REVRANGE: typeof import("@redis/time-series/dist/commands/REVRANGE");
202
+ revRange: typeof import("@redis/time-series/dist/commands/REVRANGE");
203
+ MRANGE: typeof import("@redis/time-series/dist/commands/MRANGE");
204
+ mRange: typeof import("@redis/time-series/dist/commands/MRANGE");
205
+ MRANGE_WITHLABELS: typeof import("@redis/time-series/dist/commands/MRANGE_WITHLABELS");
206
+ mRangeWithLabels: typeof import("@redis/time-series/dist/commands/MRANGE_WITHLABELS");
207
+ MREVRANGE: typeof import("@redis/time-series/dist/commands/MREVRANGE");
208
+ mRevRange: typeof import("@redis/time-series/dist/commands/MREVRANGE");
209
+ MREVRANGE_WITHLABELS: typeof import("@redis/time-series/dist/commands/MREVRANGE_WITHLABELS");
210
+ mRevRangeWithLabels: typeof import("@redis/time-series/dist/commands/MREVRANGE_WITHLABELS");
211
+ };
212
+ bf: {
213
+ ADD: typeof import("@redis/bloom/dist/commands/bloom/ADD");
214
+ add: typeof import("@redis/bloom/dist/commands/bloom/ADD");
215
+ CARD: typeof import("@redis/bloom/dist/commands/bloom/CARD");
216
+ card: typeof import("@redis/bloom/dist/commands/bloom/CARD");
217
+ EXISTS: typeof import("@redis/bloom/dist/commands/bloom/EXISTS");
218
+ exists: typeof import("@redis/bloom/dist/commands/bloom/EXISTS");
219
+ INFO: typeof import("@redis/bloom/dist/commands/bloom/INFO");
220
+ info: typeof import("@redis/bloom/dist/commands/bloom/INFO");
221
+ INSERT: typeof import("@redis/bloom/dist/commands/bloom/INSERT");
222
+ insert: typeof import("@redis/bloom/dist/commands/bloom/INSERT");
223
+ LOADCHUNK: typeof import("@redis/bloom/dist/commands/bloom/LOADCHUNK");
224
+ loadChunk: typeof import("@redis/bloom/dist/commands/bloom/LOADCHUNK");
225
+ MADD: typeof import("@redis/bloom/dist/commands/bloom/MADD");
226
+ mAdd: typeof import("@redis/bloom/dist/commands/bloom/MADD");
227
+ MEXISTS: typeof import("@redis/bloom/dist/commands/bloom/MEXISTS");
228
+ mExists: typeof import("@redis/bloom/dist/commands/bloom/MEXISTS");
229
+ RESERVE: typeof import("@redis/bloom/dist/commands/bloom/RESERVE");
230
+ reserve: typeof import("@redis/bloom/dist/commands/bloom/RESERVE");
231
+ SCANDUMP: typeof import("@redis/bloom/dist/commands/bloom/SCANDUMP");
232
+ scanDump: typeof import("@redis/bloom/dist/commands/bloom/SCANDUMP");
233
+ };
234
+ cms: {
235
+ INCRBY: typeof import("@redis/bloom/dist/commands/count-min-sketch/INCRBY");
236
+ incrBy: typeof import("@redis/bloom/dist/commands/count-min-sketch/INCRBY");
237
+ INFO: typeof import("@redis/bloom/dist/commands/count-min-sketch/INFO");
238
+ info: typeof import("@redis/bloom/dist/commands/count-min-sketch/INFO");
239
+ INITBYDIM: typeof import("@redis/bloom/dist/commands/count-min-sketch/INITBYDIM");
240
+ initByDim: typeof import("@redis/bloom/dist/commands/count-min-sketch/INITBYDIM");
241
+ INITBYPROB: typeof import("@redis/bloom/dist/commands/count-min-sketch/INITBYPROB");
242
+ initByProb: typeof import("@redis/bloom/dist/commands/count-min-sketch/INITBYPROB");
243
+ MERGE: typeof import("@redis/bloom/dist/commands/count-min-sketch/MERGE");
244
+ merge: typeof import("@redis/bloom/dist/commands/count-min-sketch/MERGE");
245
+ QUERY: typeof import("@redis/bloom/dist/commands/count-min-sketch/QUERY");
246
+ query: typeof import("@redis/bloom/dist/commands/count-min-sketch/QUERY");
247
+ };
248
+ cf: {
249
+ ADD: typeof import("@redis/bloom/dist/commands/cuckoo/ADD");
250
+ add: typeof import("@redis/bloom/dist/commands/cuckoo/ADD");
251
+ ADDNX: typeof import("@redis/bloom/dist/commands/cuckoo/ADDNX");
252
+ addNX: typeof import("@redis/bloom/dist/commands/cuckoo/ADDNX");
253
+ COUNT: typeof import("@redis/bloom/dist/commands/cuckoo/COUNT");
254
+ count: typeof import("@redis/bloom/dist/commands/cuckoo/COUNT");
255
+ DEL: typeof import("@redis/bloom/dist/commands/cuckoo/DEL");
256
+ del: typeof import("@redis/bloom/dist/commands/cuckoo/DEL");
257
+ EXISTS: typeof import("@redis/bloom/dist/commands/cuckoo/EXISTS");
258
+ exists: typeof import("@redis/bloom/dist/commands/cuckoo/EXISTS");
259
+ INFO: typeof import("@redis/bloom/dist/commands/cuckoo/INFO");
260
+ info: typeof import("@redis/bloom/dist/commands/cuckoo/INFO");
261
+ INSERT: typeof import("@redis/bloom/dist/commands/cuckoo/INSERT");
262
+ insert: typeof import("@redis/bloom/dist/commands/cuckoo/INSERT");
263
+ INSERTNX: typeof import("@redis/bloom/dist/commands/cuckoo/INSERTNX");
264
+ insertNX: typeof import("@redis/bloom/dist/commands/cuckoo/INSERTNX");
265
+ LOADCHUNK: typeof import("@redis/bloom/dist/commands/cuckoo/LOADCHUNK");
266
+ loadChunk: typeof import("@redis/bloom/dist/commands/cuckoo/LOADCHUNK");
267
+ RESERVE: typeof import("@redis/bloom/dist/commands/cuckoo/RESERVE");
268
+ reserve: typeof import("@redis/bloom/dist/commands/cuckoo/RESERVE");
269
+ SCANDUMP: typeof import("@redis/bloom/dist/commands/cuckoo/SCANDUMP");
270
+ scanDump: typeof import("@redis/bloom/dist/commands/cuckoo/SCANDUMP");
271
+ };
272
+ tDigest: {
273
+ ADD: typeof import("@redis/bloom/dist/commands/t-digest/ADD");
274
+ add: typeof import("@redis/bloom/dist/commands/t-digest/ADD");
275
+ BYRANK: typeof import("@redis/bloom/dist/commands/t-digest/BYRANK");
276
+ byRank: typeof import("@redis/bloom/dist/commands/t-digest/BYRANK");
277
+ BYREVRANK: typeof import("@redis/bloom/dist/commands/t-digest/BYREVRANK");
278
+ byRevRank: typeof import("@redis/bloom/dist/commands/t-digest/BYREVRANK");
279
+ CDF: typeof import("@redis/bloom/dist/commands/t-digest/CDF");
280
+ cdf: typeof import("@redis/bloom/dist/commands/t-digest/CDF");
281
+ CREATE: typeof import("@redis/bloom/dist/commands/t-digest/CREATE");
282
+ create: typeof import("@redis/bloom/dist/commands/t-digest/CREATE");
283
+ INFO: typeof import("@redis/bloom/dist/commands/t-digest/INFO");
284
+ info: typeof import("@redis/bloom/dist/commands/t-digest/INFO");
285
+ MAX: typeof import("@redis/bloom/dist/commands/t-digest/MAX");
286
+ max: typeof import("@redis/bloom/dist/commands/t-digest/MAX");
287
+ MERGE: typeof import("@redis/bloom/dist/commands/t-digest/MERGE");
288
+ merge: typeof import("@redis/bloom/dist/commands/t-digest/MERGE");
289
+ MIN: typeof import("@redis/bloom/dist/commands/t-digest/MIN");
290
+ min: typeof import("@redis/bloom/dist/commands/t-digest/MIN");
291
+ QUANTILE: typeof import("@redis/bloom/dist/commands/t-digest/QUANTILE");
292
+ quantile: typeof import("@redis/bloom/dist/commands/t-digest/QUANTILE");
293
+ RANK: typeof import("@redis/bloom/dist/commands/t-digest/RANK");
294
+ rank: typeof import("@redis/bloom/dist/commands/t-digest/RANK");
295
+ RESET: typeof import("@redis/bloom/dist/commands/t-digest/RESET");
296
+ reset: typeof import("@redis/bloom/dist/commands/t-digest/RESET");
297
+ REVRANK: typeof import("@redis/bloom/dist/commands/t-digest/REVRANK");
298
+ revRank: typeof import("@redis/bloom/dist/commands/t-digest/REVRANK");
299
+ TRIMMED_MEAN: typeof import("@redis/bloom/dist/commands/t-digest/TRIMMED_MEAN");
300
+ trimmedMean: typeof import("@redis/bloom/dist/commands/t-digest/TRIMMED_MEAN");
301
+ };
302
+ topK: {
303
+ ADD: typeof import("@redis/bloom/dist/commands/top-k/ADD");
304
+ add: typeof import("@redis/bloom/dist/commands/top-k/ADD");
305
+ COUNT: typeof import("@redis/bloom/dist/commands/top-k/COUNT");
306
+ count: typeof import("@redis/bloom/dist/commands/top-k/COUNT");
307
+ INCRBY: typeof import("@redis/bloom/dist/commands/top-k/INCRBY");
308
+ incrBy: typeof import("@redis/bloom/dist/commands/top-k/INCRBY");
309
+ INFO: typeof import("@redis/bloom/dist/commands/top-k/INFO");
310
+ info: typeof import("@redis/bloom/dist/commands/top-k/INFO");
311
+ LIST_WITHCOUNT: typeof import("@redis/bloom/dist/commands/top-k/LIST_WITHCOUNT");
312
+ listWithCount: typeof import("@redis/bloom/dist/commands/top-k/LIST_WITHCOUNT");
313
+ LIST: typeof import("@redis/bloom/dist/commands/top-k/LIST");
314
+ list: typeof import("@redis/bloom/dist/commands/top-k/LIST");
315
+ QUERY: typeof import("@redis/bloom/dist/commands/top-k/QUERY");
316
+ query: typeof import("@redis/bloom/dist/commands/top-k/QUERY");
317
+ RESERVE: typeof import("@redis/bloom/dist/commands/top-k/RESERVE");
318
+ reserve: typeof import("@redis/bloom/dist/commands/top-k/RESERVE");
319
+ };
320
+ }, Record<string, never>, Record<string, never>>;
321
+ private getLockKey;
322
+ acquireLock(lock_key: string, lock_value: string, expireSeconds?: number): Promise<boolean>;
323
+ releaseLock(lock_key: string, lock_value: string): Promise<boolean>;
324
+ withLock<T>(lock_identifier: string, callback: () => Promise<T>, release_lock_delay_ms?: number): Promise<T>;
325
+ subscribe(channel: string, listener: any): Promise<void>;
326
+ publish(channel: string, message: string): Promise<void>;
327
+ }
328
+ export declare const RedisCommand: typeof RedisClient;
@@ -0,0 +1,285 @@
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.RedisCommand = exports.RedisClient = void 0;
13
+ const index_1 = require("../index");
14
+ const redis_connection_1 = require("./redis_connection");
15
+ const MAX_SUBSCRIBE_RECONNECT = 10;
16
+ class RedisClient {
17
+ constructor(lockPrefix = '') {
18
+ this.lockMaxRetries = 10;
19
+ this.lockRetryDelayMs = 300;
20
+ this.lockExpireSeconds = 3;
21
+ this.lockPrefix = lockPrefix;
22
+ }
23
+ init() {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ this.redis_client = yield (0, redis_connection_1.getRedisConnection)('RedisClient');
26
+ (0, index_1.log_info)('[RedisClient] init done');
27
+ });
28
+ }
29
+ ensureClient() {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ if (!this.redis_client) {
32
+ this.redis_client = yield (0, redis_connection_1.getRedisConnection)('RedisClient');
33
+ }
34
+ return this.redis_client;
35
+ });
36
+ }
37
+ exists(key) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ return yield this.redis_client.exists(key);
40
+ });
41
+ }
42
+ ttl(key) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ return yield this.redis_client.ttl(key);
45
+ });
46
+ }
47
+ expire(key, ttl) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return yield this.redis_client.expire(key, ttl);
50
+ });
51
+ }
52
+ del(key_1) {
53
+ return __awaiter(this, arguments, void 0, function* (key, field = '') {
54
+ if (field) {
55
+ return yield this.redis_client.hDel(key, field);
56
+ }
57
+ return yield this.redis_client.del(key);
58
+ });
59
+ }
60
+ set_nx(key, value) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ return yield this.redis_client.setNX(key, value);
63
+ });
64
+ }
65
+ get(key) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const client = yield this.ensureClient();
68
+ return yield client.get(key);
69
+ });
70
+ }
71
+ set(key, value, expireSeconds) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const client = yield this.ensureClient();
74
+ if (expireSeconds && expireSeconds > 0) {
75
+ return yield client.set(key, value, { EX: expireSeconds });
76
+ }
77
+ return yield client.set(key, value);
78
+ });
79
+ }
80
+ hset(key, field, value, expireSeconds) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const client = yield this.ensureClient();
83
+ const res = yield client.hSet(key, field, value);
84
+ if (expireSeconds && expireSeconds > 0) {
85
+ try {
86
+ yield client.hExpire(key, field, expireSeconds);
87
+ }
88
+ catch (_) {
89
+ }
90
+ }
91
+ return res;
92
+ });
93
+ }
94
+ hget(key, field) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ const client = yield this.ensureClient();
97
+ return yield client.hGet(key, field);
98
+ });
99
+ }
100
+ hgetall(key) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ const client = yield this.ensureClient();
103
+ return yield client.hGetAll(key);
104
+ });
105
+ }
106
+ hmget(key, fields) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const client = yield this.ensureClient();
109
+ return yield client.hmGet(key, fields);
110
+ });
111
+ }
112
+ hkeys(key) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const client = yield this.ensureClient();
115
+ return yield client.hKeys(key);
116
+ });
117
+ }
118
+ hdel(key, field) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const client = yield this.ensureClient();
121
+ return yield client.hDel(key, field);
122
+ });
123
+ }
124
+ hlen(key) {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ const client = yield this.ensureClient();
127
+ return yield client.hLen(key);
128
+ });
129
+ }
130
+ hinc(key_1, field_1) {
131
+ return __awaiter(this, arguments, void 0, function* (key, field, ttl = 3600) {
132
+ let res = [];
133
+ let res1 = yield this.redis_client.hIncrBy(key, field, 1);
134
+ res.push(res1);
135
+ if (ttl > 0) {
136
+ let res2 = yield this.redis_client.hExpire(key, field, ttl);
137
+ res.push(res2);
138
+ }
139
+ return res;
140
+ });
141
+ }
142
+ lrange(key_1) {
143
+ return __awaiter(this, arguments, void 0, function* (key, start = 0, stop = -1) {
144
+ const client = yield this.ensureClient();
145
+ return yield client.lRange(key, start, stop);
146
+ });
147
+ }
148
+ lpush(key, value) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ const client = yield this.ensureClient();
151
+ return yield client.lPush(key, value);
152
+ });
153
+ }
154
+ ltrim(key, start, stop) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const client = yield this.ensureClient();
157
+ return yield client.lTrim(key, start, stop);
158
+ });
159
+ }
160
+ ping() {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ const client = yield this.ensureClient();
163
+ return yield client.ping();
164
+ });
165
+ }
166
+ type(key) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const client = yield this.ensureClient();
169
+ return yield client.type(key);
170
+ });
171
+ }
172
+ multi() {
173
+ return this.redis_client.multi();
174
+ }
175
+ getLockKey(lock_identifier) {
176
+ return `${this.lockPrefix}:lock:${lock_identifier}`;
177
+ }
178
+ acquireLock(lock_key_1, lock_value_1) {
179
+ return __awaiter(this, arguments, void 0, function* (lock_key, lock_value, expireSeconds = this.lockExpireSeconds) {
180
+ const client = yield this.ensureClient();
181
+ const result = yield client.set(lock_key, lock_value, {
182
+ NX: true,
183
+ EX: expireSeconds,
184
+ });
185
+ (0, index_1.log_info)(`[Lock] try acquire: key=${lock_key}, value=${lock_value}, expire=${expireSeconds}s, result=${result}`);
186
+ return result === 'OK';
187
+ });
188
+ }
189
+ releaseLock(lock_key, lock_value) {
190
+ return __awaiter(this, void 0, void 0, function* () {
191
+ const client = yield this.ensureClient();
192
+ const script = `
193
+ if redis.call('get', KEYS[1]) == ARGV[1] then
194
+ return redis.call('del', KEYS[1])
195
+ else
196
+ return 0
197
+ end
198
+ `;
199
+ const result = yield client.eval(script, {
200
+ keys: [lock_key],
201
+ arguments: [lock_value],
202
+ });
203
+ return Number(result) === 1;
204
+ });
205
+ }
206
+ withLock(lock_identifier_1, callback_1) {
207
+ return __awaiter(this, arguments, void 0, function* (lock_identifier, callback, release_lock_delay_ms = 100) {
208
+ const lock_key = this.getLockKey(lock_identifier);
209
+ const lock_value = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
210
+ let retries = 0;
211
+ let acquired = false;
212
+ const firstTryTime = Date.now();
213
+ let getLockTime = 0;
214
+ try {
215
+ while (retries < this.lockMaxRetries) {
216
+ acquired = yield this.acquireLock(lock_key, lock_value);
217
+ if (acquired)
218
+ break;
219
+ yield new Promise(resolve => setTimeout(resolve, this.lockRetryDelayMs));
220
+ retries++;
221
+ }
222
+ if (acquired) {
223
+ getLockTime = Date.now();
224
+ return yield callback();
225
+ }
226
+ }
227
+ finally {
228
+ if (acquired) {
229
+ const releaseDelay = parseInt(process.env.NONCE_LOCK_RELEASE_DELAY_MS || String(release_lock_delay_ms));
230
+ if (releaseDelay > 0) {
231
+ yield (0, index_1.sleep)(releaseDelay);
232
+ }
233
+ this.releaseLock(lock_key, lock_value);
234
+ (0, index_1.log_info)(`[Lock] withLock ok: key=${lock_key}, retries=${retries}, wait=${getLockTime - firstTryTime}ms, hold=${Date.now() - getLockTime}ms`);
235
+ }
236
+ else {
237
+ (0, index_1.log_warn)(`[Lock] withLock failed: key=${lock_key}, retries=${retries}, took=${Date.now() - firstTryTime}ms`);
238
+ throw new Error(`get lock for ${lock_key} failed`);
239
+ }
240
+ }
241
+ });
242
+ }
243
+ subscribe(channel, listener) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ let reconnectCount = 0;
246
+ const doSubscribe = () => __awaiter(this, void 0, void 0, function* () {
247
+ try {
248
+ const subscriber = this.redis_client.duplicate();
249
+ subscriber.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
250
+ (0, index_1.log_error)(`[PubSub] channel=${channel} error`, err);
251
+ if (reconnectCount >= MAX_SUBSCRIBE_RECONNECT) {
252
+ (0, index_1.log_warn)(`[PubSub] channel=${channel} exceeded max reconnect (${MAX_SUBSCRIBE_RECONNECT}), giving up`);
253
+ return;
254
+ }
255
+ reconnectCount++;
256
+ const delay = Math.min(1000 * reconnectCount, 10000);
257
+ (0, index_1.log_info)(`[PubSub] channel=${channel} reconnect #${reconnectCount} in ${delay}ms`);
258
+ yield (0, index_1.sleep)(delay);
259
+ doSubscribe();
260
+ }));
261
+ yield subscriber.connect();
262
+ yield subscriber.subscribe(channel, listener);
263
+ reconnectCount = 0;
264
+ (0, index_1.log_info)(`[PubSub] subscribed to ${channel}`);
265
+ }
266
+ catch (err) {
267
+ (0, index_1.log_error)(`[PubSub] subscribe to ${channel} failed`, err);
268
+ if (reconnectCount < MAX_SUBSCRIBE_RECONNECT) {
269
+ reconnectCount++;
270
+ yield (0, index_1.sleep)(3000);
271
+ doSubscribe();
272
+ }
273
+ }
274
+ });
275
+ yield doSubscribe();
276
+ });
277
+ }
278
+ publish(channel, message) {
279
+ return __awaiter(this, void 0, void 0, function* () {
280
+ this.redis_client.publish(channel, message);
281
+ });
282
+ }
283
+ }
284
+ exports.RedisClient = RedisClient;
285
+ exports.RedisCommand = RedisClient;
@@ -0,0 +1,3 @@
1
+ import type { RedisClientType } from 'redis';
2
+ declare function getRedisConnection(module_name?: string): Promise<RedisClientType>;
3
+ export { getRedisConnection, };
@@ -0,0 +1,45 @@
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.getRedisConnection = getRedisConnection;
13
+ const redis_1 = require("redis");
14
+ let redisClient = null;
15
+ let connectPromise = null;
16
+ function createConnection() {
17
+ const host = process.env.REDIS_HOST || '127.0.0.1';
18
+ const port = process.env.REDIS_PORT || '6379';
19
+ const url = `redis://${host}:${port}`;
20
+ console.log(`[Redis] connecting to ${url}`);
21
+ const client = (0, redis_1.createClient)({ url });
22
+ client.on('error', (err) => console.error(`[Redis] error: ${err.message}`));
23
+ client.on('reconnecting', () => console.info('[Redis] reconnecting...'));
24
+ client.on('ready', () => console.info('[Redis] ready'));
25
+ return client.connect().then(() => {
26
+ redisClient = client;
27
+ console.info(`[Redis] connected to ${url}`);
28
+ return client;
29
+ }).catch((err) => {
30
+ redisClient = null;
31
+ connectPromise = null;
32
+ console.error(`[Redis] connect failed: ${err.message}`);
33
+ throw err;
34
+ });
35
+ }
36
+ function getRedisConnection() {
37
+ return __awaiter(this, arguments, void 0, function* (module_name = '') {
38
+ if (redisClient)
39
+ return redisClient;
40
+ if (connectPromise)
41
+ return connectPromise;
42
+ connectPromise = createConnection();
43
+ return connectPromise;
44
+ });
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-core",
3
- "version": "3.1.8",
3
+ "version": "3.1.10",
4
4
  "description": "Common types and utilities for trading systems - use `npm run push` to publish",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",