Commit 837fca7b authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

p2p/net: cleaned up dial events

+ fixed race
parent 96cad134
...@@ -60,7 +60,6 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( ...@@ -60,7 +60,6 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
c2, err := newSecureConn(ctx, d.PrivateKey, c) c2, err := newSecureConn(ctx, d.PrivateKey, c)
if err != nil { if err != nil {
logdial["error"] = err
errOut = err errOut = err
c.Close() c.Close()
return return
......
...@@ -266,17 +266,18 @@ func (s *Swarm) gatedDialAttempt(ctx context.Context, p peer.ID) (*Conn, error) ...@@ -266,17 +266,18 @@ func (s *Swarm) gatedDialAttempt(ctx context.Context, p peer.ID) (*Conn, error)
// dial is the actual swarm's dial logic, gated by Dial. // dial is the actual swarm's dial logic, gated by Dial.
func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) { func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
var logdial = lgbl.Dial("swarm", s.LocalPeer(), p, nil, nil) var logdial = lgbl.Dial("swarm", s.LocalPeer(), p, nil, nil)
defer log.EventBegin(ctx, "swarmDialDo", logdial).Done()
if p == s.local { if p == s.local {
log.Event(ctx, "swarmDialDoDialSelf", logdial) log.Event(ctx, "swarmDialDoDialSelf", logdial)
return nil, ErrDialToSelf return nil, ErrDialToSelf
} }
defer log.EventBegin(ctx, "swarmDialDo", logdial).Done()
logdial["dial"] = "failure" // start off with failure. set to "success" at the end.
sk := s.peers.PrivKey(s.local) sk := s.peers.PrivKey(s.local)
logdial["encrypted"] = (sk != nil) // log wether this will be an encrypted dial or not.
if sk == nil { if sk == nil {
// may be fine for sk to be nil, just log. // fine for sk to be nil, just log.
log.Debug("Dial not given PrivateKey, so WILL NOT SECURE conn.") log.Debug("Dial not given PrivateKey, so WILL NOT SECURE conn.")
log.Event(ctx, "swarmDialDoInsecure", logdial)
} }
// get our own addrs. try dialing out from our listener addresses (reusing ports) // get our own addrs. try dialing out from our listener addresses (reusing ports)
...@@ -298,7 +299,9 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -298,7 +299,9 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
remoteAddrs = addrutil.Subtract(remoteAddrs, s.peers.Addresses(s.local)) remoteAddrs = addrutil.Subtract(remoteAddrs, s.peers.Addresses(s.local))
log.Debugf("%s swarm dialing %s -- remote:%s local:%s", s.local, p, remoteAddrs, s.ListenAddresses()) log.Debugf("%s swarm dialing %s -- remote:%s local:%s", s.local, p, remoteAddrs, s.ListenAddresses())
if len(remoteAddrs) == 0 { if len(remoteAddrs) == 0 {
return nil, errors.New("peer has no addresses") err := errors.New("peer has no addresses")
logdial["error"] = err
return nil, err
} }
// open connection to peer // open connection to peer
...@@ -316,20 +319,21 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -316,20 +319,21 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
// try to get a connection to any addr // try to get a connection to any addr
connC, err := s.dialAddrs(ctx, d, p, remoteAddrs) connC, err := s.dialAddrs(ctx, d, p, remoteAddrs)
if err != nil { if err != nil {
logdial["error"] = err
return nil, err return nil, err
} }
logdial["netconn"] = lgbl.NetConn(connC)
// ok try to setup the new connection. // ok try to setup the new connection.
defer log.EventBegin(ctx, "swarmDialDoSetup", logdial, lgbl.NetConn(connC)).Done() defer log.EventBegin(ctx, "swarmDialDoSetup", logdial, lgbl.NetConn(connC)).Done()
swarmC, err := dialConnSetup(ctx, s, connC) swarmC, err := dialConnSetup(ctx, s, connC)
if err != nil { if err != nil {
log.Debug("Dial newConnSetup failed. disconnecting.") logdial["error"] = err
log.Event(ctx, "swarmDialDoSetupFailed", logdial, lgbl.NetConn(connC), lgbl.Error(err))
connC.Close() // close the connection. didn't work out :( connC.Close() // close the connection. didn't work out :(
return nil, err return nil, err
} }
log.Event(ctx, "swarmDialDoSuccess", logdial, lgbl.NetConn(connC)) logdial["dial"] = "success"
return swarmC, nil return swarmC, nil
} }
...@@ -428,8 +432,6 @@ func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error ...@@ -428,8 +432,6 @@ func dialConnSetup(ctx context.Context, s *Swarm, connC conn.Conn) (*Conn, error
// ok try to setup the new connection. (newConnSetup will add to group) // ok try to setup the new connection. (newConnSetup will add to group)
swarmC, err := s.newConnSetup(ctx, psC) swarmC, err := s.newConnSetup(ctx, psC)
if err != nil { if err != nil {
log.Debug("Dial newConnSetup failed. disconnecting.")
log.Event(ctx, "dialFailureDisconnect", lgbl.NetConn(connC), lgbl.Error(err))
psC.Close() // we need to make sure psC is Closed. psC.Close() // we need to make sure psC is Closed.
return nil, err return nil, err
} }
......
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