hypercore 11.39.1 → 11.39.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.
- package/index.js +2 -2
- package/lib/session-state.js +44 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -709,9 +709,9 @@ class Hypercore extends EventEmitter {
|
|
|
709
709
|
return this.opening
|
|
710
710
|
}
|
|
711
711
|
|
|
712
|
-
async recover() {
|
|
712
|
+
async recover(opts) {
|
|
713
713
|
if (this.opened === false) await this.opening
|
|
714
|
-
return this.state.waitForRecovery(this, false)
|
|
714
|
+
return this.state.waitForRecovery(this, false, opts)
|
|
715
715
|
}
|
|
716
716
|
|
|
717
717
|
async setUserData(key, value) {
|
package/lib/session-state.js
CHANGED
|
@@ -6,13 +6,19 @@ const quickbit = require('quickbit-universal')
|
|
|
6
6
|
const HypercoreStorage = require('hypercore-storage')
|
|
7
7
|
|
|
8
8
|
const {
|
|
9
|
+
BAD_ARGUMENT,
|
|
9
10
|
INVALID_OPERATION,
|
|
10
11
|
INVALID_SIGNATURE,
|
|
11
12
|
ASSERTION,
|
|
13
|
+
REQUEST_TIMEOUT,
|
|
12
14
|
SESSION_CLOSED,
|
|
13
15
|
SESSION_NOT_WRITABLE
|
|
14
16
|
} = require('hypercore-errors')
|
|
15
17
|
|
|
18
|
+
const RECOVERY_RETRY = 5000 // between looks for a recovery peer
|
|
19
|
+
const RECOVERY_POLL = 250 // between checks of the recovery peers
|
|
20
|
+
const RECOVERY_SETTLE = 8000 // for which the recovery peers must have nothing more for us
|
|
21
|
+
|
|
16
22
|
const Mutex = require('./mutex')
|
|
17
23
|
const Bitfield = require('./bitfield')
|
|
18
24
|
const { MerkleTree, MerkleTreeBatch } = require('./merkle-tree')
|
|
@@ -611,23 +617,36 @@ class SessionState {
|
|
|
611
617
|
}
|
|
612
618
|
}
|
|
613
619
|
|
|
614
|
-
//
|
|
615
|
-
async waitForRecovery(session, writable) {
|
|
620
|
+
// waits until the recovery peers have nothing more for us
|
|
621
|
+
async waitForRecovery(session, writable, { timeout = 0, settle = RECOVERY_SETTLE } = {}) {
|
|
622
|
+
if (!writable && !this.isDefault()) {
|
|
623
|
+
throw BAD_ARGUMENT('Only the default session can recover', this.discoveryKey)
|
|
624
|
+
}
|
|
625
|
+
if (!writable && !SessionState.RECOVERY_PEERS) {
|
|
626
|
+
throw BAD_ARGUMENT('No recovery peers configured', this.discoveryKey)
|
|
627
|
+
}
|
|
628
|
+
|
|
616
629
|
const length = this.length
|
|
617
630
|
const replicator = this.core.replicator
|
|
631
|
+
const deadline = timeout > 0 ? Date.now() + timeout : 0
|
|
632
|
+
const polls = Math.max(1, Math.ceil(settle / RECOVERY_POLL))
|
|
618
633
|
|
|
619
634
|
while (session === null ? this.core.closing === null : session.closing === null) {
|
|
635
|
+
if (deadline !== 0 && Date.now() >= deadline) {
|
|
636
|
+
throw REQUEST_TIMEOUT('Recovery timed out', this.discoveryKey)
|
|
637
|
+
}
|
|
638
|
+
|
|
620
639
|
let peers = getRecoveryPeers(replicator.peers)
|
|
621
640
|
|
|
622
641
|
if (peers.length < 1) {
|
|
623
|
-
await
|
|
642
|
+
await sleepOrClose(bounded(RECOVERY_RETRY, deadline), session)
|
|
624
643
|
continue
|
|
625
644
|
}
|
|
626
645
|
|
|
627
646
|
let restart = false
|
|
628
647
|
|
|
629
|
-
for (let i = 0; i <
|
|
630
|
-
await
|
|
648
|
+
for (let i = 0; i < polls; i++) {
|
|
649
|
+
await sleepOrClose(bounded(RECOVERY_POLL, deadline), session)
|
|
631
650
|
|
|
632
651
|
peers = getRecoveryPeers(replicator.peers)
|
|
633
652
|
|
|
@@ -644,6 +663,8 @@ class SessionState {
|
|
|
644
663
|
throw SESSION_NOT_WRITABLE('core recovered during append')
|
|
645
664
|
}
|
|
646
665
|
|
|
666
|
+
if (!this.core.header.hints.recovering) return
|
|
667
|
+
|
|
647
668
|
this.core.header.hints.recovering = 0
|
|
648
669
|
this.core.hintsChanged = true
|
|
649
670
|
|
|
@@ -1380,6 +1401,24 @@ function isRootIndex(index, roots) {
|
|
|
1380
1401
|
return false
|
|
1381
1402
|
}
|
|
1382
1403
|
|
|
1404
|
+
// wakes early when the session closes, so recovery tears down at once
|
|
1405
|
+
function sleepOrClose(ms, session) {
|
|
1406
|
+
return new Promise((resolve) => {
|
|
1407
|
+
const timer = setTimeout(done, ms)
|
|
1408
|
+
if (session !== null) session.once('close', done)
|
|
1409
|
+
|
|
1410
|
+
function done() {
|
|
1411
|
+
clearTimeout(timer)
|
|
1412
|
+
if (session !== null) session.off('close', done)
|
|
1413
|
+
resolve()
|
|
1414
|
+
}
|
|
1415
|
+
})
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
function bounded(ms, deadline) {
|
|
1419
|
+
return deadline === 0 ? ms : Math.max(0, Math.min(ms, deadline - Date.now()))
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1383
1422
|
function getRecoveryPeers(peers) {
|
|
1384
1423
|
if (!SessionState.RECOVERY_PEERS) return peers
|
|
1385
1424
|
|