dht-rpc 6.26.0 → 6.26.2

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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/lib/health.js +34 -30
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -179,7 +179,6 @@ class DHT extends EventEmitter {
179
179
  this._onwakeup()
180
180
  log('Resuming io')
181
181
  await this.io.resume()
182
- this.health.reset()
183
182
  log('Done, dht resumed')
184
183
  this.io.networkInterfaces.on('change', (interfaces) => this._onnetworkchange(interfaces))
185
184
  this.refresh()
@@ -557,6 +556,7 @@ class DHT extends EventEmitter {
557
556
  this._stableTicks = MORE_STABLE_TICKS
558
557
  this._refreshTicks = 1 // triggers a refresh next tick (allow network time to wake up also)
559
558
  this._lastHost = null // clear network cache check
559
+ this.health.reset()
560
560
 
561
561
  if (this.adaptive) {
562
562
  // TODO: re-enable this as soon as we find out why this is over triggering in some edge cases
package/lib/health.js CHANGED
@@ -1,7 +1,6 @@
1
- const MAX_HEALTH_WINDOW = 2
2
- const RESPONSES_SANITY = 0
3
- const TIMEOUTS_SANITY = 0
1
+ const MAX_HEALTH_WINDOW = 4
4
2
  const TIMEOUTS_THRESHOLD = 0.5
3
+ const IDLE_THRESHOLD = 4
5
4
 
6
5
  module.exports = class NetworkHealth {
7
6
  static DEFAULT_MAX_HEALTH_WINDOW = MAX_HEALTH_WINDOW
@@ -19,17 +18,31 @@ module.exports = class NetworkHealth {
19
18
  return this._window[this._tail]
20
19
  }
21
20
 
21
+ get previous() {
22
+ return this._window[(this._head - 1 + this._maxHealthWindow) % this._maxHealthWindow]
23
+ }
24
+
22
25
  get newest() {
23
26
  return this._window[this._head]
24
27
  }
25
28
 
29
+ get recentResponses() {
30
+ if (!this.newest || !this.previous) return 0
31
+ return this.newest.responses - this.previous.responses
32
+ }
33
+
34
+ get recentTimeouts() {
35
+ if (!this.newest || !this.previous) return 0
36
+ return this.newest.timeouts - this.previous.timeouts
37
+ }
38
+
26
39
  get responses() {
27
- if (!this.newest) return 0
40
+ if (!this.newest || !this.oldest) return 0
28
41
  return this.newest.responses - this.oldest.responses
29
42
  }
30
43
 
31
44
  get timeouts() {
32
- if (!this.newest) return 0
45
+ if (!this.newest || !this.oldest) return 0
33
46
  return this.newest.timeouts - this.oldest.timeouts
34
47
  }
35
48
 
@@ -38,13 +51,25 @@ module.exports = class NetworkHealth {
38
51
  return this.timeouts / (this.responses + this.timeouts)
39
52
  }
40
53
 
54
+ get cold() {
55
+ return this._window.length < this._maxHealthWindow
56
+ }
57
+
58
+ get idle() {
59
+ return this.recentResponses + this.recentTimeouts < IDLE_THRESHOLD
60
+ }
61
+
41
62
  get stats() {
42
63
  return {
43
64
  online: this.online,
44
65
  degraded: this.degraded,
66
+ cold: this.cold,
67
+ idle: this.idle,
45
68
  responses: this.responses,
46
69
  timeouts: this.timeouts,
47
- timeoutsRate: this.timeoutsRate
70
+ timeoutsRate: this.timeoutsRate,
71
+ recentResponses: this.recentResponses,
72
+ recentTimeouts: this.recentTimeouts
48
73
  }
49
74
  }
50
75
 
@@ -52,16 +77,9 @@ module.exports = class NetworkHealth {
52
77
  return (this._head + 1) % this._maxHealthWindow
53
78
  }
54
79
 
55
- get cold() {
56
- return this._window.length < this._maxHealthWindow
57
- }
58
-
59
80
  reset() {
60
81
  this._window = []
61
82
  this._head = -1
62
- this.online = true
63
- this.degraded = false
64
- this._dht._online()
65
83
  }
66
84
 
67
85
  update() {
@@ -71,24 +89,10 @@ module.exports = class NetworkHealth {
71
89
  timeouts: this._dht.stats.requests.timeouts
72
90
  }
73
91
 
74
- if (this.cold) return
92
+ if (this.cold || this.idle) return
75
93
 
76
- const responses = this.responses
77
- const timeouts = this.timeouts
78
- const timeoutsRate = this.timeoutsRate
79
-
80
- if (responses > 0) {
81
- this.online = true
82
- }
83
-
84
- if (responses > RESPONSES_SANITY * this._window.length) {
85
- this.degraded = timeoutsRate > TIMEOUTS_THRESHOLD
86
- }
87
-
88
- if (responses === 0 && timeouts > TIMEOUTS_SANITY * this._window.length) {
89
- this.online = false
90
- this.degraded = false
91
- }
94
+ this.online = this.recentResponses > 0
95
+ this.degraded = this.online && this.timeoutsRate > TIMEOUTS_THRESHOLD
92
96
 
93
97
  if (this.online && !this.degraded) this._dht._online()
94
98
  else if (this.degraded) this._dht._degraded()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dht-rpc",
3
- "version": "6.26.0",
3
+ "version": "6.26.2",
4
4
  "description": "Make RPC calls over a Kademlia based DHT",
5
5
  "main": "index.js",
6
6
  "files": [