@wildix/xbees-conversations-utils 1.1.61 → 1.1.63
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-cjs/rateLimits/helpers/RedisCooldown.js +4 -4
- package/dist-cjs/rateLimits/helpers/executeWithRateLimitHandling.js +15 -10
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitException.js +2 -2
- package/dist-es/rateLimits/helpers/RedisCooldown.js +4 -4
- package/dist-es/rateLimits/helpers/executeWithRateLimitHandling.js +15 -10
- package/dist-es/rateLimits/helpers/processStreamRateLimitException.js +2 -2
- package/dist-types/rateLimits/helpers/RedisCooldown.d.ts +3 -3
- package/dist-types/rateLimits/helpers/processStreamRateLimitException.d.ts +2 -1
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@ exports.RedisCooldown = void 0;
|
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
5
|
class RedisCooldown {
|
|
6
6
|
static async getRemainingMs(cooldownKey, context) {
|
|
7
|
-
const { redis } = context;
|
|
7
|
+
const { redis, logger } = context;
|
|
8
8
|
try {
|
|
9
9
|
const ttlMs = await redis.pttl(`${constants_1.STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`);
|
|
10
10
|
if (ttlMs > 0) {
|
|
@@ -12,7 +12,7 @@ class RedisCooldown {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
catch (error) {
|
|
15
|
-
|
|
15
|
+
logger.warn('Failed to read stream rate-limit cooldown from Redis', {
|
|
16
16
|
cooldownKey,
|
|
17
17
|
error,
|
|
18
18
|
});
|
|
@@ -23,7 +23,7 @@ class RedisCooldown {
|
|
|
23
23
|
if (cooldownMs <= 0) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
-
const { redis } = context;
|
|
26
|
+
const { redis, logger } = context;
|
|
27
27
|
const redisKey = `${constants_1.STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`;
|
|
28
28
|
try {
|
|
29
29
|
await redis.eval(`
|
|
@@ -45,7 +45,7 @@ class RedisCooldown {
|
|
|
45
45
|
`, 1, redisKey, cooldownMs.toString());
|
|
46
46
|
}
|
|
47
47
|
catch (error) {
|
|
48
|
-
|
|
48
|
+
logger.warn('Failed to write stream rate-limit cooldown to Redis', {
|
|
49
49
|
cooldownKey,
|
|
50
50
|
cooldownMs,
|
|
51
51
|
error,
|
|
@@ -7,21 +7,26 @@ const createRateLimitExceededException_1 = require("./createRateLimitExceededExc
|
|
|
7
7
|
const processStreamRateLimitException_1 = require("./processStreamRateLimitException");
|
|
8
8
|
const RedisCooldown_1 = require("./RedisCooldown");
|
|
9
9
|
function calculateRateLimitDelayMs(error, maxDelayMs) {
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
|
|
11
|
+
const baseDelayMs = hasServerRetryAfter ? error.retryAfter + constants_1.RETRY_AFTER_BUFFER_MS : constants_1.DEFAULT_RETRY_AFTER_MS;
|
|
12
|
+
if (baseDelayMs > 0) {
|
|
12
13
|
const jitterMs = Math.floor(Math.random() * constants_1.DEFAULT_JITTER_MS);
|
|
13
|
-
|
|
14
|
+
const computedDelayMs = baseDelayMs + jitterMs;
|
|
15
|
+
if (hasServerRetryAfter) {
|
|
16
|
+
return computedDelayMs;
|
|
17
|
+
}
|
|
18
|
+
return Math.min(maxDelayMs, computedDelayMs);
|
|
14
19
|
}
|
|
15
20
|
return Math.min(maxDelayMs, constants_1.DEFAULT_FIXED_RATE_LIMIT_DELAY_MS);
|
|
16
21
|
}
|
|
17
22
|
async function executeWithRateLimitHandling(execute, operation, context, options) {
|
|
18
|
-
const { redis } = context;
|
|
23
|
+
const { logger, redis } = context;
|
|
19
24
|
const { enableCooldown, maxAttempts, maxDelayMs, maxRetryableDelayMs } = options;
|
|
20
25
|
let attempt = 1;
|
|
21
26
|
if (enableCooldown) {
|
|
22
|
-
const cooldownMs = await RedisCooldown_1.RedisCooldown.getRemainingMs(operation, { redis });
|
|
27
|
+
const cooldownMs = await RedisCooldown_1.RedisCooldown.getRemainingMs(operation, { redis, logger });
|
|
23
28
|
if (cooldownMs > 0) {
|
|
24
|
-
|
|
29
|
+
logger.warn('Skipping stream request due to active rate-limit cooldown', {
|
|
25
30
|
operation,
|
|
26
31
|
cooldown: cooldownMs,
|
|
27
32
|
});
|
|
@@ -33,16 +38,16 @@ async function executeWithRateLimitHandling(execute, operation, context, options
|
|
|
33
38
|
return await execute();
|
|
34
39
|
}
|
|
35
40
|
catch (error) {
|
|
36
|
-
const rateLimitError = (0, processStreamRateLimitException_1.processStreamRateLimitException)(error);
|
|
41
|
+
const rateLimitError = (0, processStreamRateLimitException_1.processStreamRateLimitException)(error, logger);
|
|
37
42
|
if (!rateLimitError) {
|
|
38
43
|
throw error;
|
|
39
44
|
}
|
|
40
45
|
const retryAfterMs = calculateRateLimitDelayMs(rateLimitError, maxDelayMs);
|
|
41
46
|
if (enableCooldown) {
|
|
42
|
-
await RedisCooldown_1.RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis });
|
|
47
|
+
await RedisCooldown_1.RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis, logger });
|
|
43
48
|
}
|
|
44
49
|
if (attempt < maxAttempts && retryAfterMs <= maxRetryableDelayMs) {
|
|
45
|
-
|
|
50
|
+
logger.warn('Retrying stream request after rate limit', {
|
|
46
51
|
operation,
|
|
47
52
|
attempt,
|
|
48
53
|
maxAttempts,
|
|
@@ -55,7 +60,7 @@ async function executeWithRateLimitHandling(execute, operation, context, options
|
|
|
55
60
|
attempt++;
|
|
56
61
|
continue;
|
|
57
62
|
}
|
|
58
|
-
|
|
63
|
+
logger.warn('Propagating stream rate limit w/o retry', {
|
|
59
64
|
operation,
|
|
60
65
|
attempt,
|
|
61
66
|
maxAttempts,
|
|
@@ -53,13 +53,13 @@ function isStreamHttpRateLimitError(error) {
|
|
|
53
53
|
error.response !== null &&
|
|
54
54
|
error.response?.status === 429)));
|
|
55
55
|
}
|
|
56
|
-
function processStreamRateLimitException(error) {
|
|
56
|
+
function processStreamRateLimitException(error, logger) {
|
|
57
57
|
if (isStreamHttpRateLimitError(error)) {
|
|
58
58
|
const headers = error.response?.headers ?? {};
|
|
59
59
|
const rateLimit = getRateLimitFromHeaders(headers);
|
|
60
60
|
const retryAfterMs = parseRetryAfterMs(headers);
|
|
61
61
|
const message = error.response?.data?.message || error.message || 'Rate limit exceeded';
|
|
62
|
-
|
|
62
|
+
logger.warn('Stream rate limit exceeded [HTTP 429]', {
|
|
63
63
|
status: error.status ?? error.response?.status,
|
|
64
64
|
retryAfterMs,
|
|
65
65
|
rateLimit,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX } from '../constants';
|
|
2
2
|
export class RedisCooldown {
|
|
3
3
|
static async getRemainingMs(cooldownKey, context) {
|
|
4
|
-
const { redis } = context;
|
|
4
|
+
const { redis, logger } = context;
|
|
5
5
|
try {
|
|
6
6
|
const ttlMs = await redis.pttl(`${STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`);
|
|
7
7
|
if (ttlMs > 0) {
|
|
@@ -9,7 +9,7 @@ export class RedisCooldown {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
catch (error) {
|
|
12
|
-
|
|
12
|
+
logger.warn('Failed to read stream rate-limit cooldown from Redis', {
|
|
13
13
|
cooldownKey,
|
|
14
14
|
error,
|
|
15
15
|
});
|
|
@@ -20,7 +20,7 @@ export class RedisCooldown {
|
|
|
20
20
|
if (cooldownMs <= 0) {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
const { redis } = context;
|
|
23
|
+
const { redis, logger } = context;
|
|
24
24
|
const redisKey = `${STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`;
|
|
25
25
|
try {
|
|
26
26
|
await redis.eval(`
|
|
@@ -42,7 +42,7 @@ export class RedisCooldown {
|
|
|
42
42
|
`, 1, redisKey, cooldownMs.toString());
|
|
43
43
|
}
|
|
44
44
|
catch (error) {
|
|
45
|
-
|
|
45
|
+
logger.warn('Failed to write stream rate-limit cooldown to Redis', {
|
|
46
46
|
cooldownKey,
|
|
47
47
|
cooldownMs,
|
|
48
48
|
error,
|
|
@@ -4,21 +4,26 @@ import { createRateLimitExceededException } from './createRateLimitExceededExcep
|
|
|
4
4
|
import { processStreamRateLimitException } from './processStreamRateLimitException';
|
|
5
5
|
import { RedisCooldown } from './RedisCooldown';
|
|
6
6
|
function calculateRateLimitDelayMs(error, maxDelayMs) {
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
|
|
8
|
+
const baseDelayMs = hasServerRetryAfter ? error.retryAfter + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
|
|
9
|
+
if (baseDelayMs > 0) {
|
|
9
10
|
const jitterMs = Math.floor(Math.random() * DEFAULT_JITTER_MS);
|
|
10
|
-
|
|
11
|
+
const computedDelayMs = baseDelayMs + jitterMs;
|
|
12
|
+
if (hasServerRetryAfter) {
|
|
13
|
+
return computedDelayMs;
|
|
14
|
+
}
|
|
15
|
+
return Math.min(maxDelayMs, computedDelayMs);
|
|
11
16
|
}
|
|
12
17
|
return Math.min(maxDelayMs, DEFAULT_FIXED_RATE_LIMIT_DELAY_MS);
|
|
13
18
|
}
|
|
14
19
|
export async function executeWithRateLimitHandling(execute, operation, context, options) {
|
|
15
|
-
const { redis } = context;
|
|
20
|
+
const { logger, redis } = context;
|
|
16
21
|
const { enableCooldown, maxAttempts, maxDelayMs, maxRetryableDelayMs } = options;
|
|
17
22
|
let attempt = 1;
|
|
18
23
|
if (enableCooldown) {
|
|
19
|
-
const cooldownMs = await RedisCooldown.getRemainingMs(operation, { redis });
|
|
24
|
+
const cooldownMs = await RedisCooldown.getRemainingMs(operation, { redis, logger });
|
|
20
25
|
if (cooldownMs > 0) {
|
|
21
|
-
|
|
26
|
+
logger.warn('Skipping stream request due to active rate-limit cooldown', {
|
|
22
27
|
operation,
|
|
23
28
|
cooldown: cooldownMs,
|
|
24
29
|
});
|
|
@@ -30,16 +35,16 @@ export async function executeWithRateLimitHandling(execute, operation, context,
|
|
|
30
35
|
return await execute();
|
|
31
36
|
}
|
|
32
37
|
catch (error) {
|
|
33
|
-
const rateLimitError = processStreamRateLimitException(error);
|
|
38
|
+
const rateLimitError = processStreamRateLimitException(error, logger);
|
|
34
39
|
if (!rateLimitError) {
|
|
35
40
|
throw error;
|
|
36
41
|
}
|
|
37
42
|
const retryAfterMs = calculateRateLimitDelayMs(rateLimitError, maxDelayMs);
|
|
38
43
|
if (enableCooldown) {
|
|
39
|
-
await RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis });
|
|
44
|
+
await RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis, logger });
|
|
40
45
|
}
|
|
41
46
|
if (attempt < maxAttempts && retryAfterMs <= maxRetryableDelayMs) {
|
|
42
|
-
|
|
47
|
+
logger.warn('Retrying stream request after rate limit', {
|
|
43
48
|
operation,
|
|
44
49
|
attempt,
|
|
45
50
|
maxAttempts,
|
|
@@ -52,7 +57,7 @@ export async function executeWithRateLimitHandling(execute, operation, context,
|
|
|
52
57
|
attempt++;
|
|
53
58
|
continue;
|
|
54
59
|
}
|
|
55
|
-
|
|
60
|
+
logger.warn('Propagating stream rate limit w/o retry', {
|
|
56
61
|
operation,
|
|
57
62
|
attempt,
|
|
58
63
|
maxAttempts,
|
|
@@ -50,13 +50,13 @@ function isStreamHttpRateLimitError(error) {
|
|
|
50
50
|
error.response !== null &&
|
|
51
51
|
error.response?.status === 429)));
|
|
52
52
|
}
|
|
53
|
-
export function processStreamRateLimitException(error) {
|
|
53
|
+
export function processStreamRateLimitException(error, logger) {
|
|
54
54
|
if (isStreamHttpRateLimitError(error)) {
|
|
55
55
|
const headers = error.response?.headers ?? {};
|
|
56
56
|
const rateLimit = getRateLimitFromHeaders(headers);
|
|
57
57
|
const retryAfterMs = parseRetryAfterMs(headers);
|
|
58
58
|
const message = error.response?.data?.message || error.message || 'Rate limit exceeded';
|
|
59
|
-
|
|
59
|
+
logger.warn('Stream rate limit exceeded [HTTP 429]', {
|
|
60
60
|
status: error.status ?? error.response?.status,
|
|
61
61
|
retryAfterMs,
|
|
62
62
|
rateLimit,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RedisContext } from '../types';
|
|
1
|
+
import { LoggerContext, RedisContext } from '../types';
|
|
2
2
|
export declare class RedisCooldown {
|
|
3
|
-
static getRemainingMs(cooldownKey: string, context: RedisContext): Promise<number>;
|
|
4
|
-
static setRemainingMs(cooldownKey: string, cooldownMs: number, context: RedisContext): Promise<void>;
|
|
3
|
+
static getRemainingMs(cooldownKey: string, context: RedisContext & LoggerContext): Promise<number>;
|
|
4
|
+
static setRemainingMs(cooldownKey: string, cooldownMs: number, context: RedisContext & LoggerContext): Promise<void>;
|
|
5
5
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
|
|
2
|
-
|
|
2
|
+
import { Logger } from '@aws-lambda-powertools/logger';
|
|
3
|
+
export declare function processStreamRateLimitException(error: unknown, logger: Logger): RateLimitExceededException | undefined;
|