Skip to content

Commit 82dc07a

Browse files
authored
FFM-8116 Stop SSE when Client.close() is called (#72)
* FFM-8116 Import abort-controller * FFM-8116 Add abort request logic * FFM-8116 Format * FFM-8116 Update var name * FFM-8116 Tidyup * FFM-8116 Tidyup * FFM-8116 Add backwards compat solution for closing stream * FFM-8116 Add backwards compat solution for closing stream * FFM-8116 Add backwards compat solution for closing stream * FFM-8116 Comment * FFM-8116 Prettier * FFM-8116 Prettier * FFM-8116 1.2.17 release prep * FFM-8116 Use destroy()
1 parent 5c4d8cc commit 82dc07a

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@harnessio/ff-nodejs-server-sdk",
3-
"version": "1.2.16",
3+
"version": "1.2.17",
44
"description": "Feature flags SDK for NodeJS environments",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.mjs",

src/streaming.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Repository } from './repository';
66
import { ConsoleLog } from './log';
77

88
import https, { RequestOptions } from 'https';
9-
import http from 'http';
9+
import http, { ClientRequest } from 'http';
1010

1111
type FetchFunction = (
1212
identifier: string,
@@ -17,6 +17,7 @@ type FetchFunction = (
1717
export class StreamProcessor {
1818
static readonly CONNECTED = 1;
1919
static readonly RETRYING = 2;
20+
static readonly CLOSED = 3;
2021
static readonly SSE_TIMEOUT_MS = 30000;
2122

2223
private readonly apiKey: string;
@@ -28,6 +29,7 @@ export class StreamProcessor {
2829
private readonly retryDelayMs: number;
2930

3031
private options: Options;
32+
private request: ClientRequest;
3133
private eventBus: EventEmitter;
3234
private readyState: number;
3335
private log: ConsoleLog;
@@ -82,17 +84,19 @@ export class StreamProcessor {
8284
};
8385

8486
const onFailed = (msg: string) => {
85-
this.retryAttempt += 1;
86-
87-
const delayMs = this.getRandomRetryDelayMs();
88-
this.log.warn(`SSE disconnected: ${msg} will retry in ${delayMs}ms`);
89-
this.readyState = StreamProcessor.RETRYING;
90-
this.eventBus.emit(StreamEvent.RETRYING);
91-
92-
setTimeout(() => {
93-
this.log.info('SSE retrying to connect');
94-
this.connect(url, options, onConnected, onFailed);
95-
}, delayMs);
87+
if (this.readyState !== StreamProcessor.CLOSED) {
88+
this.retryAttempt += 1;
89+
90+
const delayMs = this.getRandomRetryDelayMs();
91+
this.log.warn(`SSE disconnected: ${msg} will retry in ${delayMs}ms`);
92+
this.readyState = StreamProcessor.RETRYING;
93+
this.eventBus.emit(StreamEvent.RETRYING);
94+
95+
setTimeout(() => {
96+
this.log.info('SSE retrying to connect');
97+
this.connect(url, options, onConnected, onFailed);
98+
}, delayMs);
99+
}
96100
};
97101

98102
this.connect(url, options, onConnected, onFailed);
@@ -119,7 +123,7 @@ export class StreamProcessor {
119123
const isSecure = url.startsWith('https:');
120124
this.log.debug('SSE HTTP start request', url);
121125

122-
(isSecure ? https : http)
126+
this.request = (isSecure ? https : http)
123127
.request(url, options, (res) => {
124128
this.log.debug('SSE got HTTP response code', res.statusCode);
125129

@@ -146,8 +150,8 @@ export class StreamProcessor {
146150
'ms',
147151
);
148152
})
149-
.setTimeout(StreamProcessor.SSE_TIMEOUT_MS)
150-
.end();
153+
.setTimeout(StreamProcessor.SSE_TIMEOUT_MS);
154+
this.request.end();
151155
}
152156

153157
private processData(data: any): void {
@@ -212,14 +216,19 @@ export class StreamProcessor {
212216
return this.readyState === StreamProcessor.CONNECTED;
213217
}
214218

215-
stop(): void {
216-
this.log.info('Stopping StreamProcessor');
217-
this.eventBus.emit(StreamEvent.DISCONNECTED);
218-
}
219-
220219
close(): void {
220+
if (this.readyState === StreamProcessor.CLOSED) {
221+
this.log.info('SteamProcessor already closed');
222+
return;
223+
}
224+
225+
this.readyState = StreamProcessor.CLOSED;
221226
this.log.info('Closing StreamProcessor');
222-
this.stop();
227+
228+
this.request.destroy();
229+
this.request = undefined;
230+
231+
this.eventBus.emit(StreamEvent.DISCONNECTED);
223232
this.log.info('StreamProcessor closed');
224233
}
225234
}

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const VERSION = '1.2.16';
1+
export const VERSION = "1.2.17";

0 commit comments

Comments
 (0)