Skip to content

Commit

Permalink
packages/relay-server: fix peer disconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joeltg committed Jun 21, 2024
1 parent f8a5bf9 commit 1a8b915
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/relay-server/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ ANNOUNCE = '/dns4/canvas-relay-server-thrumming-surf-3764.fly.dev/tcp/443/wss'
# DEBUG = 'libp2p:*error'
LISTEN = '/ip4/127.0.0.1/tcp/8080/ws'

[http_service]
internal_port = 3000
min_machines_running = 0
processes = ['app']

[[services]]
protocol = 'tcp'
internal_port = 8080
Expand Down
43 changes: 26 additions & 17 deletions packages/relay-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ getLibp2p().then(async (libp2p) => {
const topicMap = new Map<string, string[]>()
const topicIndex = new Map<string, Set<string>>()

app.get("/topicMap", (req, res) => void res.json(Object.fromEntries(topicMap)))
app.get(
"/topicIndex",
(req, res) => void res.json(Object.fromEntries(Array.from(topicIndex).map(([key, value]) => [key, [...value]]))),
)

libp2p.addEventListener("peer:disconnect", ({ detail: peerId }) => {
console.log(`peer:disconnect ${peerId}`)

for (const topic of topicMap.get(peerId.toString()) ?? []) {
topicMap.delete(topic)
topicMap.delete(peerId.toString())

const peers = topicIndex.get(topic)
peers?.delete(peerId.toString())
Expand All @@ -49,27 +57,28 @@ getLibp2p().then(async (libp2p) => {
const protocolPrefix = getProtocol("")

libp2p.addEventListener("peer:update", ({ detail: { peer, previous } }) => {
topicMap.set(peer.id.toString(), peer.protocols)
const topics = peer.protocols
.filter((protocol) => protocol.startsWith(protocolPrefix))
.map((protocol) => protocol.slice(protocolPrefix.length))

for (const protocol of previous?.protocols ?? []) {
if (protocol.startsWith(protocolPrefix)) {
const topic = protocol.slice(protocolPrefix.length)
topicIndex.get(topic)?.delete(peer.id.toString())
}
}
topicMap.set(peer.id.toString(), topics)

for (const protocol of peer.protocols) {
if (protocol.startsWith(protocolPrefix)) {
const topic = protocol.slice(protocolPrefix.length)
const previousTopics = previous?.protocols
.filter((protocol) => protocol.startsWith(protocolPrefix))
.map((protocol) => protocol.slice(protocolPrefix.length))

let peers = topicIndex.get(topic)
if (peers === undefined) {
peers = new Set()
topicIndex.set(topic, peers)
}
for (const topic of previousTopics ?? []) {
topicIndex.get(topic)?.delete(peer.id.toString())
}

peers.add(peer.id.toString())
for (const topic of topics) {
let peers = topicIndex.get(topic)
if (peers === undefined) {
peers = new Set()
topicIndex.set(topic, peers)
}

peers.add(peer.id.toString())
}
})

Expand Down

0 comments on commit 1a8b915

Please sign in to comment.