simplify connmanager (#573)
* no need to init orderedset * array more simple than table
This commit is contained in:
parent
83a20a992a
commit
9674a6a6f6
|
@ -71,8 +71,8 @@ type
|
||||||
outSema*: AsyncSemaphore
|
outSema*: AsyncSemaphore
|
||||||
conns: Table[PeerID, HashSet[Connection]]
|
conns: Table[PeerID, HashSet[Connection]]
|
||||||
muxed: Table[Connection, MuxerHolder]
|
muxed: Table[Connection, MuxerHolder]
|
||||||
connEvents: Table[ConnEventKind, OrderedSet[ConnEventHandler]]
|
connEvents: array[ConnEventKind, OrderedSet[ConnEventHandler]]
|
||||||
peerEvents: Table[PeerEventKind, OrderedSet[PeerEventHandler]]
|
peerEvents: array[PeerEventKind, OrderedSet[PeerEventHandler]]
|
||||||
|
|
||||||
proc newTooManyConnectionsError(): ref TooManyConnectionsError {.inline.} =
|
proc newTooManyConnectionsError(): ref TooManyConnectionsError {.inline.} =
|
||||||
result = newException(TooManyConnectionsError, "Too many connections")
|
result = newException(TooManyConnectionsError, "Too many connections")
|
||||||
|
@ -106,21 +106,19 @@ proc addConnEventHandler*(c: ConnManager,
|
||||||
##
|
##
|
||||||
|
|
||||||
if isNil(handler): return
|
if isNil(handler): return
|
||||||
c.connEvents.mgetOrPut(kind,
|
c.connEvents[kind].incl(handler)
|
||||||
initOrderedSet[ConnEventHandler]()).incl(handler)
|
|
||||||
|
|
||||||
proc removeConnEventHandler*(c: ConnManager,
|
proc removeConnEventHandler*(c: ConnManager,
|
||||||
handler: ConnEventHandler,
|
handler: ConnEventHandler,
|
||||||
kind: ConnEventKind) =
|
kind: ConnEventKind) =
|
||||||
c.connEvents.withValue(kind, handlers) do:
|
c.connEvents[kind].excl(handler)
|
||||||
handlers[].excl(handler)
|
|
||||||
|
|
||||||
proc triggerConnEvent*(c: ConnManager,
|
proc triggerConnEvent*(c: ConnManager,
|
||||||
peerId: PeerID,
|
peerId: PeerID,
|
||||||
event: ConnEvent) {.async, gcsafe.} =
|
event: ConnEvent) {.async, gcsafe.} =
|
||||||
try:
|
try:
|
||||||
trace "About to trigger connection events", peer = peerId
|
trace "About to trigger connection events", peer = peerId
|
||||||
if event.kind in c.connEvents:
|
if c.connEvents[event.kind].len() > 0:
|
||||||
trace "triggering connection events", peer = peerId, event = $event.kind
|
trace "triggering connection events", peer = peerId, event = $event.kind
|
||||||
var connEvents: seq[Future[void]]
|
var connEvents: seq[Future[void]]
|
||||||
for h in c.connEvents[event.kind]:
|
for h in c.connEvents[event.kind]:
|
||||||
|
@ -140,21 +138,19 @@ proc addPeerEventHandler*(c: ConnManager,
|
||||||
##
|
##
|
||||||
|
|
||||||
if isNil(handler): return
|
if isNil(handler): return
|
||||||
c.peerEvents.mgetOrPut(kind,
|
c.peerEvents[kind].incl(handler)
|
||||||
initOrderedSet[PeerEventHandler]()).incl(handler)
|
|
||||||
|
|
||||||
proc removePeerEventHandler*(c: ConnManager,
|
proc removePeerEventHandler*(c: ConnManager,
|
||||||
handler: PeerEventHandler,
|
handler: PeerEventHandler,
|
||||||
kind: PeerEventKind) =
|
kind: PeerEventKind) =
|
||||||
c.peerEvents.withValue(kind, handlers) do:
|
c.peerEvents[kind].excl(handler)
|
||||||
handlers[].excl(handler)
|
|
||||||
|
|
||||||
proc triggerPeerEvents*(c: ConnManager,
|
proc triggerPeerEvents*(c: ConnManager,
|
||||||
peerId: PeerID,
|
peerId: PeerID,
|
||||||
event: PeerEvent) {.async, gcsafe.} =
|
event: PeerEvent) {.async, gcsafe.} =
|
||||||
|
|
||||||
trace "About to trigger peer events", peer = peerId
|
trace "About to trigger peer events", peer = peerId
|
||||||
if event.kind notin c.peerEvents:
|
if c.peerEvents[event.kind].len == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -224,11 +220,11 @@ proc closeMuxerHolder(muxerHolder: MuxerHolder) {.async.} =
|
||||||
|
|
||||||
proc delConn(c: ConnManager, conn: Connection) =
|
proc delConn(c: ConnManager, conn: Connection) =
|
||||||
let peerId = conn.peerInfo.peerId
|
let peerId = conn.peerInfo.peerId
|
||||||
if peerId in c.conns:
|
c.conns.withValue(peerId, peerConns):
|
||||||
c.conns[peerId].excl(conn)
|
peerConns[].excl(conn)
|
||||||
|
|
||||||
if c.conns[peerId].len == 0:
|
if peerConns[].len == 0:
|
||||||
c.conns.del(peerId)
|
c.conns.del(peerId) # invalidates `peerConns`
|
||||||
|
|
||||||
libp2p_peers.set(c.conns.len.int64)
|
libp2p_peers.set(c.conns.len.int64)
|
||||||
trace "Removed connection", conn
|
trace "Removed connection", conn
|
||||||
|
@ -366,10 +362,7 @@ proc storeConn*(c: ConnManager, conn: Connection) =
|
||||||
|
|
||||||
raise newTooManyConnectionsError()
|
raise newTooManyConnectionsError()
|
||||||
|
|
||||||
if peerId notin c.conns:
|
c.conns.mgetOrPut(peerId, HashSet[Connection]()).incl(conn)
|
||||||
c.conns[peerId] = initHashSet[Connection]()
|
|
||||||
|
|
||||||
c.conns[peerId].incl(conn)
|
|
||||||
libp2p_peers.set(c.conns.len.int64)
|
libp2p_peers.set(c.conns.len.int64)
|
||||||
|
|
||||||
# Launch on close listener
|
# Launch on close listener
|
||||||
|
|
Loading…
Reference in New Issue