Skip to content

Commit

Permalink
migrate to consolidated types (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulk authored May 25, 2019
1 parent 90574a9 commit 65aca08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
4 changes: 2 additions & 2 deletions p2p/net/connmgr/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"sync"
"testing"

inet "github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-core/network"
)

func randomConns(tb testing.TB) (c [5000]inet.Conn) {
func randomConns(tb testing.TB) (c [5000]network.Conn) {
for i, _ := range c {
c[i] = randConn(tb, nil)
}
Expand Down
37 changes: 19 additions & 18 deletions p2p/net/connmgr/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"sync/atomic"
"time"

"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

logging "github.com/ipfs/go-log"
ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ type BasicConnMgr struct {
cancel func()
}

var _ ifconnmgr.ConnManager = (*BasicConnMgr)(nil)
var _ connmgr.ConnManager = (*BasicConnMgr)(nil)

type segment struct {
sync.Mutex
Expand Down Expand Up @@ -77,7 +78,7 @@ func (s *segment) tagInfoFor(p peer.ID) *peerInfo {
firstSeen: time.Now(), // this timestamp will be updated when the first Connected notification arrives.
temp: true,
tags: make(map[string]int),
conns: make(map[inet.Conn]time.Time),
conns: make(map[network.Conn]time.Time),
}
s.peers[p] = pi
return pi
Expand Down Expand Up @@ -153,7 +154,7 @@ type peerInfo struct {
value int // cached sum of all tag values
temp bool // this is a temporary entry holding early tags, and awaiting connections

conns map[inet.Conn]time.Time // start time of each connection
conns map[network.Conn]time.Time // start time of each connection

firstSeen time.Time // timestamp when we began tracking this peer.
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func (cm *BasicConnMgr) background() {

// getConnsToClose runs the heuristics described in TrimOpenConns and returns the
// connections to close.
func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {
func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []network.Conn {
if cm.lowWater == 0 || cm.highWater == 0 {
// disabled
return nil
Expand Down Expand Up @@ -248,7 +249,7 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {
target := nconns - cm.lowWater

// slightly overallocate because we may have more than one conns per peer
selected := make([]inet.Conn, 0, target+10)
selected := make([]network.Conn, 0, target+10)

for _, inf := range candidates {
if target <= 0 {
Expand Down Expand Up @@ -281,7 +282,7 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {

// GetTagInfo is called to fetch the tag information associated with a given
// peer, nil is returned if p refers to an unknown peer.
func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo {
func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *connmgr.TagInfo {
s := cm.segments.get(p)
s.Lock()
defer s.Unlock()
Expand All @@ -291,7 +292,7 @@ func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo {
return nil
}

out := &ifconnmgr.TagInfo{
out := &connmgr.TagInfo{
FirstSeen: pi.firstSeen,
Value: pi.value,
Tags: make(map[string]int),
Expand Down Expand Up @@ -384,7 +385,7 @@ func (cm *BasicConnMgr) GetInfo() CMInfo {
// Notifee returns a sink through which Notifiers can inform the BasicConnMgr when
// events occur. Currently, the notifee only reacts upon connection events
// {Connected, Disconnected}.
func (cm *BasicConnMgr) Notifee() inet.Notifiee {
func (cm *BasicConnMgr) Notifee() network.Notifiee {
return (*cmNotifee)(cm)
}

Expand All @@ -397,7 +398,7 @@ func (nn *cmNotifee) cm() *BasicConnMgr {
// Connected is called by notifiers to inform that a new connection has been established.
// The notifee updates the BasicConnMgr to start tracking the connection. If the new connection
// count exceeds the high watermark, a trim may be triggered.
func (nn *cmNotifee) Connected(n inet.Network, c inet.Conn) {
func (nn *cmNotifee) Connected(n network.Network, c network.Conn) {
cm := nn.cm()

p := c.RemotePeer()
Expand All @@ -412,7 +413,7 @@ func (nn *cmNotifee) Connected(n inet.Network, c inet.Conn) {
id: id,
firstSeen: time.Now(),
tags: make(map[string]int),
conns: make(map[inet.Conn]time.Time),
conns: make(map[network.Conn]time.Time),
}
s.peers[id] = pinfo
} else if pinfo.temp {
Expand All @@ -435,7 +436,7 @@ func (nn *cmNotifee) Connected(n inet.Network, c inet.Conn) {

// Disconnected is called by notifiers to inform that an existing connection has been closed or terminated.
// The notifee updates the BasicConnMgr accordingly to stop tracking the connection, and performs housekeeping.
func (nn *cmNotifee) Disconnected(n inet.Network, c inet.Conn) {
func (nn *cmNotifee) Disconnected(n network.Network, c network.Conn) {
cm := nn.cm()

p := c.RemotePeer()
Expand Down Expand Up @@ -463,13 +464,13 @@ func (nn *cmNotifee) Disconnected(n inet.Network, c inet.Conn) {
}

// Listen is no-op in this implementation.
func (nn *cmNotifee) Listen(n inet.Network, addr ma.Multiaddr) {}
func (nn *cmNotifee) Listen(n network.Network, addr ma.Multiaddr) {}

// ListenClose is no-op in this implementation.
func (nn *cmNotifee) ListenClose(n inet.Network, addr ma.Multiaddr) {}
func (nn *cmNotifee) ListenClose(n network.Network, addr ma.Multiaddr) {}

// OpenedStream is no-op in this implementation.
func (nn *cmNotifee) OpenedStream(inet.Network, inet.Stream) {}
func (nn *cmNotifee) OpenedStream(network.Network, network.Stream) {}

// ClosedStream is no-op in this implementation.
func (nn *cmNotifee) ClosedStream(inet.Network, inet.Stream) {}
func (nn *cmNotifee) ClosedStream(network.Network, network.Stream) {}
26 changes: 14 additions & 12 deletions p2p/net/connmgr/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"time"

detectrace "github.com/ipfs/go-detect-race"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
tu "github.com/libp2p/go-testutil"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

tu "github.com/libp2p/go-libp2p-core/test"
ma "github.com/multiformats/go-multiaddr"
)

type tconn struct {
inet.Conn
network.Conn

peer peer.ID
closed bool
disconnectNotify func(net inet.Network, conn inet.Conn)
disconnectNotify func(net network.Network, conn network.Conn)
}

func (c *tconn) Close() error {
Expand All @@ -40,7 +42,7 @@ func (c *tconn) RemoteMultiaddr() ma.Multiaddr {
return addr
}

func randConn(t testing.TB, discNotify func(inet.Network, inet.Conn)) inet.Conn {
func randConn(t testing.TB, discNotify func(network.Network, network.Conn)) network.Conn {
pid := tu.RandPeerIDFatal(t)
return &tconn{peer: pid, disconnectNotify: discNotify}
}
Expand All @@ -49,7 +51,7 @@ func TestConnTrimming(t *testing.T) {
cm := NewConnManager(200, 300, 0)
not := cm.Notifee()

var conns []inet.Conn
var conns []network.Conn
for i := 0; i < 300; i++ {
rc := randConn(t, nil)
conns = append(conns, rc)
Expand Down Expand Up @@ -310,7 +312,7 @@ func TestQuickBurstRespectsSilencePeriod(t *testing.T) {
cm := NewConnManager(10, 20, 0)
not := cm.Notifee()

var conns []inet.Conn
var conns []network.Conn

// quickly produce 30 connections (sending us above the high watermark)
for i := 0; i < 30; i++ {
Expand Down Expand Up @@ -349,7 +351,7 @@ func TestPeerProtectionSingleTag(t *testing.T) {
not := cm.Notifee()

// produce 20 connections with unique peers.
var conns []inet.Conn
var conns []network.Conn
for i := 0; i < 20; i++ {
rc := randConn(t, not.Disconnected)
conns = append(conns, rc)
Expand All @@ -358,7 +360,7 @@ func TestPeerProtectionSingleTag(t *testing.T) {
}

// protect the first 5 peers.
var protected []inet.Conn
var protected []network.Conn
for _, c := range conns[0:5] {
cm.Protect(c.RemotePeer(), "global")
protected = append(protected, c)
Expand Down Expand Up @@ -412,7 +414,7 @@ func TestPeerProtectionMultipleTags(t *testing.T) {
not := cm.Notifee()

// produce 20 connections with unique peers.
var conns []inet.Conn
var conns []network.Conn
for i := 0; i < 20; i++ {
rc := randConn(t, not.Disconnected)
conns = append(conns, rc)
Expand All @@ -421,7 +423,7 @@ func TestPeerProtectionMultipleTags(t *testing.T) {
}

// protect the first 5 peers under two tags.
var protected []inet.Conn
var protected []network.Conn
for _, c := range conns[0:5] {
cm.Protect(c.RemotePeer(), "tag1")
cm.Protect(c.RemotePeer(), "tag2")
Expand Down

0 comments on commit 65aca08

Please sign in to comment.