Skip to content

FFM-10924 Streaming disconnect log level changes #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/preact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/react-redux/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harnessio/ff-javascript-client-sdk",
"version": "1.26.1",
"version": "1.26.2",
"author": "Harness",
"license": "Apache-2.0",
"main": "dist/sdk.cjs.js",
Expand Down
30 changes: 16 additions & 14 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Streamer {
private readTimeoutCheckerId: any
private connectionOpened = false
private disconnectEventEmitted = false
private reconnectAttempts = 0

constructor(
private eventBus: Emitter,
Expand Down Expand Up @@ -40,22 +41,31 @@ export class Streamer {
const onConnected = () => {
this.logDebugMessage('Stream connected')
this.eventBus.emit(Event.CONNECTED)
this.reconnectAttempts = 0
}

const onDisconnect = () => {
clearInterval(this.readTimeoutCheckerId)
const reconnectDelayMs = getRandom(1000, 10000)
this.reconnectAttempts++
this.logDebugMessage('Stream disconnected, will reconnect in ' + reconnectDelayMs + 'ms')
if (!this.disconnectEventEmitted) {
this.eventBus.emit(Event.DISCONNECTED)
this.disconnectEventEmitted = true
}

if (this.reconnectAttempts >= 5 && this.reconnectAttempts % 5 === 0) {
this.logErrorMessage(
`Reconnection failed after ${this.reconnectAttempts} attempts; attempting further reconnections.`
)
}

setTimeout(() => this.start(), reconnectDelayMs)
}

const onFailed = (msg: string) => {
if (!!msg) {
this.logErrorMessage('Stream has issue', msg)
this.logDebugMessage('Stream has issue', msg)
}

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

// XMLHttpRequest doesn't fire an `onload` event when used to open an SSE connection, but leaving this listener
// here, in case there are some edge cases where it's fired and so need to handle the reconnect.
// XMLHttpRequest fires `onload` when a request completes successfully, meaning the entire content has been downloaded.
// For SSE, if it fires it indicates an invalid state and we should reconnect
this.xhr.onload = () => {
if (this.xhr.status >= 400 && this.xhr.status <= 599) {
onFailed(`HTTP code ${this.xhr.status}`)
return
}

if (!this.connectionOpened) {
onConnected()
this.connectionOpened = true
this.disconnectEventEmitted = false
}
onFailed(`Received XMLHttpRequest onLoad event: ${this.xhr.status}`)
return
}

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