-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify streaming reconnection (#4797)
* init commit * add back public declarations * use asyncwait, no arrow function
- Loading branch information
1 parent
3092347
commit 5abee16
Showing
2 changed files
with
45 additions
and
85 deletions.
There are no files selected for viewing
56 changes: 20 additions & 36 deletions
56
web-common/src/runtime-client/exponential-backoff-tracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,36 @@ | ||
import { asyncWait } from "@rilldata/web-common/lib/waitUtils"; | ||
import { asyncWait } from "../lib/waitUtils"; | ||
|
||
export class ExponentialBackoffTracker { | ||
private curTime: number; | ||
private curRetries = 0; | ||
private trackerPeriod: number; | ||
private currentDelay: number; | ||
|
||
public constructor( | ||
private readonly retries: number, | ||
/** | ||
* Time period within which to trigger the tracker. | ||
* Any failure after this will be considered as an intermittent failure. | ||
*/ | ||
private readonly trackerTriggerPeriod: number, | ||
// time | ||
private readonly waitPeriod: number, | ||
private readonly maxRetries: number, | ||
private readonly initialDelay: number, | ||
) { | ||
this.trackerPeriod = trackerTriggerPeriod; | ||
this.currentDelay = initialDelay; | ||
} | ||
|
||
public static createBasicTracker() { | ||
return new ExponentialBackoffTracker(5, 1000, 250); | ||
return new ExponentialBackoffTracker(5, 1000); | ||
} | ||
|
||
public async failed(): Promise<boolean> { | ||
const lastTime = this.curTime; | ||
this.curTime = Date.now(); | ||
public async try(fn: () => Promise<void> | void) { | ||
try { | ||
await fn(); | ||
|
||
// if failed after the tracker period, reset everything | ||
if (this.curTime - lastTime >= this.trackerPeriod) { | ||
this.reset(); | ||
await asyncWait(this.waitPeriod); | ||
return true; | ||
} | ||
|
||
// if retry count has reached max return false to stop the connection | ||
if (this.curRetries === this.retries) return false; | ||
this.curRetries = 0; | ||
this.currentDelay = this.initialDelay; | ||
} catch (e) { | ||
if (this.curRetries >= this.maxRetries) { | ||
throw e; | ||
} | ||
|
||
// increment retry and update the tracker periods. | ||
this.curRetries++; | ||
// A simple function to increase the tracking period. | ||
this.trackerPeriod = this.trackerTriggerPeriod * 2 ** this.curRetries; | ||
// multiply the retires to the wait period as well. | ||
await asyncWait(this.waitPeriod * 2 ** this.curRetries); | ||
return true; | ||
} | ||
this.currentDelay = this.initialDelay * 2 ** this.curRetries; | ||
await asyncWait(this.currentDelay); | ||
|
||
private reset() { | ||
this.curRetries = 0; | ||
this.trackerPeriod = this.trackerTriggerPeriod; | ||
this.curRetries++; | ||
return this.try(fn); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5abee16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://ui.rilldata.in as production
🚀 Deployed on https://663e24c0519b9c00ab6dd95a--rill-ui-dev.netlify.app