Skip to content
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

Have timeout eat into reconnect delay #2318

Merged
merged 1 commit into from
Aug 27, 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
3 changes: 1 addition & 2 deletions shared/domains/chelonia/chelonia.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ export default (sbp('sbp/selectors/register', {
strictOrdering: false,
connectionOptions: {
maxRetries: Infinity, // See https://github.com/okTurtles/group-income/issues/1183
reconnectOnTimeout: true, // can be enabled since we are not doing auth via web sockets
timeout: 5000
reconnectOnTimeout: true // can be enabled since we are not doing auth via web sockets
},
hooks: {
preHandleEvent: null, // async (message: GIMessage) => {}
Expand Down
22 changes: 19 additions & 3 deletions shared/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const defaultOptions = {
// respond because of a failed authentication.
reconnectOnTimeout: false,
reconnectionDelayGrowFactor: 2,
timeout: 5000
timeout: 60000
}

// ====== Event name constants ====== //
Expand Down Expand Up @@ -48,6 +48,7 @@ export type Message = {

export type PubSubClient = {
connectionTimeoutID: TimeoutID | void,
connectionTimeUsed?: number,
+customEventHandlers: Object,
failedConnectionAttempts: number,
+isLocal: boolean,
Expand Down Expand Up @@ -350,6 +351,7 @@ const defaultClientEventHandlers = {
const client = this
const { options } = this

client.connectionTimeUsed = undefined
client.clearAllTimers()
sbp('okTurtles.events/emit', PUBSUB_RECONNECTION_SUCCEEDED, client)

Expand Down Expand Up @@ -536,8 +538,12 @@ const publicMethods = {
client.socket = new WebSocket(client.url)

if (client.options.timeout) {
const start = performance.now()
client.connectionTimeoutID = setTimeout(() => {
client.connectionTimeoutID = undefined
if (client.options.reconnectOnTimeout) {
client.connectionTimeUsed = performance.now() - start
}
client.socket?.close(4000, 'timeout')
}, client.options.timeout)
}
Expand Down Expand Up @@ -593,8 +599,18 @@ const publicMethods = {

const minDelay = minReconnectionDelay * reconnectionDelayGrowFactor ** client.failedConnectionAttempts
const maxDelay = minDelay * reconnectionDelayGrowFactor

return Math.min(maxReconnectionDelay, Math.round(minDelay + Math.random() * (maxDelay - minDelay)))
const connectionTimeUsed = client.connectionTimeUsed
client.connectionTimeUsed = undefined

return Math.min(
// See issue #1943: Have the connection time used 'eat into' the
// reconnection time used
Math.max(
minReconnectionDelay,
connectionTimeUsed ? maxReconnectionDelay - connectionTimeUsed : maxReconnectionDelay
),
Math.round(minDelay + (0, Math.random)() * (maxDelay - minDelay))
)
},

// Schedules a connection attempt to happen after a delay computed according to
Expand Down