ioredis-streams 0.0.1
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/.eslintrc.json +24 -0
- package/.nycrc +20 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.js +356 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/src/index.ts +488 -0
- package/test/index.spec.ts +14 -0
- package/test/mocha.opts +3 -0
- package/test/tsconfig.json +46 -0
- package/tsconfig.json +78 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"es6": true
|
|
4
|
+
},
|
|
5
|
+
"extends": [
|
|
6
|
+
"eslint:recommended",
|
|
7
|
+
"plugin:@typescript-eslint/eslint-recommended"
|
|
8
|
+
],
|
|
9
|
+
"globals": {
|
|
10
|
+
"Atomics": "readonly",
|
|
11
|
+
"SharedArrayBuffer": "readonly"
|
|
12
|
+
},
|
|
13
|
+
"parser": "@typescript-eslint/parser",
|
|
14
|
+
"parserOptions": {
|
|
15
|
+
"ecmaVersion": 6,
|
|
16
|
+
"sourceType": "module"
|
|
17
|
+
},
|
|
18
|
+
"plugins": [
|
|
19
|
+
"@typescript-eslint"
|
|
20
|
+
],
|
|
21
|
+
"rules": {
|
|
22
|
+
"no-unused-vars": "off"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/.nycrc
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extension": [
|
|
3
|
+
".ts", ".js"
|
|
4
|
+
],
|
|
5
|
+
"include": [
|
|
6
|
+
"**/interfaces/**",
|
|
7
|
+
"**/uow/**",
|
|
8
|
+
"**/utils/**"
|
|
9
|
+
],
|
|
10
|
+
"exclude": [
|
|
11
|
+
"**/*.d.ts",
|
|
12
|
+
"**/*.spec.ts"
|
|
13
|
+
],
|
|
14
|
+
"reporter": [
|
|
15
|
+
"html",
|
|
16
|
+
"cobertura",
|
|
17
|
+
"text"
|
|
18
|
+
],
|
|
19
|
+
"all": true
|
|
20
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Zaid Al-Omari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import loggerFactory from '@log4js-node/log4js-api';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
export declare const getExistingRedisClient: (config?: {
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
}) => Redis.Redis;
|
|
7
|
+
export declare const getNewRedisClient: (config?: {
|
|
8
|
+
host: string;
|
|
9
|
+
port: number;
|
|
10
|
+
}) => Redis.Redis;
|
|
11
|
+
export declare class RedisStreams {
|
|
12
|
+
protected peerName: string;
|
|
13
|
+
protected groups: Map<string, Map<string, StreamGroupConsumer>>;
|
|
14
|
+
protected config: RedisStreamsConfig;
|
|
15
|
+
constructor(peerName: string, config?: Partial<RedisStreamsConfig>);
|
|
16
|
+
private getConsumerRedis;
|
|
17
|
+
private getProducerRedis;
|
|
18
|
+
private static getDefaultConfigs;
|
|
19
|
+
private buildConsumerConfigs;
|
|
20
|
+
private register;
|
|
21
|
+
private getStreamsMap;
|
|
22
|
+
group(groupName: string): ConsumerGroup;
|
|
23
|
+
private stream;
|
|
24
|
+
private _currentPipeline;
|
|
25
|
+
private getRedisPipeline;
|
|
26
|
+
private doProduce;
|
|
27
|
+
}
|
|
28
|
+
export interface IEvent<T, EventName extends string = string> {
|
|
29
|
+
time: number;
|
|
30
|
+
name: EventName;
|
|
31
|
+
v: string;
|
|
32
|
+
data: T;
|
|
33
|
+
}
|
|
34
|
+
export declare type EventProccessor = <T>(id: string, event: string, eventObj: IEvent<T>) => Promise<void>;
|
|
35
|
+
export declare type DeadLetterEvent = IEvent<{
|
|
36
|
+
id: string;
|
|
37
|
+
message: string[];
|
|
38
|
+
stream: string;
|
|
39
|
+
group: string;
|
|
40
|
+
}>;
|
|
41
|
+
export declare type RedisStreamsConfig = {
|
|
42
|
+
redis: {
|
|
43
|
+
host: string;
|
|
44
|
+
port: number;
|
|
45
|
+
};
|
|
46
|
+
logger: loggerFactory.Logger;
|
|
47
|
+
deadLetters?: {
|
|
48
|
+
stream: string;
|
|
49
|
+
maxRetries: number;
|
|
50
|
+
maxSize: number;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export declare type RegisterConsumerConfigs = {
|
|
54
|
+
readBlockTime: number;
|
|
55
|
+
claimIdleTime: number;
|
|
56
|
+
batchSize: number;
|
|
57
|
+
mode: 'parallel' | 'serial';
|
|
58
|
+
maxLen: number;
|
|
59
|
+
};
|
|
60
|
+
export declare type ConsumerConfigs = RegisterConsumerConfigs & {
|
|
61
|
+
peerName: string;
|
|
62
|
+
streamName: string;
|
|
63
|
+
groupName: string;
|
|
64
|
+
logger?: loggerFactory.Logger;
|
|
65
|
+
deadLetters?: {
|
|
66
|
+
handler: (...event: DeadLetterEvent[]) => Promise<void>;
|
|
67
|
+
maxRetries: number;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
export declare type EventHandler<T> = (id: string, event: IEvent<T>) => Promise<void>;
|
|
71
|
+
export declare type HandleFunc = <T = any, E = string>(event: E | '*', handler: EventHandler<T>) => {
|
|
72
|
+
handle: HandleFunc;
|
|
73
|
+
consume: () => Promise<{
|
|
74
|
+
stop: () => void;
|
|
75
|
+
continue: () => void;
|
|
76
|
+
}>;
|
|
77
|
+
};
|
|
78
|
+
export declare type ProduceFunc = <T>(...events: IEvent<T>[]) => {
|
|
79
|
+
produceMany: ProduceFunc;
|
|
80
|
+
flush: () => Promise<void>;
|
|
81
|
+
};
|
|
82
|
+
export declare type StreamGroupConsumer = {
|
|
83
|
+
handle: HandleFunc;
|
|
84
|
+
produce: <T>(...events: IEvent<T>[]) => Promise<void>;
|
|
85
|
+
produceMany: ProduceFunc;
|
|
86
|
+
with: <O extends FunctionsMap<R>, R>(events: O) => WithTypedHandlers<O, R>;
|
|
87
|
+
};
|
|
88
|
+
export declare type ConsumerGroup = {
|
|
89
|
+
stream: (streamName: string, config?: Partial<RegisterConsumerConfigs>) => StreamGroupConsumer;
|
|
90
|
+
};
|
|
91
|
+
export declare type HandleFunction<O extends FunctionsMap<R>, R> = <N extends (keyof O & string)>(event: N, handler: NamedEventHandler<ReturnType<O[N], N>>) => {
|
|
92
|
+
handle: HandleFunction<O, R>;
|
|
93
|
+
consume: () => Promise<{
|
|
94
|
+
stop: () => void;
|
|
95
|
+
continue: () => void;
|
|
96
|
+
}>;
|
|
97
|
+
};
|
|
98
|
+
declare type WithTypedHandlers<T extends FunctionsMap<R>, R> = T & {
|
|
99
|
+
handle: HandleFunction<T, R>;
|
|
100
|
+
};
|
|
101
|
+
declare type UnrwapPromiseEvent<T, N extends string> = T extends Promise<IEvent<infer D>> ? IEvent<D, N> : unknown;
|
|
102
|
+
declare type ReturnType<T, N extends string> = T extends (...args: any[]) => infer R ? UnrwapPromiseEvent<R, N> : unknown;
|
|
103
|
+
declare type FunctionsMap<T> = {
|
|
104
|
+
[func in keyof T]: T[func];
|
|
105
|
+
};
|
|
106
|
+
declare type NamedEventHandler<E> = (id: string, event: E) => Promise<void>;
|
|
107
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RedisStreams = exports.getNewRedisClient = exports.getExistingRedisClient = void 0;
|
|
7
|
+
const log4js_api_1 = __importDefault(require("@log4js-node/log4js-api"));
|
|
8
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
9
|
+
const redisHost = process.env.REDIS_HOST;
|
|
10
|
+
const redisPort = parseInt(process.env.REDIS_PORT || '0');
|
|
11
|
+
const getDefaultRedisConfigs = () => {
|
|
12
|
+
if (!redisHost || redisHost === '') {
|
|
13
|
+
throw new Error('No redis connection provided (REDIS_HOST) ...');
|
|
14
|
+
}
|
|
15
|
+
if (!redisPort || redisPort === 0) {
|
|
16
|
+
throw new Error('No redis connection provided (REDIS_PORT) ...');
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
host: process.env.REDIS_HOST || '',
|
|
20
|
+
port: parseInt(process.env.REDIS_PORT || '0')
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
let _redisClient;
|
|
24
|
+
const getExistingRedisClient = (config = getDefaultRedisConfigs()) => {
|
|
25
|
+
if (!_redisClient) {
|
|
26
|
+
_redisClient = new ioredis_1.default(config);
|
|
27
|
+
}
|
|
28
|
+
return _redisClient;
|
|
29
|
+
};
|
|
30
|
+
exports.getExistingRedisClient = getExistingRedisClient;
|
|
31
|
+
const getNewRedisClient = (config = getDefaultRedisConfigs()) => {
|
|
32
|
+
return new ioredis_1.default(config);
|
|
33
|
+
};
|
|
34
|
+
exports.getNewRedisClient = getNewRedisClient;
|
|
35
|
+
class RedisStreams {
|
|
36
|
+
peerName;
|
|
37
|
+
groups = new Map();
|
|
38
|
+
config;
|
|
39
|
+
constructor(peerName, config) {
|
|
40
|
+
this.peerName = peerName;
|
|
41
|
+
this.config = RedisStreams.getDefaultConfigs(peerName, config);
|
|
42
|
+
}
|
|
43
|
+
getConsumerRedis() {
|
|
44
|
+
return (0, exports.getNewRedisClient)(this.config.redis);
|
|
45
|
+
}
|
|
46
|
+
getProducerRedis() {
|
|
47
|
+
return (0, exports.getExistingRedisClient)(this.config.redis);
|
|
48
|
+
}
|
|
49
|
+
static getDefaultConfigs(peerName, config) {
|
|
50
|
+
return Object.assign({
|
|
51
|
+
redis: getDefaultRedisConfigs(),
|
|
52
|
+
logger: log4js_api_1.default.getLogger(peerName),
|
|
53
|
+
}, config || {});
|
|
54
|
+
}
|
|
55
|
+
buildConsumerConfigs(streamName, groupName, config) {
|
|
56
|
+
return Object.assign({
|
|
57
|
+
claimIdleTime: 15000,
|
|
58
|
+
groupName,
|
|
59
|
+
batchSize: 5,
|
|
60
|
+
mode: 'parallel',
|
|
61
|
+
readBlockTime: 5000,
|
|
62
|
+
peerName: this.peerName,
|
|
63
|
+
maxLen: 100000,
|
|
64
|
+
streamName,
|
|
65
|
+
deadLetters: this.config.deadLetters ? {
|
|
66
|
+
maxRetries: this.config.deadLetters.maxRetries,
|
|
67
|
+
handler: async (...events) => {
|
|
68
|
+
if (!this.config.deadLetters)
|
|
69
|
+
return;
|
|
70
|
+
const redis = this.getProducerRedis().pipeline();
|
|
71
|
+
this.doProduce(redis, this.config.deadLetters.stream, this.config.deadLetters.maxSize, ...events);
|
|
72
|
+
await redis.exec();
|
|
73
|
+
}
|
|
74
|
+
} : undefined
|
|
75
|
+
}, config || {});
|
|
76
|
+
}
|
|
77
|
+
register(streamName, groupName, handlers, config) {
|
|
78
|
+
const handler = async (id, event, eventObj) => {
|
|
79
|
+
let handle = handlers.get(event);
|
|
80
|
+
if (!handle)
|
|
81
|
+
handle = handlers.get('*');
|
|
82
|
+
if (!handle) {
|
|
83
|
+
this.config.logger.warn('No handler for:', groupName, streamName, event, id);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
;
|
|
87
|
+
return handle(id, eventObj);
|
|
88
|
+
};
|
|
89
|
+
const newStream = new StreamConsumer(this.getConsumerRedis(), handler, config);
|
|
90
|
+
return newStream;
|
|
91
|
+
}
|
|
92
|
+
getStreamsMap(groupName) {
|
|
93
|
+
let streams = this.groups.get(groupName);
|
|
94
|
+
if (!streams) {
|
|
95
|
+
streams = new Map();
|
|
96
|
+
this.groups.set(groupName, streams);
|
|
97
|
+
}
|
|
98
|
+
return streams;
|
|
99
|
+
}
|
|
100
|
+
group(groupName) {
|
|
101
|
+
const streams = this.getStreamsMap(groupName);
|
|
102
|
+
return {
|
|
103
|
+
stream: (streamName, config) => {
|
|
104
|
+
const stream = streams.get(streamName);
|
|
105
|
+
if (stream)
|
|
106
|
+
return stream;
|
|
107
|
+
const newStream = this.stream(streamName, groupName, config);
|
|
108
|
+
streams.set(streamName, newStream);
|
|
109
|
+
return newStream;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
stream(streamName, groupName, config) {
|
|
114
|
+
const handlers = new Map();
|
|
115
|
+
const readyConfigs = this.buildConsumerConfigs(streamName, groupName, config);
|
|
116
|
+
const stream = this.register(streamName, groupName, handlers, readyConfigs);
|
|
117
|
+
const consume = async () => {
|
|
118
|
+
await stream.init();
|
|
119
|
+
stream.start();
|
|
120
|
+
return {
|
|
121
|
+
stop: () => stream.dispose(),
|
|
122
|
+
continue: () => { stream.start(); return; }
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
const handle = (event, handler) => {
|
|
126
|
+
handlers.set(event, handler);
|
|
127
|
+
return { handle, consume };
|
|
128
|
+
};
|
|
129
|
+
const produceMany = (...events) => {
|
|
130
|
+
const redis = this.doProduce(this.getRedisPipeline(), streamName, readyConfigs.maxLen, ...events);
|
|
131
|
+
return {
|
|
132
|
+
produceMany,
|
|
133
|
+
flush: async () => {
|
|
134
|
+
this._currentPipeline = undefined;
|
|
135
|
+
await redis.exec();
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
const produce = async (...events) => {
|
|
140
|
+
await this.doProduce(this.getProducerRedis().pipeline(), streamName, readyConfigs.maxLen, ...events).exec();
|
|
141
|
+
};
|
|
142
|
+
const newStream = {
|
|
143
|
+
handle,
|
|
144
|
+
produceMany,
|
|
145
|
+
produce,
|
|
146
|
+
with: (events) => augmentEvents(events, newStream)
|
|
147
|
+
};
|
|
148
|
+
return newStream;
|
|
149
|
+
}
|
|
150
|
+
_currentPipeline;
|
|
151
|
+
getRedisPipeline() {
|
|
152
|
+
if (!this._currentPipeline)
|
|
153
|
+
this._currentPipeline = this.getProducerRedis().multi();
|
|
154
|
+
return this._currentPipeline;
|
|
155
|
+
}
|
|
156
|
+
doProduce(redis, stream, maxLen, ...events) {
|
|
157
|
+
if (events.length === 0)
|
|
158
|
+
return redis;
|
|
159
|
+
for (const one of events) {
|
|
160
|
+
const eventString = JSON.stringify(one);
|
|
161
|
+
if (typeof maxLen === 'number') {
|
|
162
|
+
redis = redis.xadd(stream, 'MAXLEN', '~', String(maxLen), '*', one.name, eventString);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
redis = redis.xadd(stream, '*', one.name, eventString);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return redis;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.RedisStreams = RedisStreams;
|
|
172
|
+
class StreamConsumer {
|
|
173
|
+
redis;
|
|
174
|
+
processEvent;
|
|
175
|
+
config;
|
|
176
|
+
logger;
|
|
177
|
+
disposing = false;
|
|
178
|
+
lastReadId = '0-0';
|
|
179
|
+
checkBacklog = true;
|
|
180
|
+
buffer;
|
|
181
|
+
constructor(redis, processEvent, config) {
|
|
182
|
+
this.redis = redis;
|
|
183
|
+
this.processEvent = processEvent;
|
|
184
|
+
this.config = config;
|
|
185
|
+
if (this.config.logger)
|
|
186
|
+
this.logger = this.config.logger;
|
|
187
|
+
else
|
|
188
|
+
this.logger = log4js_api_1.default.getLogger(this.config.streamName + ':Consumer');
|
|
189
|
+
if (!this.config.batchSize)
|
|
190
|
+
this.config.batchSize = 5;
|
|
191
|
+
this.buffer = new ConsumerBuffer({
|
|
192
|
+
ack: async (...ids) => {
|
|
193
|
+
await this.redis.xack(this.config.streamName, this.config.groupName, ...ids);
|
|
194
|
+
},
|
|
195
|
+
error: async (id, message, error) => {
|
|
196
|
+
this.logger.error(`Error during processMessage on stream '${this.config.streamName}' with id '${id}'`, error);
|
|
197
|
+
},
|
|
198
|
+
process: async (id, message) => {
|
|
199
|
+
return this.processMessage(id, message);
|
|
200
|
+
},
|
|
201
|
+
mode: this.config.mode || 'parallel',
|
|
202
|
+
size: this.config.batchSize
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async init() {
|
|
206
|
+
await this.redis.xgroup('CREATE', this.config.streamName, this.config.groupName, '$', 'MKSTREAM').catch(err => {
|
|
207
|
+
if (!err.message.includes('BUSYGROUP')) {
|
|
208
|
+
this.logger.error(err);
|
|
209
|
+
throw err;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
this.logger.trace('Ready...');
|
|
213
|
+
}
|
|
214
|
+
async start() {
|
|
215
|
+
this.logger.trace('Started...');
|
|
216
|
+
await this.tryReading();
|
|
217
|
+
}
|
|
218
|
+
async tryReading() {
|
|
219
|
+
while (!this.disposing) {
|
|
220
|
+
try {
|
|
221
|
+
const claimedAnything = await this.doClaim();
|
|
222
|
+
if (claimedAnything && !this.disposing)
|
|
223
|
+
continue;
|
|
224
|
+
const dataAvaliable = await this.doRead();
|
|
225
|
+
if (dataAvaliable && !this.disposing)
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
this.logger.error('Error while tryReading', err);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
lastTimePendingCheck = 0;
|
|
234
|
+
async doClaim() {
|
|
235
|
+
if (this.lastTimePendingCheck + this.config.claimIdleTime >= Date.now())
|
|
236
|
+
return false;
|
|
237
|
+
const result = await this.redis.xpending(this.config.streamName, this.config.groupName, 'IDLE', this.config.claimIdleTime, '-', '+', this.config.batchSize);
|
|
238
|
+
this.lastTimePendingCheck = Date.now();
|
|
239
|
+
if (!result.length)
|
|
240
|
+
return false;
|
|
241
|
+
const toBeClaimedMessages = result.map(([id]) => id);
|
|
242
|
+
const streamsEntries = await this.redis.xclaim(this.config.streamName, this.config.groupName, this.config.peerName, this.config.claimIdleTime, ...toBeClaimedMessages);
|
|
243
|
+
const deadMessages = result.reduce((p, [id, , , counter]) => {
|
|
244
|
+
return (counter >= (this.config.deadLetters?.maxRetries || Number.MAX_VALUE)) ? p.add(id) : p;
|
|
245
|
+
}, new Set());
|
|
246
|
+
if (this.config.deadLetters && deadMessages.size) {
|
|
247
|
+
this.logger.info('Dead Letters', JSON.stringify(Array.from(deadMessages)));
|
|
248
|
+
const deadStreamsEntries = streamsEntries.filter(([id]) => deadMessages.has(id));
|
|
249
|
+
await this.publishDeadLetters(...deadStreamsEntries);
|
|
250
|
+
}
|
|
251
|
+
const goodStreamsEntries = streamsEntries.filter(([id]) => !deadMessages.has(id));
|
|
252
|
+
if (goodStreamsEntries.length) {
|
|
253
|
+
this.logger.info('Claimed', goodStreamsEntries.length);
|
|
254
|
+
await this.buffer.add(...goodStreamsEntries);
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
async doRead() {
|
|
259
|
+
const streamsEntries = await this.redis.xreadgroup('GROUP', this.config.groupName, this.config.peerName, 'BLOCK', this.config.readBlockTime, 'COUNT', 5, 'STREAMS', this.config.streamName, this.checkBacklog ? this.lastReadId : '>');
|
|
260
|
+
if (!streamsEntries || streamsEntries.length === 0) {
|
|
261
|
+
this.checkBacklog = false;
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
for (const [steamName, streamEntries] of streamsEntries) {
|
|
265
|
+
if (streamEntries.length === 0) {
|
|
266
|
+
this.checkBacklog = false;
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
await this.buffer.add(...streamEntries);
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
async processMessage(id, message) {
|
|
274
|
+
const [event, value] = message;
|
|
275
|
+
const eventObj = JSON.parse(value);
|
|
276
|
+
return this.processEvent(id, event, eventObj);
|
|
277
|
+
}
|
|
278
|
+
async dispose() {
|
|
279
|
+
this.disposing = true;
|
|
280
|
+
}
|
|
281
|
+
async publishDeadLetters(...messages) {
|
|
282
|
+
if (!this.config.deadLetters)
|
|
283
|
+
return;
|
|
284
|
+
const finalEvents = messages.map(([id, message]) => {
|
|
285
|
+
return {
|
|
286
|
+
name: 'deadMessage',
|
|
287
|
+
v: '1.0.0',
|
|
288
|
+
time: Date.now(),
|
|
289
|
+
data: {
|
|
290
|
+
id,
|
|
291
|
+
message,
|
|
292
|
+
stream: this.config.streamName,
|
|
293
|
+
group: this.config.groupName,
|
|
294
|
+
},
|
|
295
|
+
by: {
|
|
296
|
+
userId: this.config.peerName,
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
});
|
|
300
|
+
await this.config.deadLetters.handler(...finalEvents);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
class ConsumerBuffer {
|
|
304
|
+
configs;
|
|
305
|
+
buffer = [];
|
|
306
|
+
constructor(configs) {
|
|
307
|
+
this.configs = configs;
|
|
308
|
+
}
|
|
309
|
+
get size() {
|
|
310
|
+
return this.configs.size;
|
|
311
|
+
}
|
|
312
|
+
async add(...rawMessages) {
|
|
313
|
+
this.buffer.push(...rawMessages);
|
|
314
|
+
return this.do();
|
|
315
|
+
}
|
|
316
|
+
isDoing = false;
|
|
317
|
+
async do() {
|
|
318
|
+
if (this.isDoing)
|
|
319
|
+
return;
|
|
320
|
+
this.isDoing = true;
|
|
321
|
+
const toBeAck = [];
|
|
322
|
+
const operations = [];
|
|
323
|
+
while (this.buffer.length) {
|
|
324
|
+
const [id, message] = this.buffer.shift();
|
|
325
|
+
const operation = this.configs.process(id, message)
|
|
326
|
+
.then(_ => {
|
|
327
|
+
toBeAck.push(id);
|
|
328
|
+
})
|
|
329
|
+
.catch(err => this.configs.error(id, message, err).catch());
|
|
330
|
+
if (this.configs.mode === 'serial') {
|
|
331
|
+
await operation;
|
|
332
|
+
await this.configs.ack(id);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
operations.push(operation);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
this.isDoing = false;
|
|
339
|
+
if (this.configs.mode === 'parallel') {
|
|
340
|
+
await Promise.all(operations);
|
|
341
|
+
if (toBeAck.length) {
|
|
342
|
+
await this.configs.ack(...toBeAck);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const augmentEvents = (events, stream) => {
|
|
348
|
+
const initial = { ...events, ...stream };
|
|
349
|
+
return Object.keys(events).reduce((p, c) => {
|
|
350
|
+
p[c] = (...args) => events[c](...args).then((event) => {
|
|
351
|
+
return stream.produce(event);
|
|
352
|
+
});
|
|
353
|
+
return p;
|
|
354
|
+
}, initial);
|
|
355
|
+
};
|
|
356
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|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C,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACzC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,KAAU,EAAE,EAAE;YAChE,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC,CAAC,CAAC;QACH,OAAO,CAAC,CAAC;IACX,CAAC,EAAE,OAAO,CAAC,CAAA;AACb,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ioredis-streams",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"author": "Zaid Al-Omari",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Zaid-AlOmari/ioredis-streams"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"registry": "https://registry.npmjs.org"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "./node_modules/.bin/tsc --p tsconfig.json",
|
|
17
|
+
"test": "cd test; ../node_modules/.bin/tsc -p .; NODE_ENV=test ../node_modules/.bin/nyc ../node_modules/.bin/mocha '../dist-test/**/*.spec.js'",
|
|
18
|
+
"type-check": "tsc --noEmit",
|
|
19
|
+
"lint": "./node_modules/.bin/eslint src/*/*.ts --format codeframe",
|
|
20
|
+
"fix-lint": "./node_modules/.bin/eslint src/*/*.ts --fix"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@log4js-node/log4js-api": "^1.0.2",
|
|
24
|
+
"ioredis": "^4.28.5"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/chai": "^4.2.3",
|
|
28
|
+
"@types/ioredis": "^4.28.5",
|
|
29
|
+
"@types/mocha": "^5.2.7",
|
|
30
|
+
"@types/node": "^10.12.2",
|
|
31
|
+
"@types/sinon": "^7.5.0",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^4.22.0",
|
|
33
|
+
"@typescript-eslint/experimental-utils": "^4.22.0",
|
|
34
|
+
"@typescript-eslint/parser": "^4.22.0",
|
|
35
|
+
"chai": "^4.2.0",
|
|
36
|
+
"eslint": "^7.0.0",
|
|
37
|
+
"mocha": "^9.2.2",
|
|
38
|
+
"nyc": "^15.0.1",
|
|
39
|
+
"sinon": "^9.0.2",
|
|
40
|
+
"sinon-chai": "^3.5.0",
|
|
41
|
+
"source-map-support": "^0.5.19",
|
|
42
|
+
"typescript": "^4.6.2"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,488 @@
|
|
|
1
|
+
import loggerFactory from '@log4js-node/log4js-api';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
|
|
4
|
+
const redisHost = process.env.REDIS_HOST;
|
|
5
|
+
const redisPort = parseInt(process.env.REDIS_PORT || '0');
|
|
6
|
+
const getDefaultRedisConfigs = () => {
|
|
7
|
+
if (!redisHost || redisHost === '') {
|
|
8
|
+
throw new Error('No redis connection provided (REDIS_HOST) ...')
|
|
9
|
+
}
|
|
10
|
+
if (!redisPort || redisPort === 0) {
|
|
11
|
+
throw new Error('No redis connection provided (REDIS_PORT) ...')
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
host: process.env.REDIS_HOST || '',
|
|
15
|
+
port: parseInt(process.env.REDIS_PORT || '0')
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let _redisClient: Redis.Redis;
|
|
20
|
+
export const getExistingRedisClient = (config = getDefaultRedisConfigs()) => {
|
|
21
|
+
if (!_redisClient) {
|
|
22
|
+
_redisClient = new Redis(config);
|
|
23
|
+
}
|
|
24
|
+
return _redisClient;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const getNewRedisClient = (config = getDefaultRedisConfigs()) => {
|
|
28
|
+
return new Redis(config);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export class RedisStreams {
|
|
32
|
+
|
|
33
|
+
protected groups: Map<string, Map<string, StreamGroupConsumer>> = new Map();
|
|
34
|
+
protected config: RedisStreamsConfig;
|
|
35
|
+
|
|
36
|
+
constructor(protected peerName: string, config?: Partial<RedisStreamsConfig>) {
|
|
37
|
+
this.config = RedisStreams.getDefaultConfigs(peerName, config);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private getConsumerRedis() {
|
|
41
|
+
return getNewRedisClient(this.config.redis);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private getProducerRedis() {
|
|
45
|
+
return getExistingRedisClient(this.config.redis);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private static getDefaultConfigs(peerName: string, config?: Partial<RedisStreamsConfig>) {
|
|
49
|
+
return <RedisStreamsConfig>Object.assign(<RedisStreamsConfig>{
|
|
50
|
+
redis: getDefaultRedisConfigs(),
|
|
51
|
+
logger: loggerFactory.getLogger(peerName),
|
|
52
|
+
}, config || {});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private buildConsumerConfigs(streamName: string, groupName: string, config?: Partial<RegisterConsumerConfigs>) {
|
|
56
|
+
return <ConsumerConfigs>Object.assign(<ConsumerConfigs>{
|
|
57
|
+
claimIdleTime: 15000,
|
|
58
|
+
groupName,
|
|
59
|
+
batchSize: 5,
|
|
60
|
+
mode: 'parallel',
|
|
61
|
+
readBlockTime: 5000,
|
|
62
|
+
peerName: this.peerName!,
|
|
63
|
+
maxLen: 100000,
|
|
64
|
+
streamName,
|
|
65
|
+
deadLetters: this.config.deadLetters ? {
|
|
66
|
+
maxRetries: this.config.deadLetters.maxRetries,
|
|
67
|
+
handler: async (...events) => {
|
|
68
|
+
if (!this.config.deadLetters) return;
|
|
69
|
+
const redis = this.getProducerRedis().pipeline();
|
|
70
|
+
this.doProduce(redis, this.config.deadLetters.stream, this.config.deadLetters.maxSize, ...events);
|
|
71
|
+
await redis.exec();
|
|
72
|
+
}
|
|
73
|
+
} : undefined
|
|
74
|
+
}, config || {});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private register(streamName: string, groupName: string, handlers: Map<string, EventHandler<{}>>, config: ConsumerConfigs) {
|
|
78
|
+
const handler = async <T>(id: string, event: string | '*', eventObj: IEvent<T>) => {
|
|
79
|
+
let handle = handlers.get(event);
|
|
80
|
+
if (!handle) handle = handlers.get('*');
|
|
81
|
+
if (!handle) {
|
|
82
|
+
this.config.logger.warn('No handler for:', groupName, streamName, event, id);
|
|
83
|
+
return;
|
|
84
|
+
};
|
|
85
|
+
return handle(id, eventObj);
|
|
86
|
+
};
|
|
87
|
+
const newStream = new StreamConsumer(this.getConsumerRedis(), handler, config);
|
|
88
|
+
return newStream;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private getStreamsMap(groupName: string) {
|
|
92
|
+
let streams = this.groups.get(groupName);
|
|
93
|
+
if (!streams) {
|
|
94
|
+
streams = new Map<string, any>();
|
|
95
|
+
this.groups.set(groupName, streams);
|
|
96
|
+
}
|
|
97
|
+
return streams;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
group(groupName: string) {
|
|
101
|
+
const streams = this.getStreamsMap(groupName);
|
|
102
|
+
return <ConsumerGroup>{
|
|
103
|
+
stream: (streamName: string, config?: Partial<RegisterConsumerConfigs>) => {
|
|
104
|
+
const stream = streams.get(streamName);
|
|
105
|
+
if (stream) return stream;
|
|
106
|
+
const newStream = this.stream(streamName, groupName, config);
|
|
107
|
+
streams.set(streamName, newStream);
|
|
108
|
+
return newStream;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private stream(streamName: string, groupName: string, config?: Partial<RegisterConsumerConfigs>) {
|
|
114
|
+
const handlers = new Map<string, EventHandler<{}>>();
|
|
115
|
+
const readyConfigs = this.buildConsumerConfigs(streamName, groupName, config);
|
|
116
|
+
const stream = this.register(streamName, groupName, handlers, readyConfigs);
|
|
117
|
+
const consume = async () => {
|
|
118
|
+
await stream.init();
|
|
119
|
+
stream.start();
|
|
120
|
+
return {
|
|
121
|
+
stop: () => stream.dispose(),
|
|
122
|
+
continue: () => { stream.start(); return; }
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const handle = <T>(event: string | '*', handler: EventHandler<T>) => {
|
|
126
|
+
handlers.set(event, <EventHandler<{}>>handler);
|
|
127
|
+
return { handle, consume };
|
|
128
|
+
};
|
|
129
|
+
const produceMany = <T>(...events: IEvent<T>[]) => {
|
|
130
|
+
const redis = this.doProduce(this.getRedisPipeline(), streamName, readyConfigs.maxLen, ...events);
|
|
131
|
+
return {
|
|
132
|
+
produceMany,
|
|
133
|
+
flush: async () => {
|
|
134
|
+
this._currentPipeline = undefined;
|
|
135
|
+
await redis.exec();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const produce = async <T>(...events: IEvent<T>[]) => {
|
|
140
|
+
await this.doProduce(this.getProducerRedis().pipeline(), streamName, readyConfigs.maxLen, ...events).exec();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const newStream = <StreamGroupConsumer>{
|
|
144
|
+
handle,
|
|
145
|
+
produceMany,
|
|
146
|
+
produce,
|
|
147
|
+
with: (events) => augmentEvents(events, newStream)
|
|
148
|
+
}
|
|
149
|
+
return newStream;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private _currentPipeline: Redis.Pipeline | undefined;
|
|
153
|
+
private getRedisPipeline() {
|
|
154
|
+
if (!this._currentPipeline) this._currentPipeline = this.getProducerRedis().multi();
|
|
155
|
+
return this._currentPipeline;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private doProduce<T>(redis: Redis.Pipeline, stream: string, maxLen?: number, ...events: IEvent<T>[]) {
|
|
159
|
+
if (events.length === 0) return redis;
|
|
160
|
+
for (const one of events) {
|
|
161
|
+
const eventString = JSON.stringify(one);
|
|
162
|
+
if (typeof maxLen === 'number') {
|
|
163
|
+
redis = redis.xadd(stream, 'MAXLEN', '~', String(maxLen), '*', one.name, eventString);
|
|
164
|
+
} else {
|
|
165
|
+
redis = redis.xadd(stream, '*', one.name, eventString);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return redis;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class StreamConsumer {
|
|
173
|
+
|
|
174
|
+
protected logger!: loggerFactory.Logger;
|
|
175
|
+
private disposing = false;
|
|
176
|
+
|
|
177
|
+
private lastReadId = '0-0';
|
|
178
|
+
private checkBacklog = true;
|
|
179
|
+
|
|
180
|
+
private buffer: ConsumerBuffer;
|
|
181
|
+
|
|
182
|
+
constructor(
|
|
183
|
+
protected redis: Redis.Redis,
|
|
184
|
+
protected processEvent: EventProccessor,
|
|
185
|
+
protected config: ConsumerConfigs) {
|
|
186
|
+
|
|
187
|
+
if (this.config.logger) this.logger = this.config.logger;
|
|
188
|
+
else this.logger = loggerFactory.getLogger(this.config.streamName + ':Consumer');
|
|
189
|
+
if (!this.config.batchSize) this.config.batchSize = 5;
|
|
190
|
+
this.buffer = new ConsumerBuffer({
|
|
191
|
+
ack: async (...ids: string[]) => {
|
|
192
|
+
await this.redis.xack(
|
|
193
|
+
this.config.streamName,
|
|
194
|
+
this.config.groupName, ...ids
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
error: async (id: string, message: string[], error: Error) => {
|
|
198
|
+
this.logger.error(`Error during processMessage on stream '${this.config.streamName}' with id '${id}'`, error);
|
|
199
|
+
},
|
|
200
|
+
process: async (id: string, message: string[]) => {
|
|
201
|
+
return this.processMessage(id, message);
|
|
202
|
+
},
|
|
203
|
+
mode: this.config.mode || 'parallel',
|
|
204
|
+
size: this.config.batchSize
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async init() {
|
|
209
|
+
await this.redis.xgroup(
|
|
210
|
+
'CREATE',
|
|
211
|
+
this.config.streamName,
|
|
212
|
+
this.config.groupName,
|
|
213
|
+
'$',
|
|
214
|
+
'MKSTREAM'
|
|
215
|
+
).catch(err => {
|
|
216
|
+
if (!err.message.includes('BUSYGROUP')) {
|
|
217
|
+
this.logger.error(err);
|
|
218
|
+
throw err;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
this.logger.trace('Ready...');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async start() {
|
|
225
|
+
this.logger.trace('Started...');
|
|
226
|
+
await this.tryReading()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
protected async tryReading() {
|
|
230
|
+
while (!this.disposing) {
|
|
231
|
+
try {
|
|
232
|
+
const claimedAnything = await this.doClaim()
|
|
233
|
+
if (claimedAnything && !this.disposing) continue;
|
|
234
|
+
const dataAvaliable = await this.doRead();
|
|
235
|
+
if (dataAvaliable && !this.disposing) continue;
|
|
236
|
+
} catch (err) {
|
|
237
|
+
this.logger.error('Error while tryReading', err);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
lastTimePendingCheck = 0;
|
|
243
|
+
private async doClaim() {
|
|
244
|
+
if (this.lastTimePendingCheck + this.config.claimIdleTime >= Date.now()) return false;
|
|
245
|
+
const result: ([string, string, number, number])[] = await this.redis.xpending(
|
|
246
|
+
this.config.streamName,
|
|
247
|
+
this.config.groupName,
|
|
248
|
+
'IDLE', this.config.claimIdleTime,
|
|
249
|
+
'-', '+', this.config.batchSize!
|
|
250
|
+
);
|
|
251
|
+
this.lastTimePendingCheck = Date.now();
|
|
252
|
+
if (!result.length) return false;
|
|
253
|
+
|
|
254
|
+
const toBeClaimedMessages = result.map(([id]) => id);
|
|
255
|
+
const streamsEntries = await this.redis.xclaim(
|
|
256
|
+
this.config.streamName,
|
|
257
|
+
this.config.groupName,
|
|
258
|
+
this.config.peerName,
|
|
259
|
+
this.config.claimIdleTime,
|
|
260
|
+
...toBeClaimedMessages,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const deadMessages = result.reduce((p, [id, , , counter]) => {
|
|
264
|
+
return (counter >= (this.config.deadLetters?.maxRetries || Number.MAX_VALUE)) ? p.add(id) : p;
|
|
265
|
+
}, new Set<string>());
|
|
266
|
+
|
|
267
|
+
if (this.config.deadLetters && deadMessages.size) {
|
|
268
|
+
this.logger.info('Dead Letters', JSON.stringify(Array.from(deadMessages)));
|
|
269
|
+
const deadStreamsEntries = streamsEntries.filter(([id]) => deadMessages.has(id));
|
|
270
|
+
await this.publishDeadLetters(...deadStreamsEntries)
|
|
271
|
+
}
|
|
272
|
+
const goodStreamsEntries = streamsEntries.filter(([id]) => !deadMessages.has(id));
|
|
273
|
+
if (goodStreamsEntries.length) {
|
|
274
|
+
this.logger.info('Claimed', goodStreamsEntries.length);
|
|
275
|
+
await this.buffer.add(...goodStreamsEntries);
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
protected async doRead() {
|
|
281
|
+
const streamsEntries = await this.redis.xreadgroup(
|
|
282
|
+
'GROUP', this.config.groupName,
|
|
283
|
+
this.config.peerName,
|
|
284
|
+
'BLOCK', this.config.readBlockTime,
|
|
285
|
+
'COUNT', 5,
|
|
286
|
+
'STREAMS', this.config.streamName,
|
|
287
|
+
this.checkBacklog ? this.lastReadId : '>'
|
|
288
|
+
);
|
|
289
|
+
if (!streamsEntries || streamsEntries.length === 0) {
|
|
290
|
+
this.checkBacklog = false;
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
for (const [steamName, streamEntries] of streamsEntries) {
|
|
294
|
+
if (streamEntries.length === 0) {
|
|
295
|
+
this.checkBacklog = false;
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
await this.buffer.add(...streamEntries);
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
protected async processMessage<T>(id: string, message: string[]) {
|
|
304
|
+
const [event, value] = message;
|
|
305
|
+
const eventObj: IEvent<T> = JSON.parse(value);
|
|
306
|
+
return this.processEvent(id, event, eventObj)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async dispose() {
|
|
310
|
+
this.disposing = true;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async publishDeadLetters(...messages: [string, string[]][]) {
|
|
314
|
+
if (!this.config.deadLetters) return;
|
|
315
|
+
const finalEvents = messages.map(([id, message]) => {
|
|
316
|
+
return <DeadLetterEvent>{
|
|
317
|
+
name: 'deadMessage',
|
|
318
|
+
v: '1.0.0',
|
|
319
|
+
time: Date.now(),
|
|
320
|
+
data: {
|
|
321
|
+
id,
|
|
322
|
+
message,
|
|
323
|
+
stream: this.config.streamName,
|
|
324
|
+
group: this.config.groupName,
|
|
325
|
+
},
|
|
326
|
+
by: {
|
|
327
|
+
userId: this.config.peerName,
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
await this.config.deadLetters.handler(...finalEvents);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
class ConsumerBuffer {
|
|
336
|
+
|
|
337
|
+
buffer: [string, string[]][] = [];
|
|
338
|
+
constructor(protected configs: {
|
|
339
|
+
ack: (...id: string[]) => Promise<void>,
|
|
340
|
+
process: (id: string, message: string[]) => Promise<void>,
|
|
341
|
+
error: (id: string, message: string[], error: Error) => Promise<void>,
|
|
342
|
+
mode: 'parallel' | 'serial',
|
|
343
|
+
size: number
|
|
344
|
+
}) {
|
|
345
|
+
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
get size() {
|
|
349
|
+
return this.configs.size;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async add(...rawMessages: [string, string[]][]) {
|
|
353
|
+
this.buffer.push(...rawMessages);
|
|
354
|
+
return this.do();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
isDoing = false;
|
|
358
|
+
async do() {
|
|
359
|
+
if (this.isDoing) return;
|
|
360
|
+
this.isDoing = true;
|
|
361
|
+
const toBeAck: string[] = [];
|
|
362
|
+
const operations: Promise<void>[] = [];
|
|
363
|
+
while (this.buffer.length) {
|
|
364
|
+
const [id, message] = this.buffer.shift()!;
|
|
365
|
+
const operation = this.configs.process(id, message)
|
|
366
|
+
.then(_ => {
|
|
367
|
+
toBeAck.push(id);
|
|
368
|
+
})
|
|
369
|
+
.catch(err => this.configs.error(id, message, err).catch());
|
|
370
|
+
if (this.configs.mode === 'serial') {
|
|
371
|
+
await operation;
|
|
372
|
+
await this.configs.ack(id);
|
|
373
|
+
} else {
|
|
374
|
+
operations.push(operation);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
this.isDoing = false;
|
|
378
|
+
if (this.configs.mode === 'parallel') {
|
|
379
|
+
await Promise.all(operations);
|
|
380
|
+
if (toBeAck.length) {
|
|
381
|
+
await this.configs.ack(...toBeAck);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const augmentEvents = <T extends FunctionsMap<R>, R>(events: T, stream: StreamGroupConsumer) => {
|
|
388
|
+
const initial = <WithTypedHandlers<T, R> & StreamGroupConsumer>{ ...events, ...stream };
|
|
389
|
+
return Object.keys(events).reduce((p, c) => {
|
|
390
|
+
p[c] = (...args: any[]) => events[c](...args).then((event: any) => {
|
|
391
|
+
return stream.produce(event)
|
|
392
|
+
});
|
|
393
|
+
return p;
|
|
394
|
+
}, initial)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export interface IEvent<T, EventName extends string = string> {
|
|
398
|
+
time: number;
|
|
399
|
+
name: EventName;
|
|
400
|
+
v: string;
|
|
401
|
+
data: T;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export type EventProccessor = <T>(id: string, event: string, eventObj: IEvent<T>) => Promise<void>;
|
|
405
|
+
|
|
406
|
+
export type DeadLetterEvent = IEvent<{
|
|
407
|
+
id: string,
|
|
408
|
+
message: string[],
|
|
409
|
+
stream: string,
|
|
410
|
+
group: string,
|
|
411
|
+
}>;
|
|
412
|
+
|
|
413
|
+
export type RedisStreamsConfig = {
|
|
414
|
+
redis: {
|
|
415
|
+
host: string,
|
|
416
|
+
port: number
|
|
417
|
+
},
|
|
418
|
+
logger: loggerFactory.Logger;
|
|
419
|
+
deadLetters?: {
|
|
420
|
+
stream: string;
|
|
421
|
+
maxRetries: number;
|
|
422
|
+
maxSize: number;
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export type RegisterConsumerConfigs = {
|
|
427
|
+
readBlockTime: number;
|
|
428
|
+
claimIdleTime: number;
|
|
429
|
+
batchSize: number;
|
|
430
|
+
mode: 'parallel' | 'serial';
|
|
431
|
+
maxLen: number;
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
export type ConsumerConfigs = RegisterConsumerConfigs & {
|
|
435
|
+
peerName: string;
|
|
436
|
+
streamName: string;
|
|
437
|
+
groupName: string;
|
|
438
|
+
logger?: loggerFactory.Logger;
|
|
439
|
+
deadLetters?: {
|
|
440
|
+
handler: (...event: DeadLetterEvent[]) => Promise<void>
|
|
441
|
+
maxRetries: number
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
export type EventHandler<T> = (id: string, event: IEvent<T>) => Promise<void>;
|
|
445
|
+
|
|
446
|
+
export type HandleFunc = <T = any, E = string>(event: E | '*', handler: EventHandler<T>) => {
|
|
447
|
+
handle: HandleFunc;
|
|
448
|
+
consume: () => Promise<{
|
|
449
|
+
stop: () => void;
|
|
450
|
+
continue: () => void;
|
|
451
|
+
}>;
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
export type ProduceFunc = <T>(...events: IEvent<T>[]) => {
|
|
455
|
+
produceMany: ProduceFunc;
|
|
456
|
+
flush: () => Promise<void>;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export type StreamGroupConsumer = {
|
|
460
|
+
handle: HandleFunc;
|
|
461
|
+
produce: <T>(...events: IEvent<T>[]) => Promise<void>;
|
|
462
|
+
produceMany: ProduceFunc;
|
|
463
|
+
with: <O extends FunctionsMap<R>, R>(events: O) => WithTypedHandlers<O, R>;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export type ConsumerGroup = {
|
|
467
|
+
stream: (streamName: string, config?: Partial<RegisterConsumerConfigs>) => StreamGroupConsumer;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export type HandleFunction<O extends FunctionsMap<R>, R> = <N extends (keyof O & string) >(event: N, handler: NamedEventHandler<ReturnType<O[N], N>>) => {
|
|
471
|
+
handle: HandleFunction<O, R>;
|
|
472
|
+
consume: () => Promise<{
|
|
473
|
+
stop: () => void;
|
|
474
|
+
continue: () => void;
|
|
475
|
+
}>;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
type WithTypedHandlers<T extends FunctionsMap<R>, R> = T & {
|
|
479
|
+
handle: HandleFunction<T, R>
|
|
480
|
+
}
|
|
481
|
+
type UnrwapPromiseEvent<T, N extends string> = T extends Promise<IEvent<infer D>> ? IEvent<D, N> : unknown;
|
|
482
|
+
type ReturnType<T, N extends string> = T extends (...args: any[]) => infer R ? UnrwapPromiseEvent<R, N> : unknown;
|
|
483
|
+
|
|
484
|
+
type FunctionsMap<T> = {
|
|
485
|
+
[func in keyof T]: T[func];
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
type NamedEventHandler<E> = (id: string, event: E) => Promise<void>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import 'mocha';
|
|
2
|
+
import chai from 'chai';
|
|
3
|
+
import sinon from 'sinon';
|
|
4
|
+
chai.use(require('sinon-chai'));
|
|
5
|
+
const expect = chai.expect;
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
describe('ioredis-streams', () => {
|
|
9
|
+
describe('Running ', () => {
|
|
10
|
+
it('should run', async () => {
|
|
11
|
+
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
});
|
package/test/mocha.opts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compileOnSave": true,
|
|
3
|
+
"exclude": [
|
|
4
|
+
"node_modules",
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"compilerOptions": {
|
|
8
|
+
"baseUrl": ".",
|
|
9
|
+
"paths": {
|
|
10
|
+
"*": [
|
|
11
|
+
"../types/*"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
/* Basic Options */
|
|
16
|
+
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
|
|
17
|
+
"module": "CommonJS" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
|
|
18
|
+
"lib": [
|
|
19
|
+
"es2015",
|
|
20
|
+
"es2016",
|
|
21
|
+
"es2017",
|
|
22
|
+
"es2018"
|
|
23
|
+
] /* Specify library files to be included in the compilation. */,
|
|
24
|
+
"declaration": true /* Generates corresponding '.d.ts' file. */,
|
|
25
|
+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
26
|
+
"sourceMap": true /* Generates corresponding '.map' file. */,
|
|
27
|
+
"outDir": "../dist-test" /* Redirect output structure to the directory. */,
|
|
28
|
+
"rootDir": ".." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|
|
29
|
+
/* Strict Type-Checking Options */
|
|
30
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
31
|
+
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
|
|
32
|
+
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
|
|
33
|
+
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
|
|
34
|
+
/* Module Resolution Options */
|
|
35
|
+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
|
|
36
|
+
"typeRoots": [
|
|
37
|
+
"../node_modules"
|
|
38
|
+
] /* List of folders to include type definitions from. */,
|
|
39
|
+
"types": [
|
|
40
|
+
"@types/node"
|
|
41
|
+
] /* Type declaration files to be included in compilation. */,
|
|
42
|
+
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
|
|
43
|
+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
|
|
44
|
+
"resolveJsonModule": true
|
|
45
|
+
}
|
|
46
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compileOnSave": true,
|
|
3
|
+
"exclude": [
|
|
4
|
+
"node_modules",
|
|
5
|
+
"dist",
|
|
6
|
+
"test"
|
|
7
|
+
],
|
|
8
|
+
"compilerOptions": {
|
|
9
|
+
"baseUrl": ".",
|
|
10
|
+
"paths": {
|
|
11
|
+
"*": [
|
|
12
|
+
"types/*"
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
/* Basic Options */
|
|
17
|
+
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
|
|
18
|
+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
|
19
|
+
"lib": [
|
|
20
|
+
"dom",
|
|
21
|
+
"es2015",
|
|
22
|
+
"es2016",
|
|
23
|
+
"es2017"
|
|
24
|
+
], /* Specify library files to be included in the compilation. */
|
|
25
|
+
// "allowJs": true, /* Allow javascript files to be compiled. */
|
|
26
|
+
// "checkJs": true, /* Report errors in .js files. */
|
|
27
|
+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
|
28
|
+
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
|
29
|
+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
30
|
+
"sourceMap": true, /* Generates corresponding '.map' file. */
|
|
31
|
+
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
32
|
+
"outDir": "dist", /* Redirect output structure to the directory. */
|
|
33
|
+
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
|
34
|
+
// "composite": true, /* Enable project compilation */
|
|
35
|
+
// "removeComments": true, /* Do not emit comments to output. */
|
|
36
|
+
// "noEmit": true, /* Do not emit outputs. */
|
|
37
|
+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
|
38
|
+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
|
39
|
+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
|
40
|
+
/* Strict Type-Checking Options */
|
|
41
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
42
|
+
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
|
|
43
|
+
// "strictNullChecks": true, /* Enable strict null checks. */
|
|
44
|
+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
|
45
|
+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
|
46
|
+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
|
47
|
+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
|
48
|
+
/* Additional Checks */
|
|
49
|
+
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
|
50
|
+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
|
51
|
+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
|
52
|
+
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
|
53
|
+
/* Module Resolution Options */
|
|
54
|
+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
|
55
|
+
// "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
|
|
56
|
+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
|
57
|
+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
|
58
|
+
"typeRoots": [
|
|
59
|
+
"node_modules"
|
|
60
|
+
], /* List of folders to include type definitions from. */
|
|
61
|
+
"types": [
|
|
62
|
+
"node"
|
|
63
|
+
], /* Type declaration files to be included in compilation. */
|
|
64
|
+
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
|
65
|
+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
|
66
|
+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
|
67
|
+
/* Source Map Options */
|
|
68
|
+
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
|
69
|
+
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
70
|
+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
|
71
|
+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
|
72
|
+
/* Experimental Options */
|
|
73
|
+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
|
74
|
+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
|
75
|
+
/* Advanced Options */
|
|
76
|
+
// "declarationDir": "lib" /* Output directory for generated declaration files. */
|
|
77
|
+
}
|
|
78
|
+
}
|