From 3d2ba374454f003f500d1b88effe648c8935572a Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sun, 19 Oct 2014 06:38:13 -0700 Subject: [PATCH] moved ctxcloser to own pkg --- net/conn/conn.go | 5 +++-- net/conn/interface.go | 3 ++- net/conn/listen.go | 5 +++-- net/conn/multiconn.go | 5 +++-- net/conn/secure_conn.go | 5 +++-- net/swarm/swarm.go | 5 +++-- {net/conn => util/ctxcloser}/closer.go | 2 +- 7 files changed, 18 insertions(+), 12 deletions(-) rename {net/conn => util/ctxcloser}/closer.go (99%) diff --git a/net/conn/conn.go b/net/conn/conn.go index 5741cc1d5..00d4a91e9 100644 --- a/net/conn/conn.go +++ b/net/conn/conn.go @@ -11,6 +11,7 @@ import ( peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" ) var log = u.Logger("conn") @@ -46,7 +47,7 @@ type singleConn struct { maconn manet.Conn msgio *msgioPipe - ContextCloser + ctxc.ContextCloser } // newConn constructs a new connection @@ -60,7 +61,7 @@ func newSingleConn(ctx context.Context, local, remote *peer.Peer, msgio: newMsgioPipe(10), } - conn.ContextCloser = NewContextCloser(ctx, conn.close) + conn.ContextCloser = ctxc.NewContextCloser(ctx, conn.close) log.Info("newSingleConn: %v to %v", local, remote) diff --git a/net/conn/interface.go b/net/conn/interface.go index d079490e7..5cfd8336d 100644 --- a/net/conn/interface.go +++ b/net/conn/interface.go @@ -3,6 +3,7 @@ package conn import ( peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ) @@ -13,7 +14,7 @@ type Map map[u.Key]Conn // Conn is a generic message-based Peer-to-Peer connection. type Conn interface { // implement ContextCloser too! - ContextCloser + ctxc.ContextCloser // ID is an identifier unique to this connection. ID() string diff --git a/net/conn/listen.go b/net/conn/listen.go index 726bb9629..20cfbb4fb 100644 --- a/net/conn/listen.go +++ b/net/conn/listen.go @@ -6,6 +6,7 @@ import ( manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net" peer "github.com/jbenet/go-ipfs/peer" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" ) // listener is an object that can accept connections. It implements Listener @@ -31,7 +32,7 @@ type listener struct { ctx context.Context // embedded ContextCloser - ContextCloser + ctxc.ContextCloser } // disambiguate @@ -140,7 +141,7 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local *peer.Peer, peers peer // This is because the parent context will be given to all connections too, // and if we close the listener, the connections shouldn't share the fate. ctx2, _ := context.WithCancel(ctx) - l.ContextCloser = NewContextCloser(ctx2, l.close) + l.ContextCloser = ctxc.NewContextCloser(ctx2, l.close) go l.listen() diff --git a/net/conn/multiconn.go b/net/conn/multiconn.go index 26c64be71..24b4cc994 100644 --- a/net/conn/multiconn.go +++ b/net/conn/multiconn.go @@ -8,6 +8,7 @@ import ( peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" ) // MultiConnMap is for shorthand @@ -34,7 +35,7 @@ type MultiConn struct { // for adding/removing connections concurrently sync.RWMutex - ContextCloser + ctxc.ContextCloser } // NewMultiConn constructs a new connection @@ -51,7 +52,7 @@ func NewMultiConn(ctx context.Context, local, remote *peer.Peer, conns []Conn) ( } // must happen before Adds / fanOut - c.ContextCloser = NewContextCloser(ctx, c.close) + c.ContextCloser = ctxc.NewContextCloser(ctx, c.close) if conns != nil && len(conns) > 0 { c.Add(conns...) diff --git a/net/conn/secure_conn.go b/net/conn/secure_conn.go index ac51cb429..dfccbaf2e 100644 --- a/net/conn/secure_conn.go +++ b/net/conn/secure_conn.go @@ -8,6 +8,7 @@ import ( spipe "github.com/jbenet/go-ipfs/crypto/spipe" peer "github.com/jbenet/go-ipfs/peer" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" ) // secureConn wraps another Conn object with an encrypted channel. @@ -19,7 +20,7 @@ type secureConn struct { // secure pipe, wrapping insecure secure *spipe.SecurePipe - ContextCloser + ctxc.ContextCloser } // newConn constructs a new connection @@ -28,7 +29,7 @@ func newSecureConn(ctx context.Context, insecure Conn, peers peer.Peerstore) (Co conn := &secureConn{ insecure: insecure, } - conn.ContextCloser = NewContextCloser(ctx, conn.close) + conn.ContextCloser = ctxc.NewContextCloser(ctx, conn.close) log.Debug("newSecureConn: %v to %v", insecure.LocalPeer(), insecure.RemotePeer()) // perform secure handshake before returning this connection. diff --git a/net/swarm/swarm.go b/net/swarm/swarm.go index a81b872e0..157a9ff92 100644 --- a/net/swarm/swarm.go +++ b/net/swarm/swarm.go @@ -9,6 +9,7 @@ import ( msg "github.com/jbenet/go-ipfs/net/message" peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" + ctxc "github.com/jbenet/go-ipfs/util/ctxcloser" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" ) @@ -64,7 +65,7 @@ type Swarm struct { listeners []conn.Listener // ContextCloser - conn.ContextCloser + ctxc.ContextCloser } // NewSwarm constructs a Swarm, with a Chan. @@ -78,7 +79,7 @@ func NewSwarm(ctx context.Context, local *peer.Peer, ps peer.Peerstore) (*Swarm, } // ContextCloser for proper child management. - s.ContextCloser = conn.NewContextCloser(ctx, s.close) + s.ContextCloser = ctxc.NewContextCloser(ctx, s.close) go s.fanOut() return s, s.listen() diff --git a/net/conn/closer.go b/util/ctxcloser/closer.go similarity index 99% rename from net/conn/closer.go rename to util/ctxcloser/closer.go index 6eb3f8ad4..e04178c24 100644 --- a/net/conn/closer.go +++ b/util/ctxcloser/closer.go @@ -1,4 +1,4 @@ -package conn +package ctxcloser import ( "sync"