dont save tunnel peers in stats file

This commit is contained in:
Andre Staltz 2024-02-06 13:57:36 +02:00
parent 0c7652caba
commit ee34cc6d14
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 55 additions and 27 deletions

View File

@ -47,37 +47,46 @@ function glue(infos, connections) {
* @param {Event} ev
*/
function onConnectingFailed(ev) {
if (!ev.multiaddr.startsWith('/tunnel/')) {
infos.updateStats(ev.multiaddr, (prevStats) => ({
failure: (prevStats?.failure ?? 0) + 1,
stateChange: Date.now(),
duration: stats(prevStats?.duration, 0),
}))
}
}
/**
* @param {Event} ev
*/
function onConnected(ev) {
if (!ev.multiaddr.startsWith('/tunnel/')) {
infos.updateStats(ev.multiaddr, () => ({
stateChange: Date.now(),
failure: 0,
}))
if (ev.details.weAreClient) setupPing(ev.multiaddr, ev.details.rpc)
}
if (ev.details.weAreClient) {
setupPing(ev.multiaddr, ev.details.rpc)
}
}
/**
* @param {Event} ev
*/
function bumpStateChange(ev) {
if (!ev.multiaddr.startsWith('/tunnel/')) {
infos.updateStats(ev.multiaddr, () => ({
stateChange: Date.now(),
}))
}
}
/**
* @param {Event} ev
*/
function onDisconnected(ev) {
if (!ev.multiaddr.startsWith('/tunnel/')) {
infos.updateStats(ev.multiaddr, (prevStats) => ({
stateChange: Date.now(),
duration: stats(
@ -86,6 +95,7 @@ function glue(infos, connections) {
),
}))
}
}
pull(
connections.listen(),

View File

@ -114,18 +114,15 @@ class Stats {
return
} else if (fileContents) {
const vals = SelfHealingJSONCodec.decode(fileContents)
for (const [multiaddr, statsInfo] of Object.entries(vals)) {
this.#infos.update(/**@type {`/${string}`}*/ (multiaddr), {
stats: statsInfo,
})
for (const [multiaddr, stats] of Object.entries(vals)) {
this.#infos.update(/**@type {`/${string}`}*/ (multiaddr), { stats })
}
this.#loadedResolve(true)
debug('Loaded conn.json into ConnDB in memory')
debug(`Loaded existing ${Stats.FILENAME}`)
} else {
atomic.writeFile(this.#path, '{}', 'utf8', () => {})
this.#loadedResolve(true)
// prettier-ignore
debug(`Created new ${Stats.FILENAME} because there was no existing one.`);
debug(`Created ${Stats.FILENAME} because there was none yet`)
return
}
})

View File

@ -6,7 +6,9 @@ const p = require('node:util').promisify
const { createPeerMock } = require('./util')
const PUBKEY = 'EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC'
const PUBKEY2 = 'FqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC'
const TEST_ADDR = `/ip4/127.0.0.1/tcp/9752/shse/${PUBKEY}`
const TEST_TUNNEL_ADDR = `/tunnel/${PUBKEY}.${PUBKEY2}/shse/${PUBKEY2}`
test('Glueing together stats with connections', async (t) => {
await t.test('stage() is ignored when peer already connected', async () => {
@ -66,6 +68,25 @@ test('Glueing together stats with connections', async (t) => {
assert.deepEqual(Object.keys(json[TEST_ADDR]), ['stateChange'])
})
await t.test('tunnel connections are not stats-persisted', async (t) => {
const peer = createPeerMock()
const address = TEST_TUNNEL_ADDR
const entriesBefore = await p(peer.net.peers())(null)
assert.equal(entriesBefore.length, 0, 'there is no entry in peers()')
const rpc = await p(peer.net.connect)(address)
assert.ok(rpc, 'connect() successful')
const statsJSONPath = Path.join(peer.mockDir, 'net', './stats.json')
while (FS.existsSync(statsJSONPath) === false) {
await p(setTimeout)(1)
}
const fileContents = FS.readFileSync(statsJSONPath, 'utf8')
const json = JSON.parse(fileContents)
assert.deepEqual(Object.keys(json), [])
})
await t.test('forget() will remove stats', async (t) => {
const peer = createPeerMock()
const address = TEST_ADDR