prysm-pulse/beacon-chain/sync/rpc_goodbye.go

44 lines
1.0 KiB
Go
Raw Normal View History

package sync
import (
"context"
"fmt"
"time"
libp2pcore "github.com/libp2p/go-libp2p-core"
)
const (
codeClientShutdown uint64 = iota
codeWrongNetwork
codeGenericError
)
var goodByes = map[uint64]string{
codeClientShutdown: "client shutdown",
codeWrongNetwork: "irrelevant network",
codeGenericError: "fault/error",
}
// goodbyeRPCHandler reads the incoming goodbye rpc message from the peer.
2019-09-16 17:54:46 +00:00
func (r *RegularSync) goodbyeRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
defer stream.Close()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
setRPCStreamDeadlines(stream)
m := msg.(uint64)
log := log.WithField("Reason", goodbyeMessage(m))
log.WithField("peer", stream.Conn().RemotePeer()).Info("Peer has sent a goodbye message")
// closes all streams with the peer
return r.p2p.Disconnect(stream.Conn().RemotePeer())
}
func goodbyeMessage(num uint64) string {
reason, ok := goodByes[num]
if ok {
return reason
}
return fmt.Sprintf("unknown goodbye value of %d Received", num)
}