Skip to content

Commit 1921b51

Browse files
make BLOCK time configurable
1 parent 05f9634 commit 1921b51

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

lib/adapter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export interface RedisStreamsAdapterOptions {
3131
* @default 100
3232
*/
3333
readCount?: number;
34+
/**
35+
* The number of ms before timing out.
36+
* @default 200
37+
* @see https://redis.io/docs/latest/commands/xread/#blocking-for-data
38+
*/
39+
blockTimeInMs?: number;
3440
/**
3541
* The prefix of the key used to store the Socket.IO session, when the connection state recovery feature is enabled.
3642
* @default "sio:session:"
@@ -61,6 +67,7 @@ export function createAdapter(
6167
streamName: "socket.io",
6268
maxLen: 10_000,
6369
readCount: 100,
70+
blockTimeInMs: 200,
6471
sessionKeyPrefix: "sio:session:",
6572
heartbeatInterval: 5_000,
6673
heartbeatTimeout: 10_000,
@@ -77,7 +84,8 @@ export function createAdapter(
7784
redisClient,
7885
options.streamName,
7986
offset,
80-
options.readCount
87+
options.readCount,
88+
options.blockTimeInMs
8189
);
8290

8391
if (response) {

lib/util.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,15 @@ function mapResult(result) {
5757
};
5858
}
5959

60-
// https://redis.io/docs/latest/commands/xread/#blocking-for-data
61-
const BLOCK_DURATION_IN_MS = 200;
62-
6360
/**
6461
* @see https://redis.io/commands/xread/
6562
*/
6663
export function XREAD(
6764
redisClient: any,
6865
streamName: string,
6966
offset: string,
70-
readCount: number
67+
readCount: number,
68+
blockTimeInMs: number
7169
) {
7270
if (isNodeRedisClient(redisClient)) {
7371
return redisClient.xRead(
@@ -79,14 +77,14 @@ export function XREAD(
7977
],
8078
{
8179
COUNT: readCount,
82-
BLOCK: BLOCK_DURATION_IN_MS,
80+
BLOCK: blockTimeInMs,
8381
}
8482
);
8583
} else {
8684
return redisClient
8785
.xread(
8886
"BLOCK",
89-
BLOCK_DURATION_IN_MS,
87+
blockTimeInMs,
9088
"COUNT",
9189
readCount,
9290
"STREAMS",

test/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ export function setup({
178178

179179
const httpServer = createServer();
180180
const io = new Server(httpServer, {
181-
adapter: createAdapter(redisClient),
181+
adapter: createAdapter(redisClient, {
182+
readCount: 1, // return as soon as possible
183+
}),
182184
...serverOptions,
183185
});
184186
httpServer.listen(() => {

0 commit comments

Comments
 (0)