This commit is contained in:
Raven Scott
2026-05-20 21:57:50 -04:00
parent c5d54be3ca
commit 341542f41b
426 changed files with 18651 additions and 3519 deletions
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'histogram-gossip/v1'
@@ -7,18 +9,44 @@ class HyperP2PHistogramGossip extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._samples = []
this._stats = { samples: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
record (value) {
if (typeof value !== 'number') throw new Error('value must be a number')
this._samples.push({ value, at: Date.now() })
this._stats.samples++
sendGossip(this, { type: 'histogram-gossip-sync', value })
return true
}
async ready () { return true }
summarize () {
if (!this._samples.length) return null
const vals = this._samples.map((s) => s.value)
const sum = vals.reduce((a, b) => a + b, 0)
return { count: vals.length, min: Math.min(...vals), max: Math.max(...vals), avg: sum / vals.length }
}
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
_onGossip (d) {
if (!d || d.type !== 'histogram-gossip-sync') return
this._stats.gossipIn++
if (typeof d.value === 'number') this._samples.push({ value: d.value, at: Date.now() })
}
getStats () { return { ...this._stats, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}