fix p2p error related to beacon chain (#142)

* fix p2p error related to beacon chain

* add log
pull/145/head
Richard Liu 6 years ago committed by GitHub
parent 579b357c5d
commit edbcb88d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      p2p/host/hostv1/hostv1.go

@ -13,14 +13,14 @@ import (
type HostV1 struct {
self p2p.Peer
listener net.Listener
quit chan bool
quit chan struct{}
}
// New creates a HostV1
func New(self p2p.Peer) *HostV1 {
h := &HostV1{
self: self,
quit: make(chan bool, 1),
quit: make(chan struct{}, 1),
}
return h
}
@ -46,12 +46,14 @@ func (host *HostV1) BindHandlerAndServe(handler p2p.StreamHandler) {
}
backoff := p2p.NewExpBackoff(250*time.Millisecond, 15*time.Second, 2.0)
for { // Keep listening
conn, err := host.listener.Accept()
select {
case <-host.quit:
// If we've already received quit signal, simply ignore the error and return
log.Info("Quit host", "addr", net.JoinHostPort(host.self.IP, host.self.Port))
return
default:
{
conn, err := host.listener.Accept()
if err != nil {
log.Error("Error listening on port.", "port", port,
"err", err)
@ -94,6 +96,6 @@ func (host *HostV1) SendMessage(peer p2p.Peer, message []byte) (err error) {
// Close closes the host
func (host *HostV1) Close() error {
host.quit <- true
host.quit <- struct{}{}
return host.listener.Close()
}

Loading…
Cancel
Save