Skip to content

Commit

Permalink
feat(reconnection strategy): add randomBackOffStrategy
Browse files Browse the repository at this point in the history
* feat(reconnection strategy): add `randomBackOffStrategy`

* bump version + update changelog
  • Loading branch information
claylaut committed Dec 5, 2017
1 parent d6a81f3 commit 31714c5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="0.3.3"></a>
## [0.3.3](https://github.com/sketch7/signalr-client/compare/0.3.2...0.3.3) (2017-12-05)


### Features

* **reconnection strategy:** add `randomBackOffStrategy` ([3bd046c](https://github.com/sketch7/signalr-client/commit/3bd046c))



<a name="0.3.2"></a>
## [0.3.2](https://github.com/sketch7/signalr-client/compare/0.3.1...0.3.2) (2017-12-04)

Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ SignalR client library built on top of `@aspnet/signalr-client`. This gives you
* Update connection details easily without losing current connection state
* Subscriptions are handled through `RxJS` streams
* Reconnection strategies
* Random
* BackOff
* Custom
* Random strategy
* BackOff strategy
* Random BackOff strategy
* Custom strategy
* Auto re-subscriptions after getting disconnected and re-connected (***in development***)
* Contains minimal dependencies (`SignalR` and `RxJS` only)
* `No constraints` with any framework
Expand All @@ -36,6 +37,7 @@ SignalR client library built on top of `@aspnet/signalr-client`. This gives you
* Real world example (***coming soon***):
* Client: Angular
* Server: Microsoft Orleans integrated with SignalR
* [Angular basic example](#angular-basic-example)

## Installation

Expand Down Expand Up @@ -65,7 +67,7 @@ You're all set! Now it's fully integrated with your Angular application.

Continue from the [vanilla usage - step 2](#usage) onwards

***Example***
### Angular Basic Example
```ts
import { HubConnectionFactory } from "@ssv/signalr-client";

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ssv/signalr-client",
"version": "0.3.2",
"version": "0.3.3",
"description": "SignalR client library built on top of @aspnet/signalr-client. This gives you more features and easier to use.",
"keywords": [
"sketch7",
Expand Down
1 change: 1 addition & 0 deletions src/hub-connection.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ConnectionOptions extends IHubConnectionOptions {
export interface ReconnectionStrategyOptions {
maximumAttempts?: number;
customStrategy?: (retryOptions: ReconnectionStrategyOptions, retryCount: number) => number;
randomBackOffStrategy?: RandomStrategyOptions;
randomStrategy?: RandomStrategyOptions;
backOffStrategy?: BackOffStrategyOptions;
}
Expand Down
11 changes: 10 additions & 1 deletion src/reconnection-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export function getReconnectionDelay(retryOptions: ReconnectionStrategyOptions,
if (retryOptions.customStrategy) {
return retryOptions.customStrategy(retryOptions, retryCount);
}
if (retryOptions.randomBackOffStrategy) {
return randomBackOffStrategyDelay(retryOptions.randomBackOffStrategy, retryCount);
}
if (retryOptions.backOffStrategy) {
return backOffStrategyDelay(retryOptions.backOffStrategy, retryCount);
}
Expand All @@ -18,10 +21,16 @@ function randomStrategyDelay(randomStrategy: RandomStrategyOptions) {
return random(randomStrategy.min, randomStrategy.max) * randomStrategy.intervalMs;
}

function randomBackOffStrategyDelay(randomStrategy: RandomStrategyOptions, retryCount: number) {
let maxValue = Math.min(retryCount, randomStrategy.max);
maxValue = randomStrategy.min >= maxValue ? (maxValue + 2) : maxValue;
return random(randomStrategy.min, maxValue) * randomStrategy.intervalMs;
}

function backOffStrategyDelay(backOffStrategy: BackOffStrategyOptions, retryCount: number) {
return Math.min(retryCount * backOffStrategy.delayRetriesMs, backOffStrategy.maxDelayRetriesMs);
}

function defaultStrategy(retryCount: number) {
return backOffStrategyDelay({ delayRetriesMs: 1000, maxDelayRetriesMs: 15000 }, retryCount);
return randomBackOffStrategyDelay({ min: 3, max: 15, intervalMs: 1000 }, retryCount);
}

0 comments on commit 31714c5

Please sign in to comment.