Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
remove BestConnFn; simplify AddConnFn usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk committed Nov 9, 2018
1 parent f4e2dfe commit 2b36943
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
3 changes: 0 additions & 3 deletions dial/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"io"

"github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-peer"
tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -69,4 +67,3 @@ type Selector interface {
}

type TransportResolverFn func(a ma.Multiaddr) tpt.Transport
type BestConnFn func(p peer.ID) inet.Conn
13 changes: 3 additions & 10 deletions dial/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import lgbl "github.com/libp2p/go-libp2p-loggables"
// ErrDialToSelf is returned if we attempt to dial our own peer
var ErrDialToSelf = errors.New("dial to self attempted")

type validator struct {
bestConnFn BestConnFn
}
type validator struct{}

var _ Preparer = (*validator)(nil)

func NewValidator(bestConnFn BestConnFn) Preparer {
return &validator{bestConnFn: bestConnFn}
func NewValidator() Preparer {
return &validator{}
}

func (v *validator) Prepare(req *Request) {
Expand All @@ -36,9 +34,4 @@ func (v *validator) Prepare(req *Request) {
}

defer log.EventBegin(req.ctx, "swarmDialAttemptSync", id).Done()

// check if we already have an open connection first
if conn := v.bestConnFn(id); conn != nil {
req.Complete(conn, nil)
}
}
22 changes: 8 additions & 14 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,7 @@ func NewSwarm(ctx context.Context, local peer.ID, peers pstore.Peerstore, bwc me
}

func (s *Swarm) defaultPipeline() *dial.Pipeline {
// go cannot deal with contravariant types in return values, we *have* to do this proxying.
addConnFn := func(tc transport.Conn, dir inet.Direction) (inet.Conn, error) { return s.addConn(tc, dir) }
bestConnFn := func(p peer.ID) inet.Conn {
// this is necessary because of https://golang.org/doc/faq#nil_error
c := s.bestConnToPeer(p)
if c == nil {
return nil
}
return c
}

p := dial.NewPipeline(s.ctx, s, addConnFn)
p := dial.NewPipeline(s.ctx, s, s.addConn)

canDial := func(addr ma.Multiaddr) bool {
t := s.TransportForDialing(addr)
Expand All @@ -151,7 +140,7 @@ func (s *Swarm) defaultPipeline() *dial.Pipeline {

// preparers
seq := new(dial.PreparerSeq)
seq.AddLast("validator", dial.NewValidator(bestConnFn))
seq.AddLast("validator", dial.NewValidator())
seq.AddLast("request_timeout", dial.NewRequestTimeout())
seq.AddLast("syncer", dial.NewDialSync())
seq.AddLast("backoff", s.backoff)
Expand Down Expand Up @@ -181,6 +170,11 @@ func (s *Swarm) defaultPipeline() *dial.Pipeline {
// This allows us to use various transport protocols, do NAT traversal/relay,
// etc. to achieve connection.
func (s *Swarm) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) {
// check if we already have an open connection first
if conn := s.bestConnToPeer(p); conn != nil {
return conn, nil
}

return s.pipeline.Dial(ctx, p)
}

Expand Down Expand Up @@ -248,7 +242,7 @@ func (s *Swarm) Process() goprocess.Process {
return s.proc
}

func (s *Swarm) addConn(tc transport.Conn, dir inet.Direction) (*Conn, error) {
func (s *Swarm) addConn(tc transport.Conn, dir inet.Direction) (inet.Conn, error) {
// The underlying transport (or the dialer) *should* filter it's own
// connections but we should double check anyways.
raddr := tc.RemoteMultiaddr()
Expand Down

0 comments on commit 2b36943

Please sign in to comment.