-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
28 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ReconnectContext, ReconnectPolicy } from './ReconnectPolicy'; | ||
|
||
const DEFAULT_RETRY_DELAYS_IN_MS = [ | ||
0, | ||
300, | ||
2 * 2 * 300, | ||
3 * 3 * 300, | ||
4 * 4 * 300, | ||
5 * 5 * 300, | ||
6 * 6 * 300, | ||
7 * 7 * 300, | ||
8 * 8 * 300, | ||
9 * 9 * 300, | ||
]; | ||
|
||
class DefaultReconnectPolicy implements ReconnectPolicy { | ||
private readonly _retryDelays: number[]; | ||
|
||
constructor(retryDelays?: number[]) { | ||
this._retryDelays = retryDelays !== undefined ? [...retryDelays] : DEFAULT_RETRY_DELAYS_IN_MS; | ||
} | ||
|
||
public nextRetryDelayInMs(context: ReconnectContext): number | null { | ||
if (context.retryCount === this._retryDelays.length) return null; | ||
|
||
const retryDelay = this._retryDelays[context.retryCount]; | ||
if (context.retryCount <= 1) return retryDelay; | ||
|
||
return retryDelay + Math.random() * 1_000; | ||
} | ||
} | ||
|
||
export default DefaultReconnectPolicy; |
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** Controls reconnecting of the client */ | ||
export interface ReconnectPolicy { | ||
/** Called after disconnect was detected | ||
* | ||
* @returns {number | null} Amount of time in milliseconds to delay the next reconnect attempt, `null` signals to stop retrying. | ||
*/ | ||
nextRetryDelayInMs(context: ReconnectContext): number | null; | ||
} | ||
|
||
export interface ReconnectContext { | ||
/** | ||
* Number of failed reconnect attempts | ||
*/ | ||
readonly retryCount: number; | ||
|
||
/** | ||
* Elapsed amount of time in milliseconds since the disconnect. | ||
*/ | ||
readonly elapsedMs: number; | ||
|
||
/** | ||
* Reason for retrying | ||
*/ | ||
readonly retryReason?: Error; | ||
} |
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