Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-swarm
Commits
d73f313e
Commit
d73f313e
authored
Mar 19, 2021
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: DRY direct connect logic
parent
1dde927f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
22 deletions
+18
-22
swarm.go
swarm.go
+11
-0
swarm_dial.go
swarm_dial.go
+7
-22
No files found.
swarm.go
View file @
d73f313e
...
...
@@ -439,6 +439,17 @@ func (s *Swarm) bestConnToPeer(p peer.ID) *Conn {
return
best
}
func
(
s
*
Swarm
)
bestAcceptableConnToPeer
(
ctx
context
.
Context
,
p
peer
.
ID
)
*
Conn
{
conn
:=
s
.
bestConnToPeer
(
p
)
if
conn
!=
nil
{
forceDirect
,
_
:=
network
.
GetForceDirectDial
(
ctx
)
if
!
forceDirect
||
isDirectConn
(
conn
)
{
return
conn
}
}
return
nil
}
func
isDirectConn
(
c
*
Conn
)
bool
{
return
c
!=
nil
&&
!
c
.
conn
.
Transport
()
.
Proxy
()
}
...
...
swarm_dial.go
View file @
d73f313e
...
...
@@ -251,14 +251,9 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) {
defer
log
.
EventBegin
(
ctx
,
"swarmDialAttemptSync"
,
p
)
.
Done
()
conn
:=
s
.
bestConnToPeer
(
p
)
forceDirect
,
_
:=
network
.
GetForceDirectDial
(
ctx
)
if
forceDirect
{
if
isDirectConn
(
conn
)
{
return
conn
,
nil
}
}
else
if
conn
!=
nil
{
// check if we already have an open connection first
// check if we already have an open (usable) connection first
conn
:=
s
.
bestAcceptableConnToPeer
(
ctx
,
p
)
if
conn
!=
nil
{
return
conn
,
nil
}
...
...
@@ -292,13 +287,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) {
// Short circuit.
// By the time we take the dial lock, we may already *have* a connection
// to the peer.
forceDirect
,
_
:=
network
.
GetForceDirectDial
(
ctx
)
c
:=
s
.
bestConnToPeer
(
p
)
if
forceDirect
{
if
isDirectConn
(
c
)
{
return
c
,
nil
}
}
else
if
c
!=
nil
{
c
:=
s
.
bestAcceptableConnToPeer
(
ctx
,
p
)
if
c
!=
nil
{
return
c
,
nil
}
...
...
@@ -310,13 +300,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) {
conn
,
err
:=
s
.
dial
(
ctx
,
p
)
if
err
!=
nil
{
conn
=
s
.
bestConnToPeer
(
p
)
if
forceDirect
{
if
isDirectConn
(
conn
)
{
log
.
Debugf
(
"ignoring dial error because we already have a direct connection: %s"
,
err
)
return
conn
,
nil
}
}
else
if
conn
!=
nil
{
conn
:=
s
.
bestAcceptableConnToPeer
(
ctx
,
p
)
if
conn
!=
nil
{
// Hm? What error?
// Could have canceled the dial because we received a
// connection or some other random reason.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment