Skip to content

Commit

Permalink
gc monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
MKPLKN committed Sep 5, 2024
1 parent 2733b4f commit 589bc24
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = class Hyperdrive extends ReadyResource {
this.blobs = null
this.supportsMetadata = true
this.encryptionKey = opts.encryptionKey || null
this.monitors = new Set()

this._active = opts.active !== false
this._openingBlobs = null
Expand Down Expand Up @@ -189,6 +190,8 @@ module.exports = class Hyperdrive extends ReadyResource {
if (!this._checkout && !this._batching) {
await this.corestore.close()
}

await this.closeMonitors()
}

async _openBlobsFromHeader (opts) {
Expand Down Expand Up @@ -280,7 +283,15 @@ module.exports = class Hyperdrive extends ReadyResource {
}

monitor (name, opts = {}) {
return new Monitor(this, { name, ...opts })
const monitor = new Monitor(this, { name, ...opts })
this.monitors.add(monitor)
return monitor
}

async closeMonitors () {
const closing = []
for (const monitor of this.monitors) closing.push(monitor.close())
await Promise.allSettled(closing)
}

async get (name, opts) {
Expand Down
5 changes: 2 additions & 3 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ module.exports = class Monitor extends ReadyResource {
this._boundOnAppend = debounce(this._onAppend.bind(this))
this._boundOnUpload = this._onUpload.bind(this)
this._boundOnDownload = this._onDownload.bind(this)
this.drive.on('close', () => this.close())

const stats = {
startTime: 0,
percentage: 0,
peersCount: 0,
peers: 0,
speed: 0,
blocks: 0,
totalBytes: 0, // local + bytes loaded during monitoring
Expand Down Expand Up @@ -89,7 +88,7 @@ module.exports = class Monitor extends ReadyResource {
if (!isWithinRange(index, this.entry)) return

if (!stats.startTime) stats.startTime = Date.now()
stats.peersCount = from.replicator.peers.length
stats.peers = from.replicator.peers.length
stats.blocks++
stats.monitoringBytes += bytes
stats.totalBytes += bytes
Expand Down

0 comments on commit 589bc24

Please sign in to comment.