mirror of https://github.com/vacp2p/nim-libp2p.git
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
|
||||
|
||||
var peers: HashSet[string]
|
||||
# TODO: we probably don't need to try multiple times
|
||||
if data.len > 0 and topic.len > 0:
|
||||
for _ in 0..<5: # try to get peers up to 5 times
|
||||
if peers.len > 0:
|
||||
|
@ -470,13 +471,17 @@ method publish*(g: GossipSub,
|
|||
await sleepAsync(1.seconds)
|
||||
|
||||
let msg = newMessage(g.peerInfo, data, topic, g.sign)
|
||||
trace "created new message", msg
|
||||
var sent: seq[Future[void]]
|
||||
for p in peers:
|
||||
if p == g.peerInfo.id:
|
||||
continue
|
||||
|
||||
trace "publishing on topic", name = topic
|
||||
if msg.msgId notin g.mcache:
|
||||
g.mcache.put(msg)
|
||||
|
||||
if p in g.peers:
|
||||
sent.add(g.peers[p].send(@[RPCMsg(messages: @[msg])]))
|
||||
checkFutures(await allFinished(sent))
|
||||
|
||||
|
|
|
@ -22,6 +22,14 @@ type
|
|||
historySize*: 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 handler(key: string, val: Message) {.gcsafe.} =
|
||||
## make sure we remove the message from history
|
||||
|
@ -30,14 +38,10 @@ proc put*(c: MCache, msg: Message) =
|
|||
it.filterIt(it.mid != msg.msgId)
|
||||
)
|
||||
|
||||
if msg.msgId notin c.msgs:
|
||||
c.msgs.put(msg.msgId, msg, handler = handler)
|
||||
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] =
|
||||
result = initHashSet[string]()
|
||||
|
||||
|
|
|
@ -379,17 +379,16 @@ proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
|
|||
## Subscribe to pub sub peer
|
||||
if s.pubSub.isSome and (peerInfo.id notin s.dialedPubSubPeers):
|
||||
let conn = await s.getMuxedStream(peerInfo)
|
||||
try:
|
||||
if isNil(conn):
|
||||
trace "unable to subscribe to peer", peer = peerInfo.shortLog
|
||||
return
|
||||
|
||||
s.dialedPubSubPeers.incl(peerInfo.id)
|
||||
try:
|
||||
if (await s.ms.select(conn, s.pubSub.get().codec)):
|
||||
await s.pubSub.get().subscribeToPeer(conn)
|
||||
else:
|
||||
await conn.close()
|
||||
|
||||
except CatchableError as exc:
|
||||
trace "exception in subscribe to peer", peer = peerInfo.shortLog, exc = exc.msg
|
||||
await conn.close()
|
||||
|
|
Loading…
Reference in New Issue