Skip to content

Commit 0a1f3aa

Browse files
committed
fix: cancel state at connection scope instead of stream
1 parent a17f9ec commit 0a1f3aa

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/service.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ async function startService ({ peerId, port, peerAnnounceAddr, awsClient, connec
101101
}
102102
}
103103

104+
const cancelsPerPeer = new Map()
104105
service.addEventListener('error', err => {
105106
logger.warn({ err }, 'libp2p error event')
106107
})
@@ -109,7 +110,7 @@ async function startService ({ peerId, port, peerAnnounceAddr, awsClient, connec
109110
service.handle(protocol, async ({ connection: dial, stream }) => {
110111
try {
111112
const connection = new Connection(stream)
112-
const canceled = new LRU({ max: 200 })
113+
const canceled = cancelsPerPeer.get(dial.remotePeer.toString())
113114

114115
const hrTime = process.hrtime()
115116
const connectionId = hrTime[0] * 1000000000 + hrTime[1]
@@ -161,20 +162,28 @@ async function startService ({ peerId, port, peerAnnounceAddr, awsClient, connec
161162

162163
// TODO move to networking
163164
service.connectionManager.addEventListener('peer:connect', connection => {
165+
cancelsPerPeer.set(
166+
connection.detail.remotePeer.toString(),
167+
new LRU({ max: 200 })
168+
)
169+
164170
try {
165171
telemetry.increaseCount('bitswap-connections')
166172
telemetry.increaseGauge('bitswap-active-connections')
167173
} catch (err) {
168-
logger.warn({ err, remotePeer: connection.remotePeer }, 'Error while peer connecting')
174+
logger.warn({ err, remotePeer: connection.detail.remotePeer.toString() }, 'Error while peer connecting')
169175
}
170176
})
171177

172178
// TODO move to networking
173179
service.connectionManager.addEventListener('peer:disconnect', connection => {
180+
cancelsPerPeer.delete(
181+
connection.detail.remotePeer.toString()
182+
)
174183
try {
175184
telemetry.decreaseGauge('bitswap-active-connections')
176185
} catch (err) {
177-
logger.warn({ err, remotePeer: connection.remotePeer }, 'Error while peer disconnecting')
186+
logger.warn({ err, remotePeer: connection.detail.remotePeer.toString() }, 'Error while peer disconnecting')
178187
}
179188
})
180189

0 commit comments

Comments
 (0)