Updates
This commit is contained in:
@@ -1,59 +1,107 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
|
||||
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'qos-topic/v1'
|
||||
const MAX_QOS = 2
|
||||
|
||||
class HyperP2PQosTopic extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this._store = new Map()
|
||||
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.peerHex = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this._queues = [ [], [], [] ]
|
||||
this._handlers = new Map()
|
||||
this._stats = { published: 0, delivered: 0, gossipIn: 0, gossipOut: 0 }
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
put (key, value) {
|
||||
assertNonEmpty(key, 'key')
|
||||
this._store.set(key, value)
|
||||
this._stats.ops++
|
||||
sendGossip(this, { type: 'qos-topic-sync', key, value })
|
||||
this.emit('update', { key, value })
|
||||
return true
|
||||
subscribe (channel, handler, qos = 0) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
if (typeof handler !== 'function') throw new Error('handler must be a function')
|
||||
this._handlers.set(channel, { handler, qos: Math.min(MAX_QOS, Math.max(0, qos | 0)) })
|
||||
return () => this._handlers.delete(channel)
|
||||
}
|
||||
|
||||
get (key) { return this._store.get(key) }
|
||||
|
||||
delete (key) {
|
||||
const ok = this._store.delete(key)
|
||||
if (ok) sendGossip(this, { type: 'qos-topic-sync', key, value: null })
|
||||
return ok
|
||||
publish (channel, payload, opts = {}) {
|
||||
assertNonEmpty(channel, 'channel')
|
||||
const qos = Math.min(MAX_QOS, Math.max(0, opts.qos | 0))
|
||||
const msg = {
|
||||
type: 'qos-publish',
|
||||
channel,
|
||||
payload,
|
||||
qos,
|
||||
from: this.peerHex,
|
||||
at: Date.now()
|
||||
}
|
||||
this._queues[qos].push(msg)
|
||||
this._stats.published++
|
||||
this._drain()
|
||||
if (this._peerMsgs) {
|
||||
gossipSend(this, msg)
|
||||
this._stats.gossipOut++
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
entries () { return [...this._store.entries()] }
|
||||
|
||||
_onGossip (d) {
|
||||
if (!d || d.type !== 'qos-topic-sync') return
|
||||
this._stats.gossipIn++
|
||||
if (d.key !== undefined) {
|
||||
if (d.value === null) this._store.delete(d.key)
|
||||
else this._store.set(d.key, d.value)
|
||||
_drain () {
|
||||
for (let q = MAX_QOS; q >= 0; q--) {
|
||||
while (this._queues[q].length) {
|
||||
const msg = this._queues[q].shift()
|
||||
this._deliver(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
|
||||
_deliver (msg) {
|
||||
const sub = this._handlers.get(msg.channel)
|
||||
if (sub && msg.qos >= sub.qos) {
|
||||
this._stats.delivered++
|
||||
sub.handler({ channel: msg.channel, payload: msg.payload, qos: msg.qos, from: msg.from })
|
||||
}
|
||||
this.emit('message', msg)
|
||||
}
|
||||
|
||||
pending (qos) {
|
||||
if (qos == null) return this._queues.reduce((n, q) => n + q.length, 0)
|
||||
return this._queues[qos]?.length || 0
|
||||
}
|
||||
|
||||
_onGossip (data) {
|
||||
if (!data || data.type !== 'qos-publish') return
|
||||
this._stats.gossipIn++
|
||||
this._queues[data.qos].push(data)
|
||||
this._drain()
|
||||
}
|
||||
|
||||
getStats () {
|
||||
return {
|
||||
...this._stats,
|
||||
pending: this.pending(),
|
||||
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) })
|
||||
await initModuleSwarm(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
|
||||
this._handlers.clear()
|
||||
for (const q of this._queues) q.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,23 +4,38 @@ const { HyperP2PQosTopic, PROTOCOL } = require('../index.js')
|
||||
|
||||
test('exports', (t) => {
|
||||
t.ok(HyperP2PQosTopic)
|
||||
t.ok(PROTOCOL)
|
||||
t.is(PROTOCOL, 'qos-topic/v1')
|
||||
})
|
||||
|
||||
test('basic operation', async (t) => {
|
||||
test('higher qos delivered first on drain', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
m.put('k', 1); t.is(m.get('k'), 1)
|
||||
const order = []
|
||||
m.subscribe('c', (msg) => order.push(msg.payload), 0)
|
||||
m._queues[0].push({ type: 'qos-publish', channel: 'c', payload: 'low', qos: 0, from: 'p' })
|
||||
m._queues[2].push({ type: 'qos-publish', channel: 'c', payload: 'high', qos: 2, from: 'p' })
|
||||
m._drain()
|
||||
t.is(order[0], 'high')
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('validation', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
try { m.put(null, 1) } catch (e) { t.ok(e) }
|
||||
try { m.publish('', 1) } catch (e) { t.ok(e) }
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('gossip delivery', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
let got = false
|
||||
m.subscribe('x', () => { got = true })
|
||||
m._onGossip({ type: 'qos-publish', channel: 'x', payload: 1, qos: 1, from: 'p' })
|
||||
t.ok(got)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('getStats', async (t) => {
|
||||
const m = new HyperP2PQosTopic()
|
||||
t.ok(m.getStats().protocol)
|
||||
m.publish('a', 1)
|
||||
t.is(m.getStats().published, 1)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user