Skip to content

Commit

Permalink
extract interface
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Oct 6, 2017
1 parent 93b0a9a commit 1fd3dd4
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions p2p/net/connmgr/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@ import (
"time"

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"
)

var log = logging.Logger("connmgr")

type ConnManager interface {
TagPeer(peer.ID, string, int)
UntagPeer(peer.ID, string)
GetTagInfo(peer.ID) *TagInfo
TrimOpenConns(context.Context)
Notifee() inet.Notifiee
}

type connManager struct {
highWater int
lowWater int
Expand All @@ -37,9 +30,11 @@ type connManager struct {
lastTrim time.Time
}

var _ ifconnmgr.ConnManager = (*connManager)(nil)

var DefaultGracePeriod = time.Second * 10

func NewConnManager(low, hi int, grace time.Duration) ConnManager {
func NewConnManager(low, hi int, grace time.Duration) ifconnmgr.ConnManager {
return &connManager{
highWater: hi,
lowWater: low,
Expand All @@ -57,13 +52,6 @@ type peerInfo struct {
firstSeen time.Time
}

type TagInfo struct {
FirstSeen time.Time
Value int
Tags map[string]int
Conns map[string]time.Time
}

func (cm *connManager) TrimOpenConns(ctx context.Context) {
for _, c := range cm.getConnsToClose(ctx) {
c.Close()
Expand Down Expand Up @@ -117,7 +105,7 @@ func (cm *connManager) getConnsToClose(ctx context.Context) []io.Closer {
return closed
}

func (cm *connManager) GetTagInfo(p peer.ID) *TagInfo {
func (cm *connManager) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo {
cm.lk.Lock()
defer cm.lk.Unlock()

Expand All @@ -126,7 +114,7 @@ func (cm *connManager) GetTagInfo(p peer.ID) *TagInfo {
return nil
}

out := &TagInfo{
out := &ifconnmgr.TagInfo{
FirstSeen: pi.firstSeen,
Value: pi.value,
Tags: make(map[string]int),
Expand Down

0 comments on commit 1fd3dd4

Please sign in to comment.