mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-01-12 09:44:14 +00:00
Small fixes (#230)
* count published messages * don't call `switch.dial` in `subscribeToPeer` * make sure sending doesn't fail * add `contains` * review comment from prev pr
This commit is contained in:
parent
5f75e5adc6
commit
719744f46a
@ -451,6 +451,7 @@ method publish*(g: GossipSub,
|
|||||||
data = data.shortLog
|
data = data.shortLog
|
||||||
|
|
||||||
var peers: HashSet[string]
|
var peers: HashSet[string]
|
||||||
|
# TODO: we probably don't need to try multiple times
|
||||||
if data.len > 0 and topic.len > 0:
|
if data.len > 0 and topic.len > 0:
|
||||||
for _ in 0..<5: # try to get peers up to 5 times
|
for _ in 0..<5: # try to get peers up to 5 times
|
||||||
if peers.len > 0:
|
if peers.len > 0:
|
||||||
@ -470,13 +471,17 @@ method publish*(g: GossipSub,
|
|||||||
await sleepAsync(1.seconds)
|
await sleepAsync(1.seconds)
|
||||||
|
|
||||||
let msg = newMessage(g.peerInfo, data, topic, g.sign)
|
let msg = newMessage(g.peerInfo, data, topic, g.sign)
|
||||||
|
trace "created new message", msg
|
||||||
var sent: seq[Future[void]]
|
var sent: seq[Future[void]]
|
||||||
for p in peers:
|
for p in peers:
|
||||||
if p == g.peerInfo.id:
|
if p == g.peerInfo.id:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
trace "publishing on topic", name = topic
|
trace "publishing on topic", name = topic
|
||||||
|
if msg.msgId notin g.mcache:
|
||||||
g.mcache.put(msg)
|
g.mcache.put(msg)
|
||||||
|
|
||||||
|
if p in g.peers:
|
||||||
sent.add(g.peers[p].send(@[RPCMsg(messages: @[msg])]))
|
sent.add(g.peers[p].send(@[RPCMsg(messages: @[msg])]))
|
||||||
checkFutures(await allFinished(sent))
|
checkFutures(await allFinished(sent))
|
||||||
|
|
||||||
|
@ -22,6 +22,14 @@ type
|
|||||||
historySize*: Natural
|
historySize*: Natural
|
||||||
windowSize*: Natural
|
windowSize*: Natural
|
||||||
|
|
||||||
|
proc get*(c: MCache, mid: string): Option[Message] =
|
||||||
|
result = none(Message)
|
||||||
|
if mid in c.msgs:
|
||||||
|
result = some(c.msgs[mid])
|
||||||
|
|
||||||
|
proc contains*(c: MCache, mid: string): bool =
|
||||||
|
c.get(mid).isSome
|
||||||
|
|
||||||
proc put*(c: MCache, msg: Message) =
|
proc put*(c: MCache, msg: Message) =
|
||||||
proc handler(key: string, val: Message) {.gcsafe.} =
|
proc handler(key: string, val: Message) {.gcsafe.} =
|
||||||
## make sure we remove the message from history
|
## make sure we remove the message from history
|
||||||
@ -30,14 +38,10 @@ proc put*(c: MCache, msg: Message) =
|
|||||||
it.filterIt(it.mid != msg.msgId)
|
it.filterIt(it.mid != msg.msgId)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if msg.msgId notin c.msgs:
|
||||||
c.msgs.put(msg.msgId, msg, handler = handler)
|
c.msgs.put(msg.msgId, msg, handler = handler)
|
||||||
c.history[0].add(CacheEntry(mid: msg.msgId, msg: msg))
|
c.history[0].add(CacheEntry(mid: msg.msgId, msg: msg))
|
||||||
|
|
||||||
proc get*(c: MCache, mid: string): Option[Message] =
|
|
||||||
result = none(Message)
|
|
||||||
if mid in c.msgs:
|
|
||||||
result = some(c.msgs[mid])
|
|
||||||
|
|
||||||
proc window*(c: MCache, topic: string): HashSet[string] =
|
proc window*(c: MCache, topic: string): HashSet[string] =
|
||||||
result = initHashSet[string]()
|
result = initHashSet[string]()
|
||||||
|
|
||||||
|
@ -379,17 +379,16 @@ proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
|
|||||||
## Subscribe to pub sub peer
|
## Subscribe to pub sub peer
|
||||||
if s.pubSub.isSome and (peerInfo.id notin s.dialedPubSubPeers):
|
if s.pubSub.isSome and (peerInfo.id notin s.dialedPubSubPeers):
|
||||||
let conn = await s.getMuxedStream(peerInfo)
|
let conn = await s.getMuxedStream(peerInfo)
|
||||||
try:
|
|
||||||
if isNil(conn):
|
if isNil(conn):
|
||||||
trace "unable to subscribe to peer", peer = peerInfo.shortLog
|
trace "unable to subscribe to peer", peer = peerInfo.shortLog
|
||||||
return
|
return
|
||||||
|
|
||||||
s.dialedPubSubPeers.incl(peerInfo.id)
|
s.dialedPubSubPeers.incl(peerInfo.id)
|
||||||
|
try:
|
||||||
if (await s.ms.select(conn, s.pubSub.get().codec)):
|
if (await s.ms.select(conn, s.pubSub.get().codec)):
|
||||||
await s.pubSub.get().subscribeToPeer(conn)
|
await s.pubSub.get().subscribeToPeer(conn)
|
||||||
else:
|
else:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "exception in subscribe to peer", peer = peerInfo.shortLog, exc = exc.msg
|
trace "exception in subscribe to peer", peer = peerInfo.shortLog, exc = exc.msg
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user