Commit d73f313e authored by Steven Allen's avatar Steven Allen

fix: DRY direct connect logic

parent 1dde927f
...@@ -439,6 +439,17 @@ func (s *Swarm) bestConnToPeer(p peer.ID) *Conn { ...@@ -439,6 +439,17 @@ func (s *Swarm) bestConnToPeer(p peer.ID) *Conn {
return best 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 { func isDirectConn(c *Conn) bool {
return c != nil && !c.conn.Transport().Proxy() return c != nil && !c.conn.Transport().Proxy()
} }
......
...@@ -251,14 +251,9 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -251,14 +251,9 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) {
defer log.EventBegin(ctx, "swarmDialAttemptSync", p).Done() defer log.EventBegin(ctx, "swarmDialAttemptSync", p).Done()
conn := s.bestConnToPeer(p) // check if we already have an open (usable) connection first
forceDirect, _ := network.GetForceDirectDial(ctx) conn := s.bestAcceptableConnToPeer(ctx, p)
if forceDirect { if conn != nil {
if isDirectConn(conn) {
return conn, nil
}
} else if conn != nil {
// check if we already have an open connection first
return conn, nil return conn, nil
} }
...@@ -292,13 +287,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -292,13 +287,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) {
// Short circuit. // Short circuit.
// By the time we take the dial lock, we may already *have* a connection // By the time we take the dial lock, we may already *have* a connection
// to the peer. // to the peer.
forceDirect, _ := network.GetForceDirectDial(ctx) c := s.bestAcceptableConnToPeer(ctx, p)
c := s.bestConnToPeer(p) if c != nil {
if forceDirect {
if isDirectConn(c) {
return c, nil
}
} else if c != nil {
return c, nil return c, nil
} }
...@@ -310,13 +300,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -310,13 +300,8 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) {
conn, err := s.dial(ctx, p) conn, err := s.dial(ctx, p)
if err != nil { if err != nil {
conn = s.bestConnToPeer(p) conn := s.bestAcceptableConnToPeer(ctx, p)
if forceDirect { if conn != nil {
if isDirectConn(conn) {
log.Debugf("ignoring dial error because we already have a direct connection: %s", err)
return conn, nil
}
} else if conn != nil {
// Hm? What error? // Hm? What error?
// Could have canceled the dial because we received a // Could have canceled the dial because we received a
// connection or some other random reason. // connection or some other random reason.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment