-
-
Notifications
You must be signed in to change notification settings - Fork 101
feat: websocket reconnect #405
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
Changes from 14 commits
584e1f1
a8dbdfd
8592261
c6071e7
b827e9b
54ad7a4
9350edc
7dd97b9
b0a3dae
ae2a31b
8f9501f
01d02b1
e13f2fd
ac1225a
bfaf30a
51f447d
78597c6
08d7394
08bcc07
83047a9
2610677
15e6ba8
bd35703
b0f65c2
3a51c6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const DEFAULT_PING_INTERVAL = 30_000 | ||
const DEFAULT_MAX_RECONNECTION_RETRIES = Infinity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Max retries should be limited by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intention is to keep the connection alive, assuming the error messages are being monitored, that's why I set Infinity We can remove the default value as Infinity, but in that case we can't set an arbitrary default value, it really depends by the context, ping interval, reconnection delay and so on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Those should be limited, possibly to 30 seconds. |
||
const DEFAULT_RECONNECT_INTERVAL = 1_000 | ||
const DEFAULT_RECONNECT_DECAY = 1.5 | ||
const DEFAULT_CONNECTION_TIMEOUT = 5_000 | ||
const DEFAULT_RECONNECT_ON_CLOSE = false | ||
const DEFAULT_LOGS = false | ||
|
||
function validateOptions (options) { | ||
if (!options.upstream && !options.websocket && !((options.upstream === '' || options.wsUpstream === '') && options.replyOptions && typeof options.replyOptions.getUpstream === 'function')) { | ||
throw new Error('upstream must be specified') | ||
} | ||
|
||
if (options.wsReconnect) { | ||
const wsReconnect = options.wsReconnect | ||
|
||
if (wsReconnect.pingInterval !== undefined && (typeof wsReconnect.pingInterval !== 'number' || wsReconnect.pingInterval < 0)) { | ||
throw new Error('wsReconnect.pingInterval must be a non-negative number') | ||
} | ||
wsReconnect.pingInterval = wsReconnect.pingInterval ?? DEFAULT_PING_INTERVAL | ||
|
||
if (wsReconnect.maxReconnectionRetries !== undefined && (typeof wsReconnect.maxReconnectionRetries !== 'number' || wsReconnect.maxReconnectionRetries < 1)) { | ||
throw new Error('wsReconnect.maxReconnectionRetries must be a number greater than or equal to 1') | ||
} | ||
wsReconnect.maxReconnectionRetries = wsReconnect.maxReconnectionRetries ?? DEFAULT_MAX_RECONNECTION_RETRIES | ||
|
||
if (wsReconnect.reconnectInterval !== undefined && (typeof wsReconnect.reconnectInterval !== 'number' || wsReconnect.reconnectInterval < 100)) { | ||
throw new Error('wsReconnect.reconnectInterval (ms) must be a number greater than or equal to 100') | ||
} | ||
wsReconnect.reconnectInterval = wsReconnect.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL | ||
|
||
if (wsReconnect.reconnectDecay !== undefined && (typeof wsReconnect.reconnectDecay !== 'number' || wsReconnect.reconnectDecay < 1)) { | ||
throw new Error('wsReconnect.reconnectDecay must be a number greater than or equal to 1') | ||
} | ||
wsReconnect.reconnectDecay = wsReconnect.reconnectDecay ?? DEFAULT_RECONNECT_DECAY | ||
|
||
if (wsReconnect.connectionTimeout !== undefined && (typeof wsReconnect.connectionTimeout !== 'number' || wsReconnect.connectionTimeout < 0)) { | ||
throw new Error('wsReconnect.connectionTimeout must be a non-negative number') | ||
} | ||
wsReconnect.connectionTimeout = wsReconnect.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT | ||
|
||
if (wsReconnect.reconnectOnClose !== undefined && typeof wsReconnect.reconnectOnClose !== 'boolean') { | ||
throw new Error('wsReconnect.reconnectOnClose must be a boolean') | ||
} | ||
wsReconnect.reconnectOnClose = wsReconnect.reconnectOnClose ?? DEFAULT_RECONNECT_ON_CLOSE | ||
|
||
if (wsReconnect.logs !== undefined && typeof wsReconnect.logs !== 'boolean') { | ||
throw new Error('wsReconnect.logs must be a boolean') | ||
} | ||
wsReconnect.logs = wsReconnect.logs ?? DEFAULT_LOGS | ||
} | ||
|
||
return options | ||
} | ||
|
||
module.exports = { | ||
validateOptions, | ||
DEFAULT_PING_INTERVAL, | ||
DEFAULT_MAX_RECONNECTION_RETRIES, | ||
DEFAULT_RECONNECT_INTERVAL, | ||
DEFAULT_RECONNECT_DECAY, | ||
DEFAULT_CONNECTION_TIMEOUT, | ||
DEFAULT_RECONNECT_ON_CLOSE, | ||
DEFAULT_LOGS | ||
} |
Uh oh!
There was an error while loading. Please reload this page.