dht-rpc 6.26.2 → 6.26.3
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/index.js +1 -2
- package/lib/health.js +35 -26
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -26,7 +26,6 @@ const OLD_NODE = 360 // if an node has been around more than 30 min we consider
|
|
|
26
26
|
const DEFAULTS = {
|
|
27
27
|
concurrency: 10,
|
|
28
28
|
maxWindow: IO.DEFAULT_MAX_WINDOW,
|
|
29
|
-
maxHealthWindow: NetworkHealth.DEFAULT_MAX_HEALTH_WINDOW,
|
|
30
29
|
maxPingDelay: 10_000
|
|
31
30
|
}
|
|
32
31
|
|
|
@@ -44,7 +43,7 @@ class DHT extends EventEmitter {
|
|
|
44
43
|
onresponse: this._onresponse.bind(this),
|
|
45
44
|
ontimeout: this._ontimeout.bind(this)
|
|
46
45
|
})
|
|
47
|
-
this.health = new NetworkHealth(this
|
|
46
|
+
this.health = new NetworkHealth(this)
|
|
48
47
|
|
|
49
48
|
this.concurrency = opts.concurrency || DEFAULTS.concurrency
|
|
50
49
|
this.maxPingDelay = opts.maxPingDelay || DEFAULTS.maxPingDelay
|
package/lib/health.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
const MAX_HEALTH_WINDOW = 4
|
|
2
|
-
const TIMEOUTS_THRESHOLD = 0.5
|
|
3
2
|
const IDLE_THRESHOLD = 4
|
|
3
|
+
const DEGRADED_TIMEOUT_RATE_THRESHOLD = 0.5
|
|
4
4
|
|
|
5
5
|
module.exports = class NetworkHealth {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(dht, { maxHealthWindow = MAX_HEALTH_WINDOW } = {}) {
|
|
6
|
+
constructor(dht) {
|
|
9
7
|
this._dht = dht
|
|
10
|
-
this._maxHealthWindow = maxHealthWindow
|
|
11
8
|
this._window = []
|
|
12
9
|
this._head = -1
|
|
10
|
+
this._degradedTicks = 0
|
|
11
|
+
this._healthyTicks = 0
|
|
13
12
|
this.online = true
|
|
14
13
|
this.degraded = false
|
|
15
14
|
}
|
|
@@ -19,44 +18,42 @@ module.exports = class NetworkHealth {
|
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
get previous() {
|
|
22
|
-
return this._window[(this._head - 1 +
|
|
21
|
+
return this._window[(this._head - 1 + MAX_HEALTH_WINDOW) % MAX_HEALTH_WINDOW]
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
get newest() {
|
|
26
25
|
return this._window[this._head]
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
get
|
|
28
|
+
get responses() {
|
|
30
29
|
if (!this.newest || !this.previous) return 0
|
|
31
30
|
return this.newest.responses - this.previous.responses
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
get
|
|
33
|
+
get timeouts() {
|
|
35
34
|
if (!this.newest || !this.previous) return 0
|
|
36
35
|
return this.newest.timeouts - this.previous.timeouts
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
get responses() {
|
|
40
|
-
if (!this.newest || !this.oldest) return 0
|
|
41
|
-
return this.newest.responses - this.oldest.responses
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
get timeouts() {
|
|
45
|
-
if (!this.newest || !this.oldest) return 0
|
|
46
|
-
return this.newest.timeouts - this.oldest.timeouts
|
|
47
|
-
}
|
|
48
|
-
|
|
49
38
|
get timeoutsRate() {
|
|
50
39
|
if (this.timeouts === 0) return 0
|
|
51
40
|
return this.timeouts / (this.responses + this.timeouts)
|
|
52
41
|
}
|
|
53
42
|
|
|
54
43
|
get cold() {
|
|
55
|
-
return this._window.length <
|
|
44
|
+
return this._window.length < MAX_HEALTH_WINDOW
|
|
56
45
|
}
|
|
57
46
|
|
|
58
47
|
get idle() {
|
|
59
|
-
return this.
|
|
48
|
+
return this.responses + this.timeouts < IDLE_THRESHOLD
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get allDegraded() {
|
|
52
|
+
return this._degradedTicks === MAX_HEALTH_WINDOW
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get allHealthy() {
|
|
56
|
+
return this._healthyTicks === MAX_HEALTH_WINDOW
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
get stats() {
|
|
@@ -67,22 +64,28 @@ module.exports = class NetworkHealth {
|
|
|
67
64
|
idle: this.idle,
|
|
68
65
|
responses: this.responses,
|
|
69
66
|
timeouts: this.timeouts,
|
|
70
|
-
timeoutsRate: this.timeoutsRate
|
|
71
|
-
recentResponses: this.recentResponses,
|
|
72
|
-
recentTimeouts: this.recentTimeouts
|
|
67
|
+
timeoutsRate: this.timeoutsRate
|
|
73
68
|
}
|
|
74
69
|
}
|
|
75
70
|
|
|
76
71
|
get _tail() {
|
|
77
|
-
return (this._head + 1) %
|
|
72
|
+
return (this._head + 1) % MAX_HEALTH_WINDOW
|
|
78
73
|
}
|
|
79
74
|
|
|
80
75
|
reset() {
|
|
81
76
|
this._window = []
|
|
82
77
|
this._head = -1
|
|
78
|
+
this._degradedTicks = 0
|
|
79
|
+
this._healthyTicks = 0
|
|
80
|
+
this.online = true
|
|
81
|
+
this.degraded = false
|
|
82
|
+
this._dht._online()
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
update() {
|
|
86
|
+
if (this.oldest?.degraded) this._degradedTicks--
|
|
87
|
+
else if (this.oldest?.degraded === false) this._healthyTicks--
|
|
88
|
+
|
|
86
89
|
this._head = this._tail
|
|
87
90
|
this._window[this._head] = {
|
|
88
91
|
responses: this._dht.stats.requests.responses,
|
|
@@ -91,8 +94,14 @@ module.exports = class NetworkHealth {
|
|
|
91
94
|
|
|
92
95
|
if (this.cold || this.idle) return
|
|
93
96
|
|
|
94
|
-
this.
|
|
95
|
-
|
|
97
|
+
this.newest.degraded = this.timeoutsRate > DEGRADED_TIMEOUT_RATE_THRESHOLD
|
|
98
|
+
|
|
99
|
+
if (this.newest.degraded) this._degradedTicks++
|
|
100
|
+
else this._healthyTicks++
|
|
101
|
+
|
|
102
|
+
this.online = this.responses > 0
|
|
103
|
+
if (this.online && this.allDegraded) this.degraded = true
|
|
104
|
+
if (!this.online || this.allHealthy) this.degraded = false
|
|
96
105
|
|
|
97
106
|
if (this.online && !this.degraded) this._dht._online()
|
|
98
107
|
else if (this.degraded) this._dht._degraded()
|