Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try a direct connection only if there isn't one already #891

Merged
merged 4 commits into from
May 25, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions libp2p/services/hpservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ proc tryStartingDirectConn(self: HPService, switch: Switch, peerId: PeerId): Fut
continue
return false

proc newConnectedPeerHandler(self: HPService, switch: Switch, peerId: PeerId, event: PeerEvent) {.async.} =
try:
# Get all connections to the peer. If there is at least one non-relayed connection, return.
var relayedConnt: Connection
for muxer in switch.connManager.getConnections()[peerId]:
let conn = muxer.connection
if not isRelayed(conn):
return
elif conn.transportDir == Direction.In:
relayedConnt = conn

if not isNil(relayedConnt):
Menduist marked this conversation as resolved.
Show resolved Hide resolved
if await self.tryStartingDirectConn(switch, peerId):
await relayedConnt.close()
return
let dcutrClient = DcutrClient.new()
var natAddrs = switch.peerStore.getMostObservedProtosAndPorts()
if natAddrs.len == 0:
natAddrs = switch.peerInfo.listenAddrs.mapIt(switch.peerStore.guessDialableAddr(it))
await dcutrClient.startSync(switch, peerId, natAddrs)
await sleepAsync(2000.milliseconds) # grace period before closing relayed connection
await relayedConnt.close()
except CatchableError as err:
debug "Hole punching failed during dcutr", err = err.msg

method setup*(self: HPService, switch: Switch): Future[bool] {.async.} =
var hasBeenSetup = await procCall Service(self).setup(switch)
hasBeenSetup = hasBeenSetup and await self.autonatService.setup(switch)
Expand All @@ -71,22 +96,8 @@ method setup*(self: HPService, switch: Switch): Future[bool] {.async.} =
let dcutrProto = Dcutr.new(switch)
switch.mount(dcutrProto)

self.newConnectedPeerHandler = proc (peerId: PeerId, event: PeerEvent): Future[void] {.async.} =
try:
let conn = switch.connManager.selectMuxer(peerId).connection
if isRelayed(conn) and conn.transportDir == Direction.In:
if await self.tryStartingDirectConn(switch, peerId):
await conn.close()
return
let dcutrClient = DcutrClient.new()
var natAddrs = switch.peerStore.getMostObservedProtosAndPorts()
if natAddrs.len == 0:
natAddrs = switch.peerInfo.listenAddrs.mapIt(switch.peerStore.guessDialableAddr(it))
await dcutrClient.startSync(switch, peerId, natAddrs)
await sleepAsync(2000.milliseconds) # grace period before closing relayed connection
await conn.close()
except CatchableError as err:
debug "Hole punching failed during dcutr", err = err.msg
self.newConnectedPeerHandler = proc (peerId: PeerId, event: PeerEvent) {.async.} =
await newConnectedPeerHandler(self, switch, peerId, event)

switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined)

Expand Down