dht-rpc 6.26.3 → 6.27.0
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/lib/health.js +25 -14
- package/lib/peer.js +2 -3
- package/package.json +5 -5
package/lib/health.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const MAX_HEALTH_WINDOW = 4
|
|
2
|
+
const OFFLINE_THRESHOLD = 2
|
|
2
3
|
const IDLE_THRESHOLD = 4
|
|
3
4
|
const DEGRADED_TIMEOUT_RATE_THRESHOLD = 0.5
|
|
4
5
|
|
|
@@ -7,6 +8,7 @@ module.exports = class NetworkHealth {
|
|
|
7
8
|
this._dht = dht
|
|
8
9
|
this._window = []
|
|
9
10
|
this._head = -1
|
|
11
|
+
this._offlineTicks = 0
|
|
10
12
|
this._degradedTicks = 0
|
|
11
13
|
this._healthyTicks = 0
|
|
12
14
|
this.online = true
|
|
@@ -48,14 +50,6 @@ module.exports = class NetworkHealth {
|
|
|
48
50
|
return this.responses + this.timeouts < IDLE_THRESHOLD
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
get allDegraded() {
|
|
52
|
-
return this._degradedTicks === MAX_HEALTH_WINDOW
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
get allHealthy() {
|
|
56
|
-
return this._healthyTicks === MAX_HEALTH_WINDOW
|
|
57
|
-
}
|
|
58
|
-
|
|
59
53
|
get stats() {
|
|
60
54
|
return {
|
|
61
55
|
online: this.online,
|
|
@@ -75,6 +69,7 @@ module.exports = class NetworkHealth {
|
|
|
75
69
|
reset() {
|
|
76
70
|
this._window = []
|
|
77
71
|
this._head = -1
|
|
72
|
+
this._offlineTicks = 0
|
|
78
73
|
this._degradedTicks = 0
|
|
79
74
|
this._healthyTicks = 0
|
|
80
75
|
this.online = true
|
|
@@ -83,26 +78,42 @@ module.exports = class NetworkHealth {
|
|
|
83
78
|
}
|
|
84
79
|
|
|
85
80
|
update() {
|
|
81
|
+
// update counters before dropping oldest
|
|
86
82
|
if (this.oldest?.degraded) this._degradedTicks--
|
|
87
83
|
else if (this.oldest?.degraded === false) this._healthyTicks--
|
|
88
84
|
|
|
85
|
+
// move window
|
|
89
86
|
this._head = this._tail
|
|
90
87
|
this._window[this._head] = {
|
|
91
88
|
responses: this._dht.stats.requests.responses,
|
|
92
89
|
timeouts: this._dht.stats.requests.timeouts
|
|
93
90
|
}
|
|
94
91
|
|
|
92
|
+
// skip state changes
|
|
95
93
|
if (this.cold || this.idle) return
|
|
96
94
|
|
|
97
|
-
|
|
95
|
+
// update newest
|
|
96
|
+
this.newest.offline = this.responses === 0
|
|
97
|
+
this.newest.degraded =
|
|
98
|
+
!this.newest.offline && this.timeoutsRate > DEGRADED_TIMEOUT_RATE_THRESHOLD
|
|
99
|
+
|
|
100
|
+
// update counters
|
|
101
|
+
if (this.newest.offline) this._offlineTicks++
|
|
102
|
+
else {
|
|
103
|
+
this._offlineTicks = 0
|
|
104
|
+
if (this.newest.degraded) this._degradedTicks++
|
|
105
|
+
else this._healthyTicks++
|
|
106
|
+
}
|
|
98
107
|
|
|
99
|
-
|
|
100
|
-
|
|
108
|
+
// check online/offline
|
|
109
|
+
if (!this.newest.offline) this.online = true
|
|
110
|
+
else if (this._offlineTicks >= OFFLINE_THRESHOLD) this.online = false
|
|
101
111
|
|
|
102
|
-
|
|
103
|
-
if (this.online
|
|
104
|
-
if (
|
|
112
|
+
// check degraded
|
|
113
|
+
if (!this.online || this._healthyTicks === MAX_HEALTH_WINDOW) this.degraded = false
|
|
114
|
+
else if (this._degradedTicks === MAX_HEALTH_WINDOW) this.degraded = true
|
|
105
115
|
|
|
116
|
+
// fire events
|
|
106
117
|
if (this.online && !this.degraded) this._dht._online()
|
|
107
118
|
else if (this.degraded) this._dht._degraded()
|
|
108
119
|
else this._dht._offline()
|
package/lib/peer.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
const sodium = require('sodium-universal')
|
|
2
2
|
const c = require('compact-encoding')
|
|
3
|
-
const net = require('compact-encoding-net')
|
|
4
3
|
const b4a = require('b4a')
|
|
5
4
|
|
|
6
5
|
const ipv4 = {
|
|
7
|
-
...
|
|
6
|
+
...c.ipv4Address,
|
|
8
7
|
decode(state) {
|
|
9
|
-
const ip =
|
|
8
|
+
const ip = c.ipv4Address.decode(state)
|
|
10
9
|
return {
|
|
11
10
|
id: null, // populated by the callee
|
|
12
11
|
host: ip.host,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dht-rpc",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.27.0",
|
|
4
4
|
"description": "Make RPC calls over a Kademlia based DHT",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"adaptive-timeout": "^1.0.1",
|
|
18
18
|
"b4a": "^1.6.1",
|
|
19
19
|
"bare-events": "^2.2.0",
|
|
20
|
-
"compact-encoding": "^
|
|
20
|
+
"compact-encoding": "^3.0.0",
|
|
21
21
|
"compact-encoding-net": "^1.2.0",
|
|
22
22
|
"fast-fifo": "^1.1.0",
|
|
23
23
|
"kademlia-routing-table": "^1.0.1",
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
},
|
|
44
44
|
"repository": {
|
|
45
45
|
"type": "git",
|
|
46
|
-
"url": "https://github.com/
|
|
46
|
+
"url": "https://github.com/holepunchto/dht-rpc.git"
|
|
47
47
|
},
|
|
48
48
|
"author": "Mathias Buus (@mafintosh)",
|
|
49
49
|
"license": "MIT",
|
|
50
50
|
"bugs": {
|
|
51
|
-
"url": "https://github.com/
|
|
51
|
+
"url": "https://github.com/holepunchto/dht-rpc/issues"
|
|
52
52
|
},
|
|
53
|
-
"homepage": "https://github.com/
|
|
53
|
+
"homepage": "https://github.com/holepunchto/dht-rpc#"
|
|
54
54
|
}
|