network: remove useless Done() method from the peer

It's internal state of the peer that no one should care about.
This commit is contained in:
Roman Khimov 2020-01-15 18:25:58 +03:00
parent f39d5d5a10
commit 7f0882767c
3 changed files with 16 additions and 35 deletions

View file

@ -183,10 +183,6 @@ func (p *localPeer) WriteMsg(msg *Message) error {
p.messageHandler(p.t, msg) p.messageHandler(p.t, msg)
return nil return nil
} }
func (p *localPeer) Done() chan error {
done := make(chan error)
return done
}
func (p *localPeer) Version() *payload.Version { func (p *localPeer) Version() *payload.Version {
return p.version return p.version
} }

View file

@ -19,7 +19,6 @@ type Peer interface {
PeerAddr() net.Addr PeerAddr() net.Addr
Disconnect(error) Disconnect(error)
WriteMsg(msg *Message) error WriteMsg(msg *Message) error
Done() chan error
Version() *payload.Version Version() *payload.Version
LastBlockIndex() uint32 LastBlockIndex() uint32
UpdateLastBlockIndex(lbIndex uint32) UpdateLastBlockIndex(lbIndex uint32)

View file

@ -38,9 +38,10 @@ type TCPPeer struct {
lastBlockIndex uint32 lastBlockIndex uint32
lock sync.RWMutex lock sync.RWMutex
finale sync.Once
handShake handShakeStage handShake handShakeStage
done chan error done chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
@ -53,7 +54,7 @@ func NewTCPPeer(conn net.Conn, s *Server) *TCPPeer {
return &TCPPeer{ return &TCPPeer{
conn: conn, conn: conn,
server: s, server: s,
done: make(chan error, 1), done: make(chan struct{}),
} }
} }
@ -68,10 +69,6 @@ func (p *TCPPeer) WriteMsg(msg *Message) error {
} }
func (p *TCPPeer) writeMsg(msg *Message) error { func (p *TCPPeer) writeMsg(msg *Message) error {
select {
case err := <-p.done:
return err
default:
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
if err := msg.Encode(w.BinWriter); err != nil { if err := msg.Encode(w.BinWriter); err != nil {
return err return err
@ -80,7 +77,6 @@ func (p *TCPPeer) writeMsg(msg *Message) error {
_, err := p.conn.Write(w.Bytes()) _, err := p.conn.Write(w.Bytes())
return err return err
}
} }
// handleConn handles the read side of the connection, it should be started as // handleConn handles the read side of the connection, it should be started as
@ -137,8 +133,8 @@ func (p *TCPPeer) StartProtocol() {
pingTimer := time.NewTimer(p.server.PingTimeout) pingTimer := time.NewTimer(p.server.PingTimeout)
for { for {
select { select {
case err = <-p.Done(): case <-p.done:
// time to stop return
case m := <-p.server.addrReq: case m := <-p.server.addrReq:
err = p.WriteMsg(m) err = p.WriteMsg(m)
case <-timer.C: case <-timer.C:
@ -273,23 +269,13 @@ func (p *TCPPeer) PeerAddr() net.Addr {
return tcpAddr return tcpAddr
} }
// Done implements the Peer interface and notifies
// all other resources operating on it that this peer
// is no longer running.
func (p *TCPPeer) Done() chan error {
return p.done
}
// Disconnect will fill the peer's done channel with the given error. // Disconnect will fill the peer's done channel with the given error.
func (p *TCPPeer) Disconnect(err error) { func (p *TCPPeer) Disconnect(err error) {
p.finale.Do(func() {
p.server.unregister <- peerDrop{p, err} p.server.unregister <- peerDrop{p, err}
p.conn.Close() p.conn.Close()
select { close(p.done)
case p.done <- err: })
// one message to the queue
default:
// the other side may already be gone, it's OK
}
} }
// Version implements the Peer interface. // Version implements the Peer interface.