Skip to content

Commit e7653c4

Browse files
feat: make BLOCK timeout configurable
1 parent 1f7e6fc commit e7653c4

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ io.listen(3000);
133133
| `streamCount` | The number of streams to use to scale horizontally. | `1` |
134134
| `maxLen` | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000` |
135135
| `readCount` | The number of elements to fetch per XREAD call. | `100` |
136+
| `blockTimeInMs` | The number of ms before the XREAD call times out. | `5_000` |
136137
| `sessionKeyPrefix` | The prefix of the key used to store the Socket.IO session, when the connection state recovery feature is enabled. | `sio:session:` |
137138
| `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
138139
| `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |

lib/adapter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export interface RedisStreamsAdapterOptions {
5252
* @default 100
5353
*/
5454
readCount?: number;
55+
/**
56+
* The number of ms before the XREAD call times out.
57+
* @default 200
58+
* @see https://redis.io/docs/latest/commands/xread/#blocking-for-data
59+
*/
60+
blockTimeInMs?: number;
5561
/**
5662
* The prefix of the key used to store the Socket.IO session, when the connection state recovery feature is enabled.
5763
* @default "sio:session:"
@@ -117,7 +123,8 @@ function startPolling(
117123
redisClient,
118124
streamName,
119125
offset,
120-
options.readCount
126+
options.readCount,
127+
options.blockTimeInMs
121128
);
122129

123130
if (response) {
@@ -163,6 +170,7 @@ export function createAdapter(
163170
streamCount: 1,
164171
maxLen: 10_000,
165172
readCount: 100,
173+
blockTimeInMs: 5_000,
166174
sessionKeyPrefix: "sio:session:",
167175
heartbeatInterval: 5_000,
168176
heartbeatTimeout: 10_000,

lib/util.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export function XREAD(
7272
redisClient: any,
7373
streamName: string,
7474
offset: string,
75-
readCount: number
75+
readCount: number,
76+
blockTimeInMs: number
7677
) {
7778
if (isRedisV4Client(redisClient)) {
7879
return redisClient.xRead(
@@ -84,12 +85,20 @@ export function XREAD(
8485
],
8586
{
8687
COUNT: readCount,
87-
BLOCK: 5000,
88+
BLOCK: blockTimeInMs,
8889
}
8990
);
9091
} else {
9192
return redisClient
92-
.xread("BLOCK", 5000, "COUNT", readCount, "STREAMS", streamName, offset)
93+
.xread(
94+
"BLOCK",
95+
blockTimeInMs,
96+
"COUNT",
97+
readCount,
98+
"STREAMS",
99+
streamName,
100+
offset
101+
)
93102
.then((results) => {
94103
if (results === null) {
95104
return null;

0 commit comments

Comments
 (0)