hypercore 10.29.1 → 10.30.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.
Files changed (2) hide show
  1. package/lib/replicator.js +23 -7
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -11,6 +11,7 @@ const caps = require('./caps')
11
11
  const DEFAULT_MAX_INFLIGHT = [32, 512]
12
12
  const SCALE_LATENCY = 50
13
13
  const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
14
+ const BLOCK_TIMEOUT = 5000
14
15
 
15
16
  const PRIORITY = {
16
17
  NORMAL: 0,
@@ -208,6 +209,7 @@ class InflightTracker {
208
209
  const id = this._free.length ? this._free.pop() : this._requests.push(null)
209
210
 
210
211
  req.id = id
212
+ req.timeout = setTimeout(onblocktimeout, BLOCK_TIMEOUT, req)
211
213
  this._requests[id - 1] = req
212
214
  return req
213
215
  }
@@ -218,6 +220,9 @@ class InflightTracker {
218
220
 
219
221
  remove (id) {
220
222
  if (id <= this._requests.length) {
223
+ const req = this._requests[id - 1]
224
+ clearTimeout(req.timeout)
225
+ req.timeout = null
221
226
  this._requests[id - 1] = null
222
227
  this._free.push(id)
223
228
  }
@@ -785,6 +790,7 @@ class Peer {
785
790
  return {
786
791
  peer: this,
787
792
  id: 0,
793
+ timeout: null,
788
794
  fork: this.remoteFork,
789
795
  block: null,
790
796
  hash: null,
@@ -1059,12 +1065,22 @@ class Peer {
1059
1065
  }
1060
1066
  }
1061
1067
 
1068
+ isActive () {
1069
+ if (this.paused || this.removed) return false
1070
+ return true
1071
+ }
1072
+
1062
1073
  async _send (req) {
1063
1074
  const fork = this.core.tree.fork
1064
1075
 
1065
1076
  this.inflight++
1066
1077
  this.replicator._inflight.add(req)
1067
1078
 
1079
+ if (req.upgrade !== null && req.fork === fork) {
1080
+ const u = this.replicator._addUpgrade()
1081
+ u.inflight.push(req)
1082
+ }
1083
+
1068
1084
  try {
1069
1085
  if (req.block !== null && req.fork === fork) {
1070
1086
  req.block.nodes = await this.core.tree.missingNodes(2 * req.block.index)
@@ -1084,11 +1100,6 @@ class Peer {
1084
1100
  return
1085
1101
  }
1086
1102
 
1087
- if (req.upgrade !== null && req.fork === fork) {
1088
- const u = this.replicator._addUpgrade()
1089
- u.inflight.push(req)
1090
- }
1091
-
1092
1103
  this.wireRequest.send(req)
1093
1104
  }
1094
1105
  }
@@ -1414,6 +1425,7 @@ module.exports = class Replicator {
1414
1425
 
1415
1426
  _removePeer (peer) {
1416
1427
  this.peers.splice(this.peers.indexOf(peer), 1)
1428
+ peer.removed = true
1417
1429
 
1418
1430
  if (this._manifestPeer === peer) this._manifestPeer = null
1419
1431
 
@@ -1765,7 +1777,7 @@ module.exports = class Replicator {
1765
1777
  }
1766
1778
 
1767
1779
  _updatePeer (peer) {
1768
- if (peer.paused || peer.inflight >= peer.getMaxInflight()) {
1780
+ if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
1769
1781
  return false
1770
1782
  }
1771
1783
 
@@ -1797,7 +1809,7 @@ module.exports = class Replicator {
1797
1809
  }
1798
1810
 
1799
1811
  _updatePeerNonPrimary (peer) {
1800
- if (peer.paused || peer.inflight >= peer.getMaxInflight()) {
1812
+ if (!peer.isActive() || peer.inflight >= peer.getMaxInflight()) {
1801
1813
  return false
1802
1814
  }
1803
1815
 
@@ -2062,3 +2074,7 @@ function onwirerange (m, c) {
2062
2074
  function onwireextension (m, c) {
2063
2075
  return c.userData.onextension(m)
2064
2076
  }
2077
+
2078
+ function onblocktimeout (req) {
2079
+ if (req.peer.isActive()) req.peer.stream.destroy(REQUEST_TIMEOUT())
2080
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.29.1",
3
+ "version": "10.30.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {