socket-function 1.1.43 → 1.1.44

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/index.d.ts CHANGED
@@ -630,6 +630,8 @@ declare module "socket-function/src/batching" {
630
630
  shouldRetry?: (message: string) => boolean;
631
631
  minDelay?: number;
632
632
  maxDelay?: number;
633
+ timeout?: number;
634
+ warningInterval?: number;
633
635
  }): T;
634
636
  /** @deprecated Use safeLoop instead */
635
637
  export declare const throttledLoop: typeof unblockLoop;
@@ -1131,6 +1133,9 @@ declare module "socket-function/src/misc" {
1131
1133
  export declare function timeoutToUndefined<T>(time: number, p: Promise<T>): Promise<T | undefined>;
1132
1134
  export declare function timeoutToUndefinedSilent<T>(time: number, p: Promise<T>): Promise<T | undefined>;
1133
1135
  export declare function errorToWarning<T>(promise: Promise<T>): void;
1136
+ export declare function watchSlowPromise<T>(title: string, promise: Promise<T>, config: {
1137
+ interval?: number;
1138
+ }): Promise<T>;
1134
1139
 
1135
1140
  }
1136
1141
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.1.43",
3
+ "version": "1.1.44",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
package/src/batching.d.ts CHANGED
@@ -32,6 +32,8 @@ export declare function retryFunctional<T extends AnyFunction>(fnc: T, config?:
32
32
  shouldRetry?: (message: string) => boolean;
33
33
  minDelay?: number;
34
34
  maxDelay?: number;
35
+ timeout?: number;
36
+ warningInterval?: number;
35
37
  }): T;
36
38
  /** @deprecated Use safeLoop instead */
37
39
  export declare const throttledLoop: typeof unblockLoop;
package/src/batching.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { formatTime } from "./formatting/format";
2
2
  import { red } from "./formatting/logColors";
3
- import { PromiseObj, isNode, timeoutToError } from "./misc";
3
+ import { PromiseObj, isNode, timeoutToError, watchSlowPromise } from "./misc";
4
4
  import { measureWrap } from "./profiling/measure";
5
5
  import { AnyFunction, Args, MaybePromise } from "./types";
6
6
 
@@ -312,18 +312,32 @@ export function retryFunctional<T extends AnyFunction>(fnc: T, config?: {
312
312
  shouldRetry?: (message: string) => boolean;
313
313
  minDelay?: number;
314
314
  maxDelay?: number;
315
+ timeout?: number;
316
+ warningInterval?: number;
315
317
  }): T {
316
- let { maxRetries = DEFAULT_MAX_RETRIES, shouldRetry, minDelay = DEFAULT_RETRY_DELAY, maxDelay = DEFAULT_RETRY_DELAY } = config || {};
318
+ let {
319
+ maxRetries = DEFAULT_MAX_RETRIES, shouldRetry, minDelay = DEFAULT_RETRY_DELAY, maxDelay = DEFAULT_RETRY_DELAY,
320
+ timeout,
321
+ warningInterval,
322
+ } = config || {};
317
323
  let expFactor = Math.max(1, Math.log(maxDelay / minDelay) / Math.log(Math.max(maxRetries, 2)));
324
+ let name = fnc.name || fnc.toString().slice(0, 100);
318
325
  async function runFnc(args: any[], retries: number): Promise<ReturnType<T>> {
319
326
  try {
320
- return await (fnc as any)(...args);
327
+ let promise = (fnc as any)(...args);
328
+ if (timeout !== undefined) {
329
+ promise = timeoutToError(timeout, promise, () => new Error(`timed out after ${formatTime(timeout)}`));
330
+ }
331
+ if (warningInterval !== undefined) {
332
+ promise = watchSlowPromise(name, promise, { interval: warningInterval });
333
+ }
334
+ return await promise;
321
335
  } catch (e: any) {
322
336
  if (shouldRetry && !shouldRetry(String(e.stack))) {
323
337
  throw e;
324
338
  }
325
339
  if (retries < 0) throw e;
326
- console.warn(`Retrying ${fnc.name}, due to error ${String(e.stack)}`);
340
+ console.warn(`Retrying ${name}, due to error ${String(e.stack)}`);
327
341
  retries--;
328
342
  let curCount = maxRetries - retries;
329
343
  await delay(minDelay * expFactor ** curCount);
package/src/misc.d.ts CHANGED
@@ -96,3 +96,6 @@ export declare function timeoutToError<T>(time: number, p: Promise<T>, err: () =
96
96
  export declare function timeoutToUndefined<T>(time: number, p: Promise<T>): Promise<T | undefined>;
97
97
  export declare function timeoutToUndefinedSilent<T>(time: number, p: Promise<T>): Promise<T | undefined>;
98
98
  export declare function errorToWarning<T>(promise: Promise<T>): void;
99
+ export declare function watchSlowPromise<T>(title: string, promise: Promise<T>, config: {
100
+ interval?: number;
101
+ }): Promise<T>;
package/src/misc.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { canHaveChildren, MaybePromise } from "./types";
2
- import { formatNumber } from "./formatting/format";
2
+ import { formatNumber, formatTime } from "./formatting/format";
3
+ import { delay } from "./batching";
4
+ import { yellow } from "./formatting/logColors";
3
5
 
4
6
  export const timeInSecond = 1000;
5
7
  export const timeInMinute = timeInSecond * 60;
@@ -458,3 +460,20 @@ export function errorToWarning<T>(promise: Promise<T>): void {
458
460
  return undefined as any;
459
461
  }) as any;
460
462
  }
463
+
464
+ export function watchSlowPromise<T>(title: string, promise: Promise<T>, config: {
465
+ interval?: number;
466
+ }) {
467
+ let { interval = 5000 } = config;
468
+ let fulfilled = false;
469
+ void promise.finally(() => fulfilled = true);
470
+ let startTime = Date.now();
471
+ void (async () => {
472
+ while (true) {
473
+ await delay(interval);
474
+ if (fulfilled) break;
475
+ console.log(`${yellow(`Slow promise running for ${formatTime(Date.now() - startTime)}`)} | ${title}`);
476
+ }
477
+ })();
478
+ return promise;
479
+ }