diff --git a/net/interface.go b/net/interface.go index 71a774456c28..bac53497552b 100644 --- a/net/interface.go +++ b/net/interface.go @@ -48,3 +48,12 @@ type Handler srv.Handler // Service interface for network resources. type Service srv.Service + +// Dialer service that can dial to peers +// (this is usually just a Network, but other services may not need the whole +// thing, and thus it becomes easier to mock) +type Dialer interface { + + // DialPeer attempts to establish a connection to a given peer + DialPeer(peer.Peer) error +} diff --git a/routing/dht/dht.go b/routing/dht/dht.go index 1cbb73df4dc8..c6079855ac2b 100644 --- a/routing/dht/dht.go +++ b/routing/dht/dht.go @@ -33,9 +33,9 @@ type IpfsDHT struct { // NOTE: (currently, only a single table is used) routingTables []*kb.RoutingTable - // the network interface. service - network inet.Network - sender inet.Sender + // the network services we need + dialer inet.Dialer + sender inet.Sender // Local peer (yourself) self peer.Peer @@ -59,9 +59,9 @@ type IpfsDHT struct { } // NewDHT creates a new DHT object with the given peer as the 'local' host -func NewDHT(ctx context.Context, p peer.Peer, ps peer.Peerstore, net inet.Network, sender inet.Sender, dstore ds.Datastore) *IpfsDHT { +func NewDHT(ctx context.Context, p peer.Peer, ps peer.Peerstore, dialer inet.Dialer, sender inet.Sender, dstore ds.Datastore) *IpfsDHT { dht := new(IpfsDHT) - dht.network = net + dht.dialer = dialer dht.sender = sender dht.datastore = dstore dht.self = p @@ -95,7 +95,7 @@ func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.Peer) (peer.Peer, er // // /ip4/10.20.30.40/tcp/1234/ipfs/Qxhxxchxzcncxnzcnxzcxzm // - err := dht.network.DialPeer(npeer) + err := dht.dialer.DialPeer(npeer) if err != nil { return nil, err } @@ -499,7 +499,7 @@ func (dht *IpfsDHT) ensureConnectedToPeer(pbp *Message_Peer) (peer.Peer, error) } // dial connection - err = dht.network.DialPeer(p) + err = dht.dialer.DialPeer(p) return p, err } diff --git a/routing/dht/query.go b/routing/dht/query.go index 4096632b6ae4..d15e939b7b61 100644 --- a/routing/dht/query.go +++ b/routing/dht/query.go @@ -3,6 +3,7 @@ package dht import ( "sync" + inet "github.com/jbenet/go-ipfs/net" peer "github.com/jbenet/go-ipfs/peer" queue "github.com/jbenet/go-ipfs/peer/queue" kb "github.com/jbenet/go-ipfs/routing/kbucket" @@ -14,17 +15,12 @@ import ( var maxQueryConcurrency = AlphaValue -type dhtDialer interface { - // DialPeer attempts to establish a connection to a given peer - DialPeer(peer.Peer) error -} - type dhtQuery struct { // the key we're querying for key u.Key // dialer used to ensure we're connected to peers - dialer dhtDialer + dialer inet.Dialer // the function to execute per peer qfunc queryFunc @@ -42,7 +38,7 @@ type dhtQueryResult struct { } // constructs query -func newQuery(k u.Key, d dhtDialer, f queryFunc) *dhtQuery { +func newQuery(k u.Key, d inet.Dialer, f queryFunc) *dhtQuery { return &dhtQuery{ key: k, dialer: d, diff --git a/routing/dht/routing.go b/routing/dht/routing.go index 8c0d69860763..b557aa76c50b 100644 --- a/routing/dht/routing.go +++ b/routing/dht/routing.go @@ -29,7 +29,7 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error peers = append(peers, npeers...) } - query := newQuery(key, dht.network, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { + query := newQuery(key, dht.dialer, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { log.Debug("%s PutValue qry part %v", dht.self, p) err := dht.putValueToNetwork(ctx, p, string(key), value) if err != nil { @@ -65,7 +65,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) { } // setup the Query - query := newQuery(key, dht.network, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { + query := newQuery(key, dht.dialer, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { val, peers, err := dht.getValueOrPeers(ctx, p, key, routeLevel) if err != nil { @@ -230,7 +230,7 @@ func (dht *IpfsDHT) findPeerMultiple(ctx context.Context, id peer.ID) (peer.Peer } // setup query function - query := newQuery(u.Key(id), dht.network, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { + query := newQuery(u.Key(id), dht.dialer, func(ctx context.Context, p peer.Peer) (*dhtQueryResult, error) { pmes, err := dht.findPeerSingle(ctx, p, id, routeLevel) if err != nil { log.Error("%s getPeer error: %v", dht.self, err)