Skip to content

Commit

Permalink
close session only when peer sessions are all closed
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Sep 12, 2024
1 parent 465acc4 commit 5543a1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ module.exports = class Hypercore extends EventEmitter {
// check if there is still an active session
if (this.sessions.length || this.state.active > 0) {
// if this is the last session and we are auto closing, trigger that first to enforce error handling
if (this.sessions.length === 1 && this.state.active === 1 && this.autoClose) await this.sessions[0].close(err)
if (this.sessions.length === 1 && this.core.state.active === 1 && this.autoClose) await this.sessions[0].close(err)
// emit "fake" close as this is a session

this.emit('close', false)
Expand Down
4 changes: 3 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ class SessionState {

this.treeLength = treeLength

this.active = 1
this.active = 0

this._onflush = null
this._flushing = null
this._activeBatch = null

this.ref()
}

get isSnapshot () {
Expand Down
46 changes: 28 additions & 18 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,12 @@ class Peer {
const reopen = isRemote === true && this.remoteOpened === true && this.remoteDownloading === false &&
this.remoteUploading === true && this.replicator.downloading === true

if (this.useSession && !reopen && --this.replicator._peerSessions === 0) {
this.replicator._closeSession()
}

if (this.remoteOpened === false) {
this.replicator._ifAvailable--
this.replicator.updateAll()

if (this.useSession) this.replicator._closeSessionMaybe()

return
}

Expand Down Expand Up @@ -1776,6 +1775,12 @@ module.exports = class Replicator {
}

_addPeer (peer) {
if (peer.useSession) {
// session may have been unref'd underneath us
this.ensureSession()
this._peerSessions++
}

this._hadPeers = true
this.peers.push(peer)
this.updatePeer(peer)
Expand All @@ -1799,6 +1804,11 @@ module.exports = class Replicator {
this._clearRequest(peer, req)
}

if (peer.useSession) {
this._peerSessions--
this._closeSessionMaybe()
}

this.onpeerupdate(false, peer)
this.updateAll()
}
Expand Down Expand Up @@ -2275,7 +2285,9 @@ module.exports = class Replicator {
this._maybeResolveIfAvailableRanges()
}

_closeSession () {
_closeSessionMaybe () {
if (!this._hasSession || this._peerSessions > 0) return

this._hasSession = false
this.core.state.active--

Expand All @@ -2296,11 +2308,16 @@ module.exports = class Replicator {
return this._attached.has(protomux)
}

ensureSession () {
if (this._hasSession) return

this._hasSession = true
this.core.state.ref()
}

attachTo (protomux, useSession) {
if (useSession && !this._hasSession) {
this._hasSession = true
this.core.state.ref()
}
if (this.core.state.closed) return
if (useSession) this.ensureSession()

const makePeer = this._makePeer.bind(this, protomux, useSession)

Expand All @@ -2314,7 +2331,7 @@ module.exports = class Replicator {
this._ifAvailable--

if (opened && !this.destroyed) makePeer()
else if (useSession) this._closeSession()
else if (useSession) this._closeSessionMaybe()
this._checkUpgradeIfAvailable()
})
}
Expand Down Expand Up @@ -2342,10 +2359,6 @@ module.exports = class Replicator {
}

_makePeer (protomux, useSession) {
if (useSession) {
this._peerSessions++
}

const replicator = this
if (protomux.opened({ protocol: 'hypercore/alpha', id: this.discoveryKey })) return onnochannel()

Expand Down Expand Up @@ -2385,10 +2398,7 @@ module.exports = class Replicator {
return true

function onnochannel () {
if (useSession && --replicator._peerSessions === 0) {
replicator._closeSession()
}

if (useSession) replicator._closeSessionMaybe()
return false
}
}
Expand Down

0 comments on commit 5543a1c

Please sign in to comment.