|
@ -9,7 +9,6 @@ import ( |
|
|
"sync" |
|
|
"sync" |
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/crypto" |
|
|
"github.com/tendermint/tendermint/crypto" |
|
|
tmsync "github.com/tendermint/tendermint/internal/libs/sync" |
|
|
|
|
|
"github.com/tendermint/tendermint/libs/log" |
|
|
"github.com/tendermint/tendermint/libs/log" |
|
|
"github.com/tendermint/tendermint/types" |
|
|
"github.com/tendermint/tendermint/types" |
|
|
) |
|
|
) |
|
@ -175,10 +174,17 @@ func (t *MemoryTransport) Dial(ctx context.Context, endpoint Endpoint) (Connecti |
|
|
|
|
|
|
|
|
inCh := make(chan memoryMessage, t.bufferSize) |
|
|
inCh := make(chan memoryMessage, t.bufferSize) |
|
|
outCh := make(chan memoryMessage, t.bufferSize) |
|
|
outCh := make(chan memoryMessage, t.bufferSize) |
|
|
closer := tmsync.NewCloser() |
|
|
|
|
|
|
|
|
|
|
|
outConn := newMemoryConnection(t.logger, t.nodeID, peer.nodeID, inCh, outCh, closer) |
|
|
|
|
|
inConn := newMemoryConnection(peer.logger, peer.nodeID, t.nodeID, outCh, inCh, closer) |
|
|
|
|
|
|
|
|
once := &sync.Once{} |
|
|
|
|
|
closeCh := make(chan struct{}) |
|
|
|
|
|
closeFn := func() { once.Do(func() { close(closeCh) }) } |
|
|
|
|
|
|
|
|
|
|
|
outConn := newMemoryConnection(t.logger, t.nodeID, peer.nodeID, inCh, outCh) |
|
|
|
|
|
outConn.closeCh = closeCh |
|
|
|
|
|
outConn.closeFn = closeFn |
|
|
|
|
|
inConn := newMemoryConnection(peer.logger, peer.nodeID, t.nodeID, outCh, inCh) |
|
|
|
|
|
inConn.closeCh = closeCh |
|
|
|
|
|
inConn.closeFn = closeFn |
|
|
|
|
|
|
|
|
select { |
|
|
select { |
|
|
case peer.acceptCh <- inConn: |
|
|
case peer.acceptCh <- inConn: |
|
@ -202,7 +208,9 @@ type MemoryConnection struct { |
|
|
|
|
|
|
|
|
receiveCh <-chan memoryMessage |
|
|
receiveCh <-chan memoryMessage |
|
|
sendCh chan<- memoryMessage |
|
|
sendCh chan<- memoryMessage |
|
|
closer *tmsync.Closer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
closeFn func() |
|
|
|
|
|
closeCh <-chan struct{} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// memoryMessage is passed internally, containing either a message or handshake.
|
|
|
// memoryMessage is passed internally, containing either a message or handshake.
|
|
@ -222,7 +230,6 @@ func newMemoryConnection( |
|
|
remoteID types.NodeID, |
|
|
remoteID types.NodeID, |
|
|
receiveCh <-chan memoryMessage, |
|
|
receiveCh <-chan memoryMessage, |
|
|
sendCh chan<- memoryMessage, |
|
|
sendCh chan<- memoryMessage, |
|
|
closer *tmsync.Closer, |
|
|
|
|
|
) *MemoryConnection { |
|
|
) *MemoryConnection { |
|
|
return &MemoryConnection{ |
|
|
return &MemoryConnection{ |
|
|
logger: logger.With("remote", remoteID), |
|
|
logger: logger.With("remote", remoteID), |
|
@ -230,7 +237,6 @@ func newMemoryConnection( |
|
|
remoteID: remoteID, |
|
|
remoteID: remoteID, |
|
|
receiveCh: receiveCh, |
|
|
receiveCh: receiveCh, |
|
|
sendCh: sendCh, |
|
|
sendCh: sendCh, |
|
|
closer: closer, |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -264,7 +270,7 @@ func (c *MemoryConnection) Handshake( |
|
|
select { |
|
|
select { |
|
|
case c.sendCh <- memoryMessage{nodeInfo: &nodeInfo, pubKey: privKey.PubKey()}: |
|
|
case c.sendCh <- memoryMessage{nodeInfo: &nodeInfo, pubKey: privKey.PubKey()}: |
|
|
c.logger.Debug("sent handshake", "nodeInfo", nodeInfo) |
|
|
c.logger.Debug("sent handshake", "nodeInfo", nodeInfo) |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-c.closeCh: |
|
|
return types.NodeInfo{}, nil, io.EOF |
|
|
return types.NodeInfo{}, nil, io.EOF |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
return types.NodeInfo{}, nil, ctx.Err() |
|
|
return types.NodeInfo{}, nil, ctx.Err() |
|
@ -277,7 +283,7 @@ func (c *MemoryConnection) Handshake( |
|
|
} |
|
|
} |
|
|
c.logger.Debug("received handshake", "peerInfo", msg.nodeInfo) |
|
|
c.logger.Debug("received handshake", "peerInfo", msg.nodeInfo) |
|
|
return *msg.nodeInfo, msg.pubKey, nil |
|
|
return *msg.nodeInfo, msg.pubKey, nil |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-c.closeCh: |
|
|
return types.NodeInfo{}, nil, io.EOF |
|
|
return types.NodeInfo{}, nil, io.EOF |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
return types.NodeInfo{}, nil, ctx.Err() |
|
|
return types.NodeInfo{}, nil, ctx.Err() |
|
@ -289,7 +295,7 @@ func (c *MemoryConnection) ReceiveMessage(ctx context.Context) (ChannelID, []byt |
|
|
// Check close first, since channels are buffered. Otherwise, below select
|
|
|
// Check close first, since channels are buffered. Otherwise, below select
|
|
|
// may non-deterministically return non-error even when closed.
|
|
|
// may non-deterministically return non-error even when closed.
|
|
|
select { |
|
|
select { |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-c.closeCh: |
|
|
return 0, nil, io.EOF |
|
|
return 0, nil, io.EOF |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
return 0, nil, io.EOF |
|
|
return 0, nil, io.EOF |
|
@ -300,7 +306,9 @@ func (c *MemoryConnection) ReceiveMessage(ctx context.Context) (ChannelID, []byt |
|
|
case msg := <-c.receiveCh: |
|
|
case msg := <-c.receiveCh: |
|
|
c.logger.Debug("received message", "chID", msg.channelID, "msg", msg.message) |
|
|
c.logger.Debug("received message", "chID", msg.channelID, "msg", msg.message) |
|
|
return msg.channelID, msg.message, nil |
|
|
return msg.channelID, msg.message, nil |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-ctx.Done(): |
|
|
|
|
|
return 0, nil, io.EOF |
|
|
|
|
|
case <-c.closeCh: |
|
|
return 0, nil, io.EOF |
|
|
return 0, nil, io.EOF |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -310,7 +318,7 @@ func (c *MemoryConnection) SendMessage(ctx context.Context, chID ChannelID, msg |
|
|
// Check close first, since channels are buffered. Otherwise, below select
|
|
|
// Check close first, since channels are buffered. Otherwise, below select
|
|
|
// may non-deterministically return non-error even when closed.
|
|
|
// may non-deterministically return non-error even when closed.
|
|
|
select { |
|
|
select { |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-c.closeCh: |
|
|
return io.EOF |
|
|
return io.EOF |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
return io.EOF |
|
|
return io.EOF |
|
@ -323,19 +331,10 @@ func (c *MemoryConnection) SendMessage(ctx context.Context, chID ChannelID, msg |
|
|
return nil |
|
|
return nil |
|
|
case <-ctx.Done(): |
|
|
case <-ctx.Done(): |
|
|
return io.EOF |
|
|
return io.EOF |
|
|
case <-c.closer.Done(): |
|
|
|
|
|
|
|
|
case <-c.closeCh: |
|
|
return io.EOF |
|
|
return io.EOF |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Close implements Connection.
|
|
|
// Close implements Connection.
|
|
|
func (c *MemoryConnection) Close() error { |
|
|
|
|
|
select { |
|
|
|
|
|
case <-c.closer.Done(): |
|
|
|
|
|
return nil |
|
|
|
|
|
default: |
|
|
|
|
|
c.closer.Close() |
|
|
|
|
|
c.logger.Info("closed connection") |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (c *MemoryConnection) Close() error { c.closeFn(); return nil } |