dht-rpc 6.26.2 → 6.26.4

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 -2
  2. package/lib/health.js +46 -26
  3. package/package.json +4 -4
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, opts)
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,16 @@
1
1
  const MAX_HEALTH_WINDOW = 4
2
- const TIMEOUTS_THRESHOLD = 0.5
2
+ const OFFLINE_THRESHOLD = 2
3
3
  const IDLE_THRESHOLD = 4
4
+ const DEGRADED_TIMEOUT_RATE_THRESHOLD = 0.5
4
5
 
5
6
  module.exports = class NetworkHealth {
6
- static DEFAULT_MAX_HEALTH_WINDOW = MAX_HEALTH_WINDOW
7
-
8
- constructor(dht, { maxHealthWindow = MAX_HEALTH_WINDOW } = {}) {
7
+ constructor(dht) {
9
8
  this._dht = dht
10
- this._maxHealthWindow = maxHealthWindow
11
9
  this._window = []
12
10
  this._head = -1
11
+ this._offlineTicks = 0
12
+ this._degradedTicks = 0
13
+ this._healthyTicks = 0
13
14
  this.online = true
14
15
  this.degraded = false
15
16
  }
@@ -19,44 +20,34 @@ module.exports = class NetworkHealth {
19
20
  }
20
21
 
21
22
  get previous() {
22
- return this._window[(this._head - 1 + this._maxHealthWindow) % this._maxHealthWindow]
23
+ return this._window[(this._head - 1 + MAX_HEALTH_WINDOW) % MAX_HEALTH_WINDOW]
23
24
  }
24
25
 
25
26
  get newest() {
26
27
  return this._window[this._head]
27
28
  }
28
29
 
29
- get recentResponses() {
30
+ get responses() {
30
31
  if (!this.newest || !this.previous) return 0
31
32
  return this.newest.responses - this.previous.responses
32
33
  }
33
34
 
34
- get recentTimeouts() {
35
+ get timeouts() {
35
36
  if (!this.newest || !this.previous) return 0
36
37
  return this.newest.timeouts - this.previous.timeouts
37
38
  }
38
39
 
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
40
  get timeoutsRate() {
50
41
  if (this.timeouts === 0) return 0
51
42
  return this.timeouts / (this.responses + this.timeouts)
52
43
  }
53
44
 
54
45
  get cold() {
55
- return this._window.length < this._maxHealthWindow
46
+ return this._window.length < MAX_HEALTH_WINDOW
56
47
  }
57
48
 
58
49
  get idle() {
59
- return this.recentResponses + this.recentTimeouts < IDLE_THRESHOLD
50
+ return this.responses + this.timeouts < IDLE_THRESHOLD
60
51
  }
61
52
 
62
53
  get stats() {
@@ -67,33 +58,62 @@ module.exports = class NetworkHealth {
67
58
  idle: this.idle,
68
59
  responses: this.responses,
69
60
  timeouts: this.timeouts,
70
- timeoutsRate: this.timeoutsRate,
71
- recentResponses: this.recentResponses,
72
- recentTimeouts: this.recentTimeouts
61
+ timeoutsRate: this.timeoutsRate
73
62
  }
74
63
  }
75
64
 
76
65
  get _tail() {
77
- return (this._head + 1) % this._maxHealthWindow
66
+ return (this._head + 1) % MAX_HEALTH_WINDOW
78
67
  }
79
68
 
80
69
  reset() {
81
70
  this._window = []
82
71
  this._head = -1
72
+ this._offlineTicks = 0
73
+ this._degradedTicks = 0
74
+ this._healthyTicks = 0
75
+ this.online = true
76
+ this.degraded = false
77
+ this._dht._online()
83
78
  }
84
79
 
85
80
  update() {
81
+ // update counters before dropping oldest
82
+ if (this.oldest?.degraded) this._degradedTicks--
83
+ else if (this.oldest?.degraded === false) this._healthyTicks--
84
+
85
+ // move window
86
86
  this._head = this._tail
87
87
  this._window[this._head] = {
88
88
  responses: this._dht.stats.requests.responses,
89
89
  timeouts: this._dht.stats.requests.timeouts
90
90
  }
91
91
 
92
+ // skip state changes
92
93
  if (this.cold || this.idle) return
93
94
 
94
- this.online = this.recentResponses > 0
95
- this.degraded = this.online && this.timeoutsRate > TIMEOUTS_THRESHOLD
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
+ }
107
+
108
+ // check online/offline
109
+ if (!this.newest.offline) this.online = true
110
+ else if (this._offlineTicks >= OFFLINE_THRESHOLD) this.online = false
111
+
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
96
115
 
116
+ // fire events
97
117
  if (this.online && !this.degraded) this._dht._online()
98
118
  else if (this.degraded) this._dht._degraded()
99
119
  else this._dht._offline()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dht-rpc",
3
- "version": "6.26.2",
3
+ "version": "6.26.4",
4
4
  "description": "Make RPC calls over a Kademlia based DHT",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -43,12 +43,12 @@
43
43
  },
44
44
  "repository": {
45
45
  "type": "git",
46
- "url": "https://github.com/mafintosh/dht-rpc.git"
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/mafintosh/dht-rpc/issues"
51
+ "url": "https://github.com/holepunchto/dht-rpc/issues"
52
52
  },
53
- "homepage": "https://github.com/mafintosh/dht-rpc"
53
+ "homepage": "https://github.com/holepunchto/dht-rpc#"
54
54
  }