Skip to content

Commit

Permalink
Fix #16: do not close Host when externally provided
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
  • Loading branch information
hsanjuan committed Jan 3, 2017
1 parent d9d6072 commit 2c6e37d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 10 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ var (
// (AppendPipeline). This ensures a connection (and a stream) will stay open
// between peers and reduces the cost of opening/closing individual streams.
type Libp2pTransport struct {
host p2phost.Host
ctx context.Context
host p2phost.Host
extHost bool

ctx context.Context

consumeCh chan raft.RPC

Expand Down Expand Up @@ -150,6 +152,7 @@ func NewLibp2pTransport(localPeer *Peer, clusterPeers []*Peer) (*Libp2pTransport

t := &Libp2pTransport{
host: host,
extHost: false,
ctx: ctx,
consumeCh: make(chan raft.RPC),
shutdownCh: make(chan struct{}),
Expand All @@ -175,6 +178,7 @@ func NewLibp2pTransportWithHost(host p2phost.Host) (*Libp2pTransport, error) {
ctx := context.Background()
t := &Libp2pTransport{
host: host,
extHost: true,
ctx: ctx,
consumeCh: make(chan raft.RPC),
shutdownCh: make(chan struct{}),
Expand Down Expand Up @@ -558,7 +562,10 @@ func (t *Libp2pTransport) Close() error {
if !t.shutdown {
close(t.shutdownCh)
t.shutdown = true
t.host.Close()
// Close the host only when it was created by us
if !t.extHost {
t.host.Close()
}
}
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ func TestNewLibp2pTransportWithHost(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer tr1.Close()

raft2, _, tr2, err := makeTestingRaft(peer2, peers2, nil)
if err != nil {
t.Fatal(err)
}
defer tr2.Close()

raft1.Shutdown()
raft2.Shutdown()

trWithHost1, err1 := NewLibp2pTransportWithHost(tr1.host)
defer trWithHost1.Close()
trWithHost2, err2 := NewLibp2pTransportWithHost(tr2.host)

defer tr1.Close() // This will shutdown the host
defer tr2.Close()
defer trWithHost1.Close() // This shutsdown transport but not host
defer trWithHost2.Close()

if err1 != nil || err2 != nil {
Expand Down

0 comments on commit 2c6e37d

Please sign in to comment.