@mulingai-npm/redis 3.38.9 → 3.38.11
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.
|
@@ -9,7 +9,7 @@ import { RedisClient } from '../redis-client';
|
|
|
9
9
|
* - demo:pool:all (Set) All demo room IDs
|
|
10
10
|
* - demo:pool:available (Set) Currently available room IDs
|
|
11
11
|
* - demo:pool:room:{id}:session (Hash) Active session info per room
|
|
12
|
-
* - demo:ratelimit:
|
|
12
|
+
* - demo:ratelimit:ip:{hash} (Counter) Demos per IP per 24h
|
|
13
13
|
*/
|
|
14
14
|
export declare class DemoRoomPoolManager {
|
|
15
15
|
private redisClient;
|
|
@@ -31,7 +31,7 @@ export declare class DemoRoomPoolManager {
|
|
|
31
31
|
* Atomic: SPOP removes from available set in one operation.
|
|
32
32
|
* If no rooms available, attempts to reclaim rooms with expired sessions first.
|
|
33
33
|
*/
|
|
34
|
-
assignRoom(
|
|
34
|
+
assignRoom(identifierHash: string, demoSessionId: string, durationMs: number): Promise<{
|
|
35
35
|
roomId: number;
|
|
36
36
|
expiresAt: number;
|
|
37
37
|
} | null>;
|
|
@@ -56,17 +56,17 @@ export declare class DemoRoomPoolManager {
|
|
|
56
56
|
* Get session info for a room.
|
|
57
57
|
*/
|
|
58
58
|
getSession(roomId: number): Promise<{
|
|
59
|
-
|
|
59
|
+
identifierHash: string;
|
|
60
60
|
demoSessionId: string;
|
|
61
61
|
startedAt: number;
|
|
62
62
|
expiresAt: number;
|
|
63
63
|
} | null>;
|
|
64
64
|
/**
|
|
65
|
-
* Check
|
|
65
|
+
* Check IP rate limit. Returns true if under limit, false if exceeded.
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
checkIpRateLimit(ipHash: string, max?: number): Promise<boolean>;
|
|
68
68
|
/**
|
|
69
|
-
* Increment
|
|
69
|
+
* Increment IP rate limit counter with 24h TTL.
|
|
70
70
|
*/
|
|
71
|
-
|
|
71
|
+
incrementIpRateLimit(ipHash: string): Promise<void>;
|
|
72
72
|
}
|
|
@@ -11,7 +11,7 @@ exports.DemoRoomPoolManager = void 0;
|
|
|
11
11
|
* - demo:pool:all (Set) All demo room IDs
|
|
12
12
|
* - demo:pool:available (Set) Currently available room IDs
|
|
13
13
|
* - demo:pool:room:{id}:session (Hash) Active session info per room
|
|
14
|
-
* - demo:ratelimit:
|
|
14
|
+
* - demo:ratelimit:ip:{hash} (Counter) Demos per IP per 24h
|
|
15
15
|
*/
|
|
16
16
|
class DemoRoomPoolManager {
|
|
17
17
|
constructor(redisClient) {
|
|
@@ -70,7 +70,7 @@ class DemoRoomPoolManager {
|
|
|
70
70
|
* Atomic: SPOP removes from available set in one operation.
|
|
71
71
|
* If no rooms available, attempts to reclaim rooms with expired sessions first.
|
|
72
72
|
*/
|
|
73
|
-
async assignRoom(
|
|
73
|
+
async assignRoom(identifierHash, demoSessionId, durationMs) {
|
|
74
74
|
// Atomic pop from available set
|
|
75
75
|
let roomIdStr = await this.redisClient.spop('demo:pool:available');
|
|
76
76
|
// If no rooms available, try reclaiming expired sessions
|
|
@@ -87,8 +87,7 @@ class DemoRoomPoolManager {
|
|
|
87
87
|
const expiresAt = now + durationMs;
|
|
88
88
|
// Store session info
|
|
89
89
|
await this.redisClient.hset(`demo:pool:room:${roomId}:session`, {
|
|
90
|
-
|
|
91
|
-
email: email || '',
|
|
90
|
+
identifierHash,
|
|
92
91
|
demoSessionId,
|
|
93
92
|
startedAt: now.toString(),
|
|
94
93
|
expiresAt: expiresAt.toString(),
|
|
@@ -149,26 +148,26 @@ class DemoRoomPoolManager {
|
|
|
149
148
|
if (!(data === null || data === void 0 ? void 0 : data.demoSessionId))
|
|
150
149
|
return null;
|
|
151
150
|
return {
|
|
152
|
-
|
|
151
|
+
identifierHash: data.identifierHash,
|
|
153
152
|
demoSessionId: data.demoSessionId,
|
|
154
153
|
startedAt: parseInt(data.startedAt),
|
|
155
154
|
expiresAt: parseInt(data.expiresAt)
|
|
156
155
|
};
|
|
157
156
|
}
|
|
158
157
|
/**
|
|
159
|
-
* Check
|
|
158
|
+
* Check IP rate limit. Returns true if under limit, false if exceeded.
|
|
160
159
|
*/
|
|
161
|
-
async
|
|
162
|
-
const count = await this.redisClient.get(`demo:ratelimit:
|
|
160
|
+
async checkIpRateLimit(ipHash, max = 2) {
|
|
161
|
+
const count = await this.redisClient.get(`demo:ratelimit:ip:${ipHash}`);
|
|
163
162
|
return !count || parseInt(count) < max;
|
|
164
163
|
}
|
|
165
164
|
/**
|
|
166
|
-
* Increment
|
|
165
|
+
* Increment IP rate limit counter with 24h TTL.
|
|
167
166
|
*/
|
|
168
|
-
async
|
|
169
|
-
const key = `demo:ratelimit:
|
|
167
|
+
async incrementIpRateLimit(ipHash) {
|
|
168
|
+
const key = `demo:ratelimit:ip:${ipHash}`;
|
|
170
169
|
await this.redisClient.incr(key);
|
|
171
|
-
await this.redisClient.expire(key, 86400);
|
|
170
|
+
await this.redisClient.expire(key, 86400);
|
|
172
171
|
}
|
|
173
172
|
}
|
|
174
173
|
exports.DemoRoomPoolManager = DemoRoomPoolManager;
|