You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-12-12 21:21:15 +03:00
feat(xreadgroup): add claim attribute (#3122)
* feat(xreadgroup): add claim attribute the CLAIM attribute can be used to instruct redis to return PEL ( Pending Entries List ) entries with their respective deliveries and ms since last delivery * remove m01 from test matrix * add jsdoc
This commit is contained in:
committed by
GitHub
parent
130e88d45c
commit
5a0a06df69
2
.github/workflows/tests.yml
vendored
2
.github/workflows/tests.yml
vendored
@@ -22,7 +22,7 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node-version: ["18", "20", "22"]
|
node-version: ["18", "20", "22"]
|
||||||
redis-version: ["rs-7.4.0-v1", "8.0.2", "8.2", "8.4-M01-pre", "8.4-RC1-pre"]
|
redis-version: ["rs-7.4.0-v1", "8.0.2", "8.2", "8.4-RC1-pre"]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -93,6 +93,33 @@ describe('XREADGROUP', () => {
|
|||||||
['XREADGROUP', 'GROUP', 'group', 'consumer', 'COUNT', '1', 'BLOCK', '0', 'NOACK', 'STREAMS', 'key', '0-0']
|
['XREADGROUP', 'GROUP', 'group', 'consumer', 'COUNT', '1', 'BLOCK', '0', 'NOACK', 'STREAMS', 'key', '0-0']
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('with CLAIM', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
parseArgs(XREADGROUP, 'group', 'consumer', {
|
||||||
|
key: 'key',
|
||||||
|
id: '0-0'
|
||||||
|
}, {
|
||||||
|
CLAIM: 100
|
||||||
|
}),
|
||||||
|
['XREADGROUP', 'GROUP', 'group', 'consumer', 'CLAIM', '100', 'STREAMS', 'key', '0-0']
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with COUNT, BLOCK, NOACK, CLAIM', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
parseArgs(XREADGROUP, 'group', 'consumer', {
|
||||||
|
key: 'key',
|
||||||
|
id: '0-0'
|
||||||
|
}, {
|
||||||
|
COUNT: 1,
|
||||||
|
BLOCK: 0,
|
||||||
|
NOACK: true,
|
||||||
|
CLAIM: 100
|
||||||
|
}),
|
||||||
|
['XREADGROUP', 'GROUP', 'group', 'consumer', 'COUNT', '1', 'BLOCK', '0', 'NOACK', 'CLAIM', '100', 'STREAMS', 'key', '0-0']
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
testUtils.testAll('xReadGroup - null', async client => {
|
testUtils.testAll('xReadGroup - null', async client => {
|
||||||
@@ -156,35 +183,54 @@ describe('XREADGROUP', () => {
|
|||||||
cluster: GLOBAL.CLUSTERS.OPEN
|
cluster: GLOBAL.CLUSTERS.OPEN
|
||||||
});
|
});
|
||||||
|
|
||||||
testUtils.testWithClient('client.xReadGroup should throw with resp3 and unstableResp3: false', async client => {
|
testUtils.testAll('xReadGroup - without CLAIM should not include delivery fields', async client => {
|
||||||
assert.throws(
|
const [, id] = await Promise.all([
|
||||||
() => client.xReadGroup('group', 'consumer', {
|
client.xGroupCreate('key', 'group', '$', {
|
||||||
key: 'key',
|
MKSTREAM: true
|
||||||
id: '>'
|
|
||||||
}),
|
}),
|
||||||
{
|
client.xAdd('key', '*', { field: 'value' })
|
||||||
message: 'Some RESP3 results for Redis Query Engine responses may change. Refer to the readme for guidance'
|
]);
|
||||||
}
|
|
||||||
);
|
const readGroupReply = await client.xReadGroup('group', 'consumer', {
|
||||||
|
key: 'key',
|
||||||
|
id: '>'
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.ok(readGroupReply);
|
||||||
|
assert.equal(readGroupReply[0].messages[0].millisElapsedFromDelivery, undefined);
|
||||||
|
assert.equal(readGroupReply[0].messages[0].deliveriesCounter, undefined);
|
||||||
}, {
|
}, {
|
||||||
...GLOBAL.SERVERS.OPEN,
|
client: GLOBAL.SERVERS.OPEN,
|
||||||
clientOptions: {
|
cluster: GLOBAL.CLUSTERS.OPEN
|
||||||
RESP: 3
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
testUtils.testWithClient('client.xReadGroup should not throw with resp3 and unstableResp3: true', async client => {
|
testUtils.testWithClientIfVersionWithinRange([[8,4], 'LATEST'],'xReadGroup - with CLAIM should include delivery fields', async client => {
|
||||||
assert.doesNotThrow(
|
const [, id] = await Promise.all([
|
||||||
() => client.xReadGroup('group', 'consumer', {
|
client.xGroupCreate('key', 'group', '$', {
|
||||||
key: 'key',
|
MKSTREAM: true
|
||||||
id: '>'
|
}),
|
||||||
})
|
client.xAdd('key', '*', { field: 'value' })
|
||||||
);
|
]);
|
||||||
}, {
|
|
||||||
...GLOBAL.SERVERS.OPEN,
|
// First read to add message to PEL
|
||||||
clientOptions: {
|
await client.xReadGroup('group', 'consumer', {
|
||||||
RESP: 3,
|
key: 'key',
|
||||||
unstableResp3: true
|
id: '>'
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
// Read with CLAIM to get delivery fields
|
||||||
|
const readGroupReply = await client.xReadGroup('group', 'consumer2', {
|
||||||
|
key: 'key',
|
||||||
|
id: '>'
|
||||||
|
}, {
|
||||||
|
CLAIM: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.ok(readGroupReply);
|
||||||
|
assert.equal(readGroupReply[0].messages[0].id, id);
|
||||||
|
assert.ok(readGroupReply[0].messages[0].millisElapsedFromDelivery !== undefined);
|
||||||
|
assert.ok(readGroupReply[0].messages[0].deliveriesCounter !== undefined);
|
||||||
|
assert.equal(typeof readGroupReply[0].messages[0].millisElapsedFromDelivery, 'number');
|
||||||
|
assert.equal(typeof readGroupReply[0].messages[0].deliveriesCounter, 'number');
|
||||||
|
}, GLOBAL.SERVERS.OPEN);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,15 +5,17 @@ import { transformStreamsMessagesReplyResp2 } from './generic-transformers';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Options for the XREADGROUP command
|
* Options for the XREADGROUP command
|
||||||
*
|
*
|
||||||
* @property COUNT - Limit the number of entries returned per stream
|
* @property COUNT - Limit the number of entries returned per stream
|
||||||
* @property BLOCK - Milliseconds to block waiting for new entries (0 for indefinite)
|
* @property BLOCK - Milliseconds to block waiting for new entries (0 for indefinite)
|
||||||
* @property NOACK - Skip adding the message to the PEL (Pending Entries List)
|
* @property NOACK - Skip adding the message to the PEL (Pending Entries List)
|
||||||
|
* @property CLAIM - Prepend PEL entries that are at least this many milliseconds old
|
||||||
*/
|
*/
|
||||||
export interface XReadGroupOptions {
|
export interface XReadGroupOptions {
|
||||||
COUNT?: number;
|
COUNT?: number;
|
||||||
BLOCK?: number;
|
BLOCK?: number;
|
||||||
NOACK?: boolean;
|
NOACK?: boolean;
|
||||||
|
CLAIM?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -50,6 +52,10 @@ export default {
|
|||||||
parser.push('NOACK');
|
parser.push('NOACK');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options?.CLAIM !== undefined) {
|
||||||
|
parser.push('CLAIM', options.CLAIM.toString());
|
||||||
|
}
|
||||||
|
|
||||||
pushXReadStreams(parser, streams);
|
pushXReadStreams(parser, streams);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@@ -59,5 +65,4 @@ export default {
|
|||||||
2: transformStreamsMessagesReplyResp2,
|
2: transformStreamsMessagesReplyResp2,
|
||||||
3: undefined as unknown as () => ReplyUnion
|
3: undefined as unknown as () => ReplyUnion
|
||||||
},
|
},
|
||||||
unstableResp3: true,
|
|
||||||
} as const satisfies Command;
|
} as const satisfies Command;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export function transformStringDoubleArgument(num: RedisArgument | number): Redi
|
|||||||
export const transformDoubleReply = {
|
export const transformDoubleReply = {
|
||||||
2: (reply: BlobStringReply, preserve?: any, typeMapping?: TypeMapping): DoubleReply => {
|
2: (reply: BlobStringReply, preserve?: any, typeMapping?: TypeMapping): DoubleReply => {
|
||||||
const double = typeMapping ? typeMapping[RESP_TYPES.DOUBLE] : undefined;
|
const double = typeMapping ? typeMapping[RESP_TYPES.DOUBLE] : undefined;
|
||||||
|
|
||||||
switch (double) {
|
switch (double) {
|
||||||
case String: {
|
case String: {
|
||||||
return reply as unknown as DoubleReply;
|
return reply as unknown as DoubleReply;
|
||||||
@@ -58,13 +58,13 @@ export const transformDoubleReply = {
|
|||||||
case 'inf':
|
case 'inf':
|
||||||
case '+inf':
|
case '+inf':
|
||||||
ret = Infinity;
|
ret = Infinity;
|
||||||
|
|
||||||
case '-inf':
|
case '-inf':
|
||||||
ret = -Infinity;
|
ret = -Infinity;
|
||||||
|
|
||||||
case 'nan':
|
case 'nan':
|
||||||
ret = NaN;
|
ret = NaN;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
ret = Number(reply);
|
ret = Number(reply);
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ export function createTransformNullableDoubleReplyResp2Func(preserve?: any, type
|
|||||||
export const transformNullableDoubleReply = {
|
export const transformNullableDoubleReply = {
|
||||||
2: (reply: BlobStringReply | NullReply, preserve?: any, typeMapping?: TypeMapping) => {
|
2: (reply: BlobStringReply | NullReply, preserve?: any, typeMapping?: TypeMapping) => {
|
||||||
if (reply === null) return null;
|
if (reply === null) return null;
|
||||||
|
|
||||||
return transformDoubleReply[2](reply as BlobStringReply, preserve, typeMapping);
|
return transformDoubleReply[2](reply as BlobStringReply, preserve, typeMapping);
|
||||||
},
|
},
|
||||||
3: undefined as unknown as () => DoubleReply | NullReply
|
3: undefined as unknown as () => DoubleReply | NullReply
|
||||||
@@ -514,19 +514,25 @@ export function parseArgs(command: Command, ...args: Array<any>): CommandArgumen
|
|||||||
|
|
||||||
export type StreamMessageRawReply = TuplesReply<[
|
export type StreamMessageRawReply = TuplesReply<[
|
||||||
id: BlobStringReply,
|
id: BlobStringReply,
|
||||||
message: ArrayReply<BlobStringReply>
|
message: ArrayReply<BlobStringReply>,
|
||||||
|
millisElapsedFromDelivery?: NumberReply,
|
||||||
|
deliveriesCounter?: NumberReply
|
||||||
]>;
|
]>;
|
||||||
|
|
||||||
export type StreamMessageReply = {
|
export type StreamMessageReply = {
|
||||||
id: BlobStringReply,
|
id: BlobStringReply,
|
||||||
message: MapReply<BlobStringReply | string, BlobStringReply>,
|
message: MapReply<BlobStringReply | string, BlobStringReply>,
|
||||||
|
millisElapsedFromDelivery?: number
|
||||||
|
deliveriesCounter?: number
|
||||||
};
|
};
|
||||||
|
|
||||||
export function transformStreamMessageReply(typeMapping: TypeMapping | undefined, reply: StreamMessageRawReply): StreamMessageReply {
|
export function transformStreamMessageReply(typeMapping: TypeMapping | undefined, reply: StreamMessageRawReply): StreamMessageReply {
|
||||||
const [ id, message ] = reply as unknown as UnwrapReply<typeof reply>;
|
const [ id, message, millisElapsedFromDelivery, deliveriesCounter ] = reply as unknown as UnwrapReply<typeof reply>;
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
message: transformTuplesReply(message, undefined, typeMapping)
|
message: transformTuplesReply(message, undefined, typeMapping),
|
||||||
|
...(millisElapsedFromDelivery !== undefined ? { millisElapsedFromDelivery: Number(millisElapsedFromDelivery) } : {}),
|
||||||
|
...(deliveriesCounter !== undefined ? { deliveriesCounter: Number(deliveriesCounter) } : {})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,7 +563,7 @@ export function transformStreamsMessagesReplyResp2(
|
|||||||
reply: UnwrapReply<StreamsMessagesRawReply2 | NullReply>,
|
reply: UnwrapReply<StreamsMessagesRawReply2 | NullReply>,
|
||||||
preserve?: any,
|
preserve?: any,
|
||||||
typeMapping?: TypeMapping
|
typeMapping?: TypeMapping
|
||||||
): StreamsMessagesReply | NullReply {
|
): StreamsMessagesReply | NullReply {
|
||||||
// FUTURE: resposne type if resp3 was working, reverting to old v4 for now
|
// FUTURE: resposne type if resp3 was working, reverting to old v4 for now
|
||||||
//: MapReply<BlobStringReply | string, StreamMessagesReply> | NullReply {
|
//: MapReply<BlobStringReply | string, StreamMessagesReply> | NullReply {
|
||||||
if (reply === null) return null as unknown as NullReply;
|
if (reply === null) return null as unknown as NullReply;
|
||||||
@@ -569,13 +575,13 @@ export function transformStreamsMessagesReplyResp2(
|
|||||||
|
|
||||||
for (let i=0; i < reply.length; i++) {
|
for (let i=0; i < reply.length; i++) {
|
||||||
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
||||||
|
|
||||||
const name = stream[0];
|
const name = stream[0];
|
||||||
const rawMessages = stream[1];
|
const rawMessages = stream[1];
|
||||||
|
|
||||||
ret.set(name.toString(), transformStreamMessagesReply(rawMessages, typeMapping));
|
ret.set(name.toString(), transformStreamMessagesReply(rawMessages, typeMapping));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret as unknown as MapReply<string, StreamMessagesReply>;
|
return ret as unknown as MapReply<string, StreamMessagesReply>;
|
||||||
}
|
}
|
||||||
case Array: {
|
case Array: {
|
||||||
@@ -583,11 +589,11 @@ export function transformStreamsMessagesReplyResp2(
|
|||||||
|
|
||||||
for (let i=0; i < reply.length; i++) {
|
for (let i=0; i < reply.length; i++) {
|
||||||
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
||||||
|
|
||||||
const name = stream[0];
|
const name = stream[0];
|
||||||
const rawMessages = stream[1];
|
const rawMessages = stream[1];
|
||||||
|
|
||||||
ret.push(name);
|
ret.push(name);
|
||||||
ret.push(transformStreamMessagesReply(rawMessages, typeMapping));
|
ret.push(transformStreamMessagesReply(rawMessages, typeMapping));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,13 +604,13 @@ export function transformStreamsMessagesReplyResp2(
|
|||||||
|
|
||||||
for (let i=0; i < reply.length; i++) {
|
for (let i=0; i < reply.length; i++) {
|
||||||
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
const stream = reply[i] as unknown as UnwrapReply<StreamMessagesRawReply>;
|
||||||
|
|
||||||
const name = stream[0] as unknown as UnwrapReply<BlobStringReply>;
|
const name = stream[0] as unknown as UnwrapReply<BlobStringReply>;
|
||||||
const rawMessages = stream[1];
|
const rawMessages = stream[1];
|
||||||
|
|
||||||
ret[name.toString()] = transformStreamMessagesReply(rawMessages);
|
ret[name.toString()] = transformStreamMessagesReply(rawMessages);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret as unknown as MapReply<string, StreamMessagesReply>;
|
return ret as unknown as MapReply<string, StreamMessagesReply>;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
@@ -630,7 +636,7 @@ type StreamsMessagesRawReply3 = MapReply<BlobStringReply, ArrayReply<StreamMessa
|
|||||||
|
|
||||||
export function transformStreamsMessagesReplyResp3(reply: UnwrapReply<StreamsMessagesRawReply3 | NullReply>): MapReply<BlobStringReply, StreamMessagesReply> | NullReply {
|
export function transformStreamsMessagesReplyResp3(reply: UnwrapReply<StreamsMessagesRawReply3 | NullReply>): MapReply<BlobStringReply, StreamMessagesReply> | NullReply {
|
||||||
if (reply === null) return null as unknown as NullReply;
|
if (reply === null) return null as unknown as NullReply;
|
||||||
|
|
||||||
if (reply instanceof Map) {
|
if (reply instanceof Map) {
|
||||||
const ret = new Map<string, StreamMessagesReply>();
|
const ret = new Map<string, StreamMessagesReply>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user