@lido-nestjs/execution 1.20.0 → 1.22.0

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,7 @@ export interface SimpleFallbackProviderConfig {
11
11
  minBackoffMs?: number;
12
12
  maxBackoffMs?: number;
13
13
  logRetries?: boolean;
14
+ logSuccessfulAttempts?: boolean;
14
15
  resetIntervalMs?: number;
15
16
  fetchMiddlewares?: MiddlewareCallback<Promise<any>>[];
16
17
  maxTimeWithoutNewBlocksMs?: number;
@@ -75,7 +75,8 @@ export declare class ExtendedJsonRpcBatchProvider extends JsonRpcProvider {
75
75
  protected _fetchMiddlewareService: MiddlewareService<Promise<any>>;
76
76
  protected _domain: string;
77
77
  protected _eventEmitter: ExtendedJsonRpcBatchProviderEventEmitter;
78
- constructor(url: ConnectionInfo | string, network?: Networkish, requestPolicy?: RequestPolicy, fetchMiddlewares?: MiddlewareCallback<Promise<any>>[]);
78
+ protected _requestTimeoutMs?: number;
79
+ constructor(url: ConnectionInfo | string, network?: Networkish, requestPolicy?: RequestPolicy, fetchMiddlewares?: MiddlewareCallback<Promise<any>>[], requestTimeoutMs?: number);
79
80
  static _formatter: Formatter | null;
80
81
  static getFormatter(): Formatter;
81
82
  protected _batchAggregatorTick(): void;
@@ -15,14 +15,16 @@ var errorCodes = require('../error/codes/error-codes.js');
15
15
  var debugTraceBlockByHash = require('../ethers/debug-trace-block-by-hash.js');
16
16
  var networks = require('../common/networks.js');
17
17
  var sanitizeError = require('../common/sanitize-error.js');
18
+ var requestTimeout_error = require('../error/request-timeout.error.js');
18
19
  var lazyEventEmitter = require('../common/lazy-event-emitter.js');
19
20
 
20
21
  exports.ExtendedJsonRpcBatchProvider = class ExtendedJsonRpcBatchProvider extends providers.JsonRpcProvider {
21
- constructor(url, network, requestPolicy, fetchMiddlewares = []) {
22
+ constructor(url, network, requestPolicy, fetchMiddlewares = [], requestTimeoutMs) {
22
23
  super(url, network);
23
24
  this._batchAggregator = null;
24
25
  this._queue = new queue.Queue();
25
26
  this._tickCounter = 0;
27
+ this._requestTimeoutMs = requestTimeoutMs;
26
28
  this._eventEmitter = new lazyEventEmitter.LazyEventEmitter();
27
29
  this._domain = networks.getConnectionFQDN(url);
28
30
  this._requestPolicy = requestPolicy !== null && requestPolicy !== void 0 ? requestPolicy : {
@@ -189,12 +191,22 @@ exports.ExtendedJsonRpcBatchProvider = class ExtendedJsonRpcBatchProvider extend
189
191
  };
190
192
  const currentRequest = {
191
193
  request,
192
- reject: null,
193
194
  resolve: null,
195
+ reject: null,
194
196
  };
197
+ let timerId;
195
198
  const promise = new Promise((resolve, reject) => {
196
199
  currentRequest.resolve = resolve;
197
200
  currentRequest.reject = reject;
201
+ if (this._requestTimeoutMs) {
202
+ const timeoutMs = this._requestTimeoutMs;
203
+ timerId = setTimeout(() => {
204
+ reject(new requestTimeout_error.RequestTimeoutError(`Request timeout after ${timeoutMs}ms`, timeoutMs));
205
+ }, timeoutMs);
206
+ }
207
+ }).finally(() => {
208
+ if (timerId)
209
+ clearTimeout(timerId);
198
210
  });
199
211
  this._queue.enqueue(currentRequest);
200
212
  this._startBatchAggregator();
@@ -220,5 +232,5 @@ exports.ExtendedJsonRpcBatchProvider = class ExtendedJsonRpcBatchProvider extend
220
232
  exports.ExtendedJsonRpcBatchProvider._formatter = null;
221
233
  exports.ExtendedJsonRpcBatchProvider = tslib.__decorate([
222
234
  common.Injectable(),
223
- tslib.__metadata("design:paramtypes", [Object, Object, Object, Array])
235
+ tslib.__metadata("design:paramtypes", [Object, Object, Object, Array, Number])
224
236
  ], exports.ExtendedJsonRpcBatchProvider);
@@ -55,7 +55,6 @@ export declare class SimpleFallbackJsonRpcBatchProvider extends BaseProvider {
55
55
  protected get provider(): FallbackProvider;
56
56
  protected switchToNextProvider(): void;
57
57
  protected isNonRetryableError(error: Error | unknown): boolean;
58
- protected withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T>;
59
58
  perform(method: string, params: {
60
59
  [name: string]: unknown;
61
60
  }): Promise<unknown>;
@@ -30,7 +30,7 @@ exports.SimpleFallbackJsonRpcBatchProvider = class SimpleFallbackJsonRpcBatchPro
30
30
  this._childRpcListenersAttached = false;
31
31
  this._eventEmitter = new lazyEventEmitter.LazyEventEmitter();
32
32
  this._setupLazyChildListeners();
33
- this.config = Object.assign({ maxRetries: 3, minBackoffMs: 500, maxBackoffMs: 5000, logRetries: true, resetIntervalMs: 10000, maxTimeWithoutNewBlocksMs: 60000 }, config);
33
+ this.config = Object.assign({ maxRetries: 3, minBackoffMs: 500, maxBackoffMs: 5000, logRetries: true, logSuccessfulAttempts: true, resetIntervalMs: 10000, maxTimeWithoutNewBlocksMs: 60000 }, config);
34
34
  this.logger = logger;
35
35
  const conns = config.urls.filter((url) => {
36
36
  if (!url) {
@@ -46,7 +46,7 @@ exports.SimpleFallbackJsonRpcBatchProvider = class SimpleFallbackJsonRpcBatchPro
46
46
  }
47
47
  this.fallbackProviders = conns.map((conn, index) => {
48
48
  var _a;
49
- const provider = new extendedJsonRpcBatchProvider.ExtendedJsonRpcBatchProvider(conn, undefined, config.requestPolicy, (_a = config.fetchMiddlewares) !== null && _a !== void 0 ? _a : []);
49
+ const provider = new extendedJsonRpcBatchProvider.ExtendedJsonRpcBatchProvider(conn, undefined, config.requestPolicy, (_a = config.fetchMiddlewares) !== null && _a !== void 0 ? _a : [], config.requestTimeoutMs);
50
50
  return {
51
51
  network: null,
52
52
  provider,
@@ -152,16 +152,6 @@ exports.SimpleFallbackJsonRpcBatchProvider = class SimpleFallbackJsonRpcBatchPro
152
152
  errors.isErrorHasCode(error) &&
153
153
  errors.nonRetryableErrors.includes(error.code));
154
154
  }
155
- withTimeout(promise, timeoutMs) {
156
- return Promise.race([
157
- promise,
158
- new Promise((_, reject) => {
159
- setTimeout(() => {
160
- reject(new requestTimeout_error.RequestTimeoutError(`Request timeout after ${timeoutMs}ms`, timeoutMs));
161
- }, timeoutMs);
162
- }),
163
- ]);
164
- }
165
155
  async perform(method, params) {
166
156
  var _a, _b;
167
157
  const retry = retrier.retrier(this.logger, this.config.maxRetries, this.config.minBackoffMs, this.config.maxBackoffMs, this.config.logRetries, (e) => this.isNonRetryableError(e));
@@ -175,7 +165,9 @@ exports.SimpleFallbackJsonRpcBatchProvider = class SimpleFallbackJsonRpcBatchPro
175
165
  let performRetryAttempt = 0;
176
166
  attempt++;
177
167
  // Log which provider we're attempting to use
178
- this.logger.log(this.formatLog(`Attempting ${method} (attempt ${attempt}/${this.fallbackProviders.length})`, this.activeFallbackProviderIndex));
168
+ if (this.config.logSuccessfulAttempts) {
169
+ this.logger.log(this.formatLog(`Attempting ${method} (attempt ${attempt}/${this.fallbackProviders.length})`, this.activeFallbackProviderIndex));
170
+ }
179
171
  // awaiting is extremely important here
180
172
  // without it, the error will not be caught in current try-catch scope
181
173
  const result = await retry(() => {
@@ -189,15 +181,12 @@ exports.SimpleFallbackJsonRpcBatchProvider = class SimpleFallbackJsonRpcBatchPro
189
181
  retryAttempt: performRetryAttempt,
190
182
  }));
191
183
  performRetryAttempt++;
192
- const performPromise = provider.provider.perform(method, params);
193
- // Apply timeout if configured
194
- if (this.config.requestTimeoutMs) {
195
- return this.withTimeout(performPromise, this.config.requestTimeoutMs);
196
- }
197
- return performPromise;
184
+ return provider.provider.perform(method, params);
198
185
  });
199
186
  // Log successful request
200
- this.logger.log(this.formatLog(`${method} successful after ${performRetryAttempt} retry attempt(s)`, this.activeFallbackProviderIndex));
187
+ if (this.config.logSuccessfulAttempts) {
188
+ this.logger.log(this.formatLog(`${method} successful after ${performRetryAttempt} retry attempt(s)`, this.activeFallbackProviderIndex));
189
+ }
201
190
  return result;
202
191
  }
203
192
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lido-nestjs/execution",
3
- "version": "1.20.0",
3
+ "version": "1.22.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "MIT",