-
Notifications
You must be signed in to change notification settings - Fork 1
/
base-client.ts
315 lines (258 loc) · 9.1 KB
/
base-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { EventEmitter } from 'events';
import {
Logger, isError, pDelay, once
} from '@terascope/job-components';
import type * as kafka from 'node-rdkafka';
import {
isOkayError,
wrapError,
isKafkaError,
AnyKafkaError,
} from '../_kafka_helpers';
import {
ERR__STATE
} from '../_kafka_helpers/error-codes';
export default class BaseClient<T extends kafka.Client<any>> {
/** the random factory of the back of interval, [min, max] */
static BACKOFF_RANDOM_FACTOR: [number, number] = [3, 9];
static DEFAULT_BACKOFF = 1000;
static DEFAULT_MAX_RETRIES = 3;
protected readonly _topic: string;
protected _closed = false;
protected _backoff: number = BaseClient.DEFAULT_BACKOFF;
protected _invalidStateCount = 0;
protected readonly _events = new EventEmitter();
protected readonly _logger: Logger;
protected readonly _client: T;
private _cleanup: CleanupFn[] = [];
private _connected = false;
constructor(client: T, topic: string, logger: Logger) {
this._topic = topic;
this._client = client;
this._logger = logger;
this._client.on('disconnected', () => {
this._connected = false;
if (this._closed) return;
this._incBackOff();
this._logger.warn('client unexpectedly disconnected');
});
// catch unwanted errors
this._client.on('error', this._logOrEmit('client:error'));
this._events.on('error', this._logOrEmit('client:error'));
}
/**
* Disconnect from Kafka and cleanup.
*/
async disconnect(): Promise<void> {
this._closed = true;
if (this.isConnected()) {
await new Promise((resolve, reject) => {
this._client.disconnect((err: AnyKafkaError) => {
if (err) reject(wrapError('Failed to disconnect', err));
else resolve();
});
});
}
for (const fn of this._cleanup) {
fn();
}
this._cleanup = [];
// For some reason the typing broke for this and need to have a hack for it
(this._client as any).removeAllListeners();
this._events.removeAllListeners();
}
isConnected(): boolean {
return this._connected && this._client.isConnected();
}
/**
* Connect to kafka but be double sure you are connected
*/
protected async _connect(): Promise<void> {
if (this._closed) {
throw new Error('Client is closed');
}
if (this.isConnected()) return;
await new Promise((resolve, reject) => {
const metadataOptions: kafka.MetadataOptions = {
topic: this._topic,
};
if (!this._topic) metadataOptions.allTopics = false;
this._client.connect(metadataOptions, (err: AnyKafkaError) => {
if (err) {
this._connected = false;
reject(wrapError('Failed to connect', err));
} else {
this._connected = true;
resolve();
}
});
});
this._logger.debug('Connected to kafka');
}
/**
* Make sure the event has a handler or is logged
*/
protected _logOrEmit(event: string, fn = () => {}) {
return (...args: any[]): void => {
fn();
const hasListener = this._events.listenerCount(event) > 0;
if (hasListener) {
this._events.emit(event, ...args);
return;
}
const [err] = args;
if (err && isError(err)) {
if (isOkayError(err, 'retryable')) {
this._logger.warn(err, `kafka client warning for event "${event}"`);
return;
}
if (!isKafkaError(err) || !isOkayError(err, 'any')) {
this._logger.error(err, `kafka client error for event "${event}"`);
return;
}
}
this._logger.debug(`kafka client debug for event "${event}"`, ...args);
};
}
/**
* A safe once event listener that will return an error first
* Guaranteed to call the callback at least once
* @returns an off function to the event listener
*/
protected _once(event: string, fn: (err: Error|null, ...args: any[]) => void): () => void {
let off: () => void;
const cb = once(fn);
const handler = (...args: any) => {
if (args[0] && isError(args[0])) {
cb(args[0]);
off();
return;
}
cb(null, ...args);
off();
};
this._events.once(event, handler);
off = once(() => {
this._events.removeListener(event, handler);
cb(null);
this._cleanup = this._cleanup.filter((f) => f !== off);
});
this._cleanup = [...this._cleanup, off];
return off;
}
/**
* A safe timeout, if the timeout fires the first arg will be an error
* Guaranteed to call the callback at least once
* @returns an off function to cleanup the timer
*/
protected _timeout(fn: (err: Error|null) => void, timeoutMs: number): () => void {
let off: () => void;
const cb = once(fn);
const timeout = setTimeout(() => {
const error = new Error(`Timeout of ${timeoutMs}ms`);
Error.captureStackTrace(error, this._timeout);
cb(error);
off();
}, timeoutMs);
off = once(() => {
clearTimeout(timeout);
cb(null);
this._cleanup = this._cleanup.filter((f) => f !== off);
});
this._cleanup = [...this._cleanup, off];
return off;
}
/**
* Perform an action, fail if the function fails,
* or the event emits an error
*/
protected async _failIfEvent<F extends TryFn>(event: string, fn: F, action = 'any'): RetryResult<F> {
return this._tryWithEvent(event, fn, action, 0);
}
/**
* Perform an action, retry if the function fails,
* or the event emits an error
*/
protected async _tryWithEvent<F extends TryFn>(
event: string,
fn: F,
action = 'any',
retries = BaseClient.DEFAULT_MAX_RETRIES
): RetryResult<F> {
let eventError: Error|null = null;
const off = this._once(event, (err) => {
eventError = err;
});
try {
return this._try(() => {
if (eventError) {
throw eventError;
}
return fn();
}, action, retries);
} finally {
off();
}
}
/**
* Try a async fn n times and back off after each attempt
*
* **NOTE:** It will only retry if it is a retryable kafka error
*/
protected async _try<F extends TryFn>(fn: F, action = 'any', retries = BaseClient.DEFAULT_MAX_RETRIES): RetryResult<F> {
const actionStr = action === 'any' ? '' : ` when performing ${action}`;
if (this._closed) {
this._logger.error(`Kafka client closed${actionStr}`);
return null;
}
await this._beforeTry();
try {
const result = await fn();
if (action !== 'connect') {
this._resetBackOff();
}
return result;
} catch (err) {
if (isOkayError(err, action)) {
return null;
}
// if get an invalid state, increase the count
// and if it is past the threshold,
if (err && err.code === ERR__STATE) {
this._incBackOff();
this._invalidStateCount++;
}
const isRetryableError = isOkayError(err, 'retryable');
if (retries > 0 && isRetryableError) {
this._incBackOff();
await pDelay(this._backoff);
this._logger.warn(err, `got retryable kafka${actionStr}, will retry in ${this._backoff}ms`);
return this._try(fn, action, retries - 1);
}
this._resetBackOff();
if (isRetryableError) {
throw wrapError(`Failure${actionStr} after retries`, err);
}
throw wrapError(`Failure${actionStr}`, err);
}
}
protected async _beforeTry(): Promise<void> {}
protected _incBackOff(): void {
const [min, max] = BaseClient.BACKOFF_RANDOM_FACTOR;
this._backoff += Math.floor(BaseClient.DEFAULT_BACKOFF * getRandom(min, max));
if (this._backoff > 60000) {
this._backoff = 60000;
}
}
protected _resetBackOff(): void {
this._backoff = BaseClient.DEFAULT_BACKOFF;
}
}
// get random number inclusive
export function getRandom(min: number, max: number): number {
// The maximum is inclusive and the minimum is inclusive
return Math.random() * (max - min + 1) + min;
}
type CleanupFn = () => void;
export type TryFn = () => any;
type RetryResult<T extends TryFn> = Promise<ReturnType<T>|null>;