websocket-ts 1.1.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +157 -93
- package/package.json +27 -14
- package/src/backoff/backoff.ts +21 -13
- package/src/backoff/constantbackoff.ts +29 -11
- package/src/backoff/exponentialbackoff.ts +68 -28
- package/src/backoff/linearbackoff.ts +77 -26
- package/src/index.ts +23 -9
- package/src/queue/array_queue.ts +41 -0
- package/src/queue/queue.ts +46 -0
- package/src/queue/ring_queue.ts +69 -0
- package/src/websocket.ts +463 -138
- package/src/websocket_buffer.ts +18 -0
- package/src/websocket_builder.ts +274 -0
- package/src/websocket_event.ts +88 -0
- package/src/websocket_options.ts +23 -0
- package/src/websocket_retry_options.ts +22 -0
- package/{tsconfig.json → tsconfig.cjs.json} +8 -8
- package/tsconfig.esm.json +71 -0
- package/jest.config.js +0 -9
- package/lib/backoff/backoff.d.ts +0 -18
- package/lib/backoff/backoff.d.ts.map +0 -1
- package/lib/backoff/backoff.js +0 -3
- package/lib/backoff/backoff.js.map +0 -1
- package/lib/backoff/constantbackoff.d.ts +0 -11
- package/lib/backoff/constantbackoff.d.ts.map +0 -1
- package/lib/backoff/constantbackoff.js +0 -20
- package/lib/backoff/constantbackoff.js.map +0 -1
- package/lib/backoff/exponentialbackoff.d.ts +0 -22
- package/lib/backoff/exponentialbackoff.d.ts.map +0 -1
- package/lib/backoff/exponentialbackoff.js +0 -35
- package/lib/backoff/exponentialbackoff.js.map +0 -1
- package/lib/backoff/linearbackoff.d.ts +0 -19
- package/lib/backoff/linearbackoff.d.ts.map +0 -1
- package/lib/backoff/linearbackoff.js +0 -34
- package/lib/backoff/linearbackoff.js.map +0 -1
- package/lib/buffer/buffer.d.ts +0 -41
- package/lib/buffer/buffer.d.ts.map +0 -1
- package/lib/buffer/buffer.js +0 -3
- package/lib/buffer/buffer.js.map +0 -1
- package/lib/buffer/lrubuffer.d.ts +0 -24
- package/lib/buffer/lrubuffer.d.ts.map +0 -1
- package/lib/buffer/lrubuffer.js +0 -76
- package/lib/buffer/lrubuffer.js.map +0 -1
- package/lib/buffer/timebuffer.d.ts +0 -25
- package/lib/buffer/timebuffer.d.ts.map +0 -1
- package/lib/buffer/timebuffer.js +0 -92
- package/lib/buffer/timebuffer.js.map +0 -1
- package/lib/index.d.ts +0 -10
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -22
- package/lib/index.js.map +0 -1
- package/lib/websocket.d.ts +0 -47
- package/lib/websocket.d.ts.map +0 -1
- package/lib/websocket.js +0 -120
- package/lib/websocket.js.map +0 -1
- package/lib/websocketBuilder.d.ts +0 -32
- package/lib/websocketBuilder.d.ts.map +0 -1
- package/lib/websocketBuilder.js +0 -68
- package/lib/websocketBuilder.js.map +0 -1
- package/src/buffer/buffer.ts +0 -45
- package/src/buffer/lrubuffer.ts +0 -80
- package/src/buffer/timebuffer.ts +0 -105
- package/src/websocketBuilder.ts +0 -98
- package/test/backoff/constantbackoff.test.ts +0 -21
- package/test/backoff/exponentialbackoff.test.ts +0 -13
- package/test/backoff/linearbackoff.test.ts +0 -32
- package/test/buffer/common.ts +0 -10
- package/test/buffer/lrubuffer.test.ts +0 -128
- package/test/buffer/timebuffer.test.ts +0 -115
- package/test/websocket.test.ts +0 -341
- package/test/websocketBuilder.test.ts +0 -36
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,147 +1,211 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
<div>
|
|
2
|
+
<div align="center">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" />
|
|
4
|
+
</div>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="https://github.com/jjxxs/websocket-ts/actions/workflows/build.yml/badge.svg" alt="Build Status" />
|
|
7
|
+
<img src="https://github.com/jjxxs/websocket-ts/actions/workflows/test.yml/badge.svg" alt="Build Status" />
|
|
8
|
+
<a href="https://coveralls.io/github/jjxxs/websocket-ts?branch=master">
|
|
9
|
+
<img src="https://coveralls.io/repos/github/jjxxs/websocket-ts/badge.svg?branch=master&service=github" alt="Coverage Status" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/jjxxs/websocket-ts/releases/latest">
|
|
12
|
+
<img src="https://img.shields.io/github/v/release/jjxxs/websocket-ts" alt="Release" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="/LICENSE">
|
|
15
|
+
<img src="https://img.shields.io/github/license/jjxxs/websocket-ts" alt="License" />
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div align="center">
|
|
21
|
+
A <b>WebSocket</b> for browsers with <b>auto-reconnect</b> and <b>message buffering</b> written in <b>TypeScript</b>.
|
|
22
|
+
</div>
|
|
8
23
|
|
|
9
24
|
## Features
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- Builder-class for easy initialization and configuration
|
|
18
|
-
- High test-coverage and in-code documentation
|
|
19
|
-
- Enables you to easily modify and extend the code
|
|
25
|
+
|
|
26
|
+
- **Lightweight & Standalone**: No dependencies, 2.1 kB minified & gzipped.
|
|
27
|
+
- **Browser-native**: Utilizes WebSocket API, offers direct access.
|
|
28
|
+
- **Smart Reconnect**: Optional auto-reconnect and message buffering.
|
|
29
|
+
- **Easy Setup**: Optional builder class for quick initialization.
|
|
30
|
+
- **Well-Tested**: High test coverage, well-documented for extensibility.
|
|
31
|
+
- **Module Support**: Supports CommonJS, and ES6 modules.
|
|
20
32
|
|
|
21
33
|
## Installation
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
|
|
35
|
+
Install `websocket-ts` with npm:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
24
38
|
$ npm install websocket-ts
|
|
25
39
|
```
|
|
26
40
|
|
|
27
|
-
|
|
41
|
+
## Quickstart
|
|
42
|
+
This example shows how to use the package, complete with message buffering and automatic reconnection.
|
|
43
|
+
The created websocket will echo back any received messages. It will buffer messages when disconnected
|
|
44
|
+
and attempt to reconnect every 1 second.
|
|
45
|
+
|
|
28
46
|
```typescript
|
|
29
|
-
import {
|
|
47
|
+
import {
|
|
48
|
+
ArrayQueue,
|
|
49
|
+
ConstantBackoff,
|
|
50
|
+
Websocket,
|
|
51
|
+
WebsocketBuilder,
|
|
52
|
+
WebsocketEvent,
|
|
53
|
+
} from "websocket-ts";
|
|
54
|
+
|
|
55
|
+
// Initialize WebSocket with buffering and 1s reconnection delay
|
|
56
|
+
const ws = new WebsocketBuilder("ws://localhost:8080")
|
|
57
|
+
.withBuffer(new ArrayQueue()) // buffer messages when disconnected
|
|
58
|
+
.withBackoff(new ConstantBackoff(1000)) // retry every 1s
|
|
59
|
+
.build();
|
|
60
|
+
|
|
61
|
+
// Function to output & echo received messages
|
|
62
|
+
const echoOnMessage = (i: Websocket, ev: MessageEvent) => {
|
|
63
|
+
console.log(`received message: ${ev.data}`);
|
|
64
|
+
i.send(`echo: ${ev.data}`);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Add event listeners
|
|
68
|
+
ws.addEventListener(WebsocketEvent.open, () => console.log("opened!"));
|
|
69
|
+
ws.addEventListener(WebsocketEvent.close, () => console.log("closed!"));
|
|
70
|
+
ws.addEventListener(WebsocketEvent.message, echoOnMessage);
|
|
30
71
|
```
|
|
31
72
|
|
|
32
73
|
## Usage
|
|
74
|
+
This will demonstrate how to use `websocket-ts` in your project using the provided `WebsocketBuild`-class.
|
|
75
|
+
|
|
76
|
+
For a more detailed description of the API, please refer to the [API Documentation](https://jjxxs.github.io/websocket-ts/).
|
|
33
77
|
|
|
34
78
|
#### Initialization
|
|
35
|
-
|
|
79
|
+
|
|
80
|
+
Create a new instance with the `WebsocketBuilder`:
|
|
36
81
|
|
|
37
82
|
```typescript
|
|
38
|
-
const ws = new WebsocketBuilder(
|
|
83
|
+
const ws = new WebsocketBuilder("ws://localhost:42421").build();
|
|
39
84
|
```
|
|
40
85
|
|
|
41
|
-
#### Events
|
|
42
|
-
|
|
86
|
+
#### Events
|
|
87
|
+
|
|
88
|
+
There are six events which can be subscribed to through with event listeners:
|
|
89
|
+
|
|
43
90
|
```typescript
|
|
44
|
-
export enum
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
91
|
+
export enum WebsocketEvent {
|
|
92
|
+
open = "open", // Connection opened
|
|
93
|
+
close = "close", // Connection closed
|
|
94
|
+
error = "error", // Error-induced closure
|
|
95
|
+
message = "message", // Message received
|
|
96
|
+
retry = "retry", // Reconnect attempt
|
|
97
|
+
reconnect = "reconnect" // Successful reconnect
|
|
50
98
|
}
|
|
51
99
|
```
|
|
52
|
-
The callbacks are called with the issuing websocket-instance and the causing event as arguments:
|
|
53
|
-
```typescript
|
|
54
|
-
const ws = new WebsocketBuilder('ws://localhost:42421')
|
|
55
|
-
.onOpen((i, ev) => { console.log("opened") })
|
|
56
|
-
.onClose((i, ev) => { console.log("closed") })
|
|
57
|
-
.onError((i, ev) => { console.log("error") })
|
|
58
|
-
.onMessage((i, ev) => { console.log("message") })
|
|
59
|
-
.onRetry((i, ev) => { console.log("retry") })
|
|
60
|
-
.build();
|
|
61
|
-
```
|
|
62
100
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
101
|
+
#### Add Event Listeners
|
|
102
|
+
Event listeners receive the websocket instance (`i`) and the triggering event (`ev`) as arguments.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
106
|
+
.onOpen((i, ev) => console.log("opened"))
|
|
107
|
+
.onClose((i, ev) => console.log("closed"))
|
|
108
|
+
.onError((i, ev) => console.log("error"))
|
|
109
|
+
.onMessage((i, ev) => console.log("message"))
|
|
110
|
+
.onRetry((i, ev) => console.log("retry"))
|
|
111
|
+
.onReconnect((i, ev) => console.log("reconnect"))
|
|
112
|
+
.build();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Remove Event Listeners
|
|
116
|
+
|
|
117
|
+
To unregister a specific event listener, use `removeEventListener`:
|
|
71
118
|
|
|
72
|
-
You can remove event-listener with `removeEventListener`:
|
|
73
119
|
```typescript
|
|
74
120
|
let ws: Websocket
|
|
75
121
|
/* ... */
|
|
76
|
-
ws.removeEventListener(
|
|
122
|
+
ws.removeEventListener(WebsocketEvent.open, openEventListener);
|
|
77
123
|
```
|
|
78
124
|
|
|
79
|
-
#### Send
|
|
80
|
-
|
|
125
|
+
#### Send Message
|
|
126
|
+
|
|
127
|
+
Use the `send` method to send a message to the server:
|
|
128
|
+
|
|
81
129
|
```typescript
|
|
82
130
|
let ws: Websocket;
|
|
83
131
|
/* ... */
|
|
84
132
|
ws.send("Hello World!");
|
|
85
133
|
```
|
|
86
134
|
|
|
87
|
-
#### Reconnect & Backoff
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
135
|
+
#### Reconnect & Backoff (Optional)
|
|
136
|
+
|
|
137
|
+
If you'd like the websocket to automatically reconnect upon disconnection, you can optionally provide a `Backoff` strategy.
|
|
138
|
+
This sets the delay between reconnection attempts. There are three built-in `Backoff` implementations, or you can create
|
|
139
|
+
your own by implementing the `Backoff` interface. If no Backoff is provided, the websocket will not attempt to reconnect.
|
|
91
140
|
|
|
92
141
|
##### ConstantBackoff
|
|
93
|
-
|
|
94
|
-
|
|
142
|
+
|
|
143
|
+
The `ConstantBackoff` strategy enforces a fixed delay between each reconnection attempt.
|
|
144
|
+
To set a constant 1-second wait time, use:
|
|
145
|
+
|
|
95
146
|
```typescript
|
|
96
|
-
const ws
|
|
97
|
-
|
|
98
|
-
|
|
147
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
148
|
+
.withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
|
|
149
|
+
.build();
|
|
99
150
|
```
|
|
100
151
|
|
|
101
152
|
##### LinearBackoff
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
153
|
+
|
|
154
|
+
The `LinearBackoff` strategy increases the delay between reconnection attempts linearly,
|
|
155
|
+
up to an optional maximum. For example, to start with a 0-second delay and increase by
|
|
156
|
+
10 second for each retry, capping at 60 seconds, use:
|
|
157
|
+
|
|
105
158
|
```typescript
|
|
106
|
-
const ws
|
|
107
|
-
|
|
108
|
-
|
|
159
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
160
|
+
.withBackoff(new LinearBackoff(0, 10000, 60000)) // 0ms, 10s, 20s, 30s, 40s, 50s, 60s
|
|
161
|
+
.build();
|
|
109
162
|
```
|
|
110
163
|
|
|
111
164
|
##### ExponentialBackoff
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
165
|
+
|
|
166
|
+
The `ExponentialBackoff` strategy doubles the delay between each reconnection attempt, up
|
|
167
|
+
to a specified maximum. This approach is inspired by the binary exponential backoff algorithm
|
|
168
|
+
commonly used in networking. For example, to generate a backoff series like `[1s, 2s, 4s, 8s]`, use:
|
|
169
|
+
|
|
115
170
|
```typescript
|
|
116
|
-
const ws
|
|
117
|
-
|
|
118
|
-
|
|
171
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
172
|
+
.withBackoff(new ExponentialBackoff(1000, 6)) // 1s, 2s, 4s, 8s, 16s, 32s, 64s
|
|
173
|
+
.build();
|
|
119
174
|
```
|
|
120
175
|
|
|
121
|
-
#### Buffer
|
|
176
|
+
#### Buffer (Optional)
|
|
177
|
+
|
|
178
|
+
To buffer outgoing messages when the websocket is disconnected, you can optionally specify
|
|
179
|
+
a `Queue`. This queue will temporarily store your messages and send them in sequence when
|
|
180
|
+
the websocket (re)connects. Two built-in `Queue` implementations are available, or you can
|
|
181
|
+
create your own by implementing the `Queue` interface. If no queue is provided, messages
|
|
182
|
+
won't be buffered.
|
|
183
|
+
|
|
184
|
+
##### RingQueue
|
|
122
185
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
186
|
+
The `RingQueue` is a fixed-capacity, first-in-first-out (FIFO) queue. When it reaches capacity,
|
|
187
|
+
the oldest element is removed to accommodate new ones. Reading from the queue returns and
|
|
188
|
+
removes the oldest element. For instance, to set up a `RingQueue` with a 100-element capacity,
|
|
189
|
+
use:
|
|
127
190
|
|
|
128
|
-
##### LRUBuffer
|
|
129
|
-
The `LRUBuffer` keeps the last `n` messages. When the buffer is full, the oldest message in the buffer will be replaced.
|
|
130
|
-
It uses an array as a circular-buffer for linear space- and time-requirements. To use the `LRUBuffer` with a capacity of `1000`:
|
|
131
191
|
```typescript
|
|
132
|
-
const ws = new WebsocketBuilder(
|
|
133
|
-
|
|
134
|
-
|
|
192
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
193
|
+
.withBuffer(new RingQueue(100))
|
|
194
|
+
.build();
|
|
135
195
|
```
|
|
136
196
|
|
|
137
|
-
#####
|
|
138
|
-
|
|
139
|
-
|
|
197
|
+
##### ArrayQueue
|
|
198
|
+
|
|
199
|
+
The ArrayQueue offers an unbounded capacity, functioning as a first-in-first-out (FIFO) queue.
|
|
200
|
+
Reading from this queue returns and removes the oldest element. To use an `ArrayQueue`, use:
|
|
201
|
+
|
|
140
202
|
```typescript
|
|
141
|
-
const ws = new WebsocketBuilder(
|
|
142
|
-
|
|
143
|
-
|
|
203
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
204
|
+
.withBuffer(new ArrayQueue())
|
|
205
|
+
.build();
|
|
144
206
|
```
|
|
145
207
|
|
|
146
|
-
|
|
147
|
-
|
|
208
|
+
## Build & Tests
|
|
209
|
+
|
|
210
|
+
To compile the project, execute `npm run build`. The codebase includes unit tests for all
|
|
211
|
+
components. To run these tests, use `npm run test`.
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "websocket-ts",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "
|
|
5
|
-
"types": "
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"main": "dist/cjs/src/index.js",
|
|
5
|
+
"types": "dist/cjs/src/index.d.ts",
|
|
6
|
+
"module": "dist/esm/src/index.js",
|
|
6
7
|
"license": "MIT",
|
|
7
8
|
"keywords": [
|
|
8
9
|
"websocket",
|
|
@@ -12,22 +13,34 @@
|
|
|
12
13
|
"reconnecting",
|
|
13
14
|
"buffered"
|
|
14
15
|
],
|
|
16
|
+
"sideEffects": false,
|
|
15
17
|
"repository": {
|
|
16
18
|
"url": "github:jjxxs/websocket-ts"
|
|
17
19
|
},
|
|
18
20
|
"scripts": {
|
|
19
|
-
"build": "tsc -p .",
|
|
20
|
-
"
|
|
21
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
22
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
23
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
21
24
|
"test": "jest",
|
|
22
|
-
"
|
|
25
|
+
"test:coverage": "jest --coverage",
|
|
26
|
+
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
27
|
+
"lint": "cross-env-shell eslint './src/{**/*,*}.{js,ts}' './tests/{**/*,*}.{js,ts}'"
|
|
23
28
|
},
|
|
24
29
|
"devDependencies": {
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
|
31
|
+
"@typescript-eslint/parser": "^6.6.0",
|
|
32
|
+
"eslint-config-prettier": "^9.0.0",
|
|
33
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
34
|
+
"coveralls": "^3.1.1",
|
|
35
|
+
"eslint": "^8.49.0",
|
|
36
|
+
"@types/jest": "^29.5.4",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
39
|
+
"ts-jest": "^29.1.1",
|
|
40
|
+
"ts-node": "^10.9.1",
|
|
41
|
+
"typescript": "^5.2.2",
|
|
42
|
+
"@types/ws": "^8.5.5",
|
|
43
|
+
"ws": "^7.0.0",
|
|
44
|
+
"cross-env": "^7.0.3"
|
|
32
45
|
}
|
|
33
|
-
}
|
|
46
|
+
}
|
package/src/backoff/backoff.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Backoff
|
|
3
|
-
*
|
|
4
|
-
* exponentially increasing etc.
|
|
2
|
+
* A Backoff produces a series of numbers that are used to determine
|
|
3
|
+
* the delay between connection-retries. Values are expected to be in milliseconds.
|
|
5
4
|
*/
|
|
6
5
|
export interface Backoff {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
next(): number
|
|
6
|
+
/**
|
|
7
|
+
* The number of retries. Starts at 0, increases by 1 for each call to next(). Resets to 0 when reset() is called.
|
|
8
|
+
*/
|
|
9
|
+
readonly retries: number;
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Current number in the series.
|
|
13
|
+
*/
|
|
14
|
+
readonly current: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Advances the series to the next number and returns it.
|
|
18
|
+
* @return the next number in the series
|
|
19
|
+
*/
|
|
20
|
+
next(): number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resets the series to its initial state.
|
|
24
|
+
*/
|
|
25
|
+
reset(): void;
|
|
18
26
|
}
|
|
@@ -1,20 +1,38 @@
|
|
|
1
|
-
import {Backoff} from "./backoff";
|
|
1
|
+
import { Backoff } from "./backoff";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* ConstantBackoff always returns the same
|
|
4
|
+
* ConstantBackoff always returns the same backoff-time.
|
|
5
5
|
*/
|
|
6
6
|
export class ConstantBackoff implements Backoff {
|
|
7
|
-
|
|
7
|
+
private readonly backoff: number;
|
|
8
|
+
private _retries: number = 0;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new ConstantBackoff.
|
|
12
|
+
* @param backoff the backoff-time to return
|
|
13
|
+
*/
|
|
14
|
+
constructor(backoff: number) {
|
|
15
|
+
if (!Number.isInteger(backoff) || backoff < 0) {
|
|
16
|
+
throw new Error("Backoff must be a positive integer");
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
19
|
+
this.backoff = backoff;
|
|
20
|
+
}
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
get retries(): number {
|
|
23
|
+
return this._retries;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get current(): number {
|
|
27
|
+
return this.backoff;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
next(): number {
|
|
31
|
+
this._retries++;
|
|
32
|
+
return this.backoff;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
reset(): void {
|
|
36
|
+
this._retries = 0;
|
|
37
|
+
}
|
|
20
38
|
}
|
|
@@ -1,38 +1,78 @@
|
|
|
1
|
-
import {Backoff} from "./backoff";
|
|
1
|
+
import { Backoff } from "./backoff";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* ExponentialBackoff
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* ExponentialBackoff increases the backoff-time exponentially.
|
|
5
|
+
* An optional maximum can be provided as an upper bound to the
|
|
6
|
+
* exponent and thus to the returned backoff.
|
|
7
7
|
*
|
|
8
|
-
* The
|
|
9
|
-
*
|
|
8
|
+
* The series can be described as ('i' is the current step/retry):
|
|
9
|
+
* backoff = base * 2^i | without bound
|
|
10
|
+
* backoff = base * 2^min(i, expMax) | with bound
|
|
10
11
|
*
|
|
11
|
-
* Example:
|
|
12
|
-
*
|
|
12
|
+
* Example:
|
|
13
|
+
*
|
|
14
|
+
* 1) Without bound:
|
|
15
|
+
* base = 1000, expMax = undefined
|
|
16
|
+
* backoff = 1000 * 2^0 = 1000 // first retry
|
|
17
|
+
* backoff = 1000 * 2^1 = 2000 // second retry
|
|
18
|
+
* backoff = 1000 * 2^2 = 4000 // ...doubles with every retry
|
|
19
|
+
* backoff = 1000 * 2^3 = 8000
|
|
20
|
+
* backoff = 1000 * 2^4 = 16000
|
|
21
|
+
* ... // and so on
|
|
22
|
+
*
|
|
23
|
+
* 2) With bound:
|
|
24
|
+
* base = 1000, expMax = 3
|
|
25
|
+
* backoff = 1000 * 2^0 = 1000 // first retry
|
|
26
|
+
* backoff = 1000 * 2^1 = 2000 // second retry
|
|
27
|
+
* backoff = 1000 * 2^2 = 4000 // third retry
|
|
28
|
+
* backoff = 1000 * 2^3 = 8000 // maximum reached, don't increase further
|
|
29
|
+
* backoff = 1000 * 2^3 = 8000
|
|
30
|
+
* backoff = 1000 * 2^3 = 8000
|
|
31
|
+
* ... // and so on
|
|
13
32
|
*/
|
|
14
33
|
export class ExponentialBackoff implements Backoff {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
34
|
+
private readonly base: number;
|
|
35
|
+
private readonly expMax?: number;
|
|
36
|
+
private i: number;
|
|
37
|
+
private _retries: number = 0;
|
|
19
38
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new ExponentialBackoff.
|
|
41
|
+
* @param base the base of the exponentiation
|
|
42
|
+
* @param expMax the maximum exponent, no bound if undefined
|
|
43
|
+
*/
|
|
44
|
+
constructor(base: number, expMax?: number) {
|
|
45
|
+
if (!Number.isInteger(base) || base < 0) {
|
|
46
|
+
throw new Error("Base must be a positive integer or zero");
|
|
25
47
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const backoff = this.current;
|
|
29
|
-
if (this.expMax > this.expCurrent++)
|
|
30
|
-
this.current = this.current * 2;
|
|
31
|
-
return backoff;
|
|
48
|
+
if (expMax !== undefined && (!Number.isInteger(expMax) || expMax < 0)) {
|
|
49
|
+
throw new Error("ExpMax must be a undefined, a positive integer or zero");
|
|
32
50
|
}
|
|
33
51
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
this.base = base;
|
|
53
|
+
this.expMax = expMax;
|
|
54
|
+
this.i = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get retries() {
|
|
58
|
+
return this._retries;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get current(): number {
|
|
62
|
+
return this.base * Math.pow(2, this.i);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
next(): number {
|
|
66
|
+
this._retries++;
|
|
67
|
+
this.i =
|
|
68
|
+
this.expMax === undefined
|
|
69
|
+
? this.i + 1
|
|
70
|
+
: Math.min(this.i + 1, this.expMax);
|
|
71
|
+
return this.current;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
reset(): void {
|
|
75
|
+
this._retries = 0;
|
|
76
|
+
this.i = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|