Skip to content

Commit f98a442

Browse files
authored
FFM-10924 Streaming disconnect log level changes (#125)
1 parent 53da920 commit f98a442

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

Diff for: examples/preact/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/react-redux/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: examples/react/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@harnessio/ff-javascript-client-sdk",
3-
"version": "1.26.1",
3+
"version": "1.26.2",
44
"author": "Harness",
55
"license": "Apache-2.0",
66
"main": "dist/sdk.cjs.js",

Diff for: src/stream.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class Streamer {
1111
private readTimeoutCheckerId: any
1212
private connectionOpened = false
1313
private disconnectEventEmitted = false
14+
private reconnectAttempts = 0
1415

1516
constructor(
1617
private eventBus: Emitter,
@@ -40,22 +41,31 @@ export class Streamer {
4041
const onConnected = () => {
4142
this.logDebugMessage('Stream connected')
4243
this.eventBus.emit(Event.CONNECTED)
44+
this.reconnectAttempts = 0
4345
}
4446

4547
const onDisconnect = () => {
4648
clearInterval(this.readTimeoutCheckerId)
4749
const reconnectDelayMs = getRandom(1000, 10000)
50+
this.reconnectAttempts++
4851
this.logDebugMessage('Stream disconnected, will reconnect in ' + reconnectDelayMs + 'ms')
4952
if (!this.disconnectEventEmitted) {
5053
this.eventBus.emit(Event.DISCONNECTED)
5154
this.disconnectEventEmitted = true
5255
}
56+
57+
if (this.reconnectAttempts >= 5 && this.reconnectAttempts % 5 === 0) {
58+
this.logErrorMessage(
59+
`Reconnection failed after ${this.reconnectAttempts} attempts; attempting further reconnections.`
60+
)
61+
}
62+
5363
setTimeout(() => this.start(), reconnectDelayMs)
5464
}
5565

5666
const onFailed = (msg: string) => {
5767
if (!!msg) {
58-
this.logErrorMessage('Stream has issue', msg)
68+
this.logDebugMessage('Stream has issue', msg)
5969
}
6070

6171
// Fallback to polling while we have a stream failure
@@ -97,19 +107,11 @@ export class Streamer {
97107
onFailed('SSE timeout')
98108
}
99109

100-
// XMLHttpRequest doesn't fire an `onload` event when used to open an SSE connection, but leaving this listener
101-
// here, in case there are some edge cases where it's fired and so need to handle the reconnect.
110+
// XMLHttpRequest fires `onload` when a request completes successfully, meaning the entire content has been downloaded.
111+
// For SSE, if it fires it indicates an invalid state and we should reconnect
102112
this.xhr.onload = () => {
103-
if (this.xhr.status >= 400 && this.xhr.status <= 599) {
104-
onFailed(`HTTP code ${this.xhr.status}`)
105-
return
106-
}
107-
108-
if (!this.connectionOpened) {
109-
onConnected()
110-
this.connectionOpened = true
111-
this.disconnectEventEmitted = false
112-
}
113+
onFailed(`Received XMLHttpRequest onLoad event: ${this.xhr.status}`)
114+
return
113115
}
114116

115117
let offset = 0
@@ -135,7 +137,7 @@ export class Streamer {
135137
this.readTimeoutCheckerId = setInterval(() => {
136138
// this task will kill and restart the SSE connection if no data or heartbeat has arrived in a while
137139
if (lastActivity < Date.now() - SSE_TIMEOUT_MS) {
138-
this.logErrorMessage('SSE read timeout')
140+
this.logDebugMessage('SSE read timeout')
139141
this.xhr.abort()
140142
}
141143
}, SSE_TIMEOUT_MS)

0 commit comments

Comments
 (0)