prysm-pulse/beacon-chain/sync/rpc_goodbye.go
Preston Van Loon d5ddd012bc
Enforce error handling and checking type assertions (#5403)
* Enforce error handling and checking type assertions
* Reference issue #5404 in the TODO message
* doc description
* Merge branch 'master' into errcheck
* fix tests and address @nisdas feedbacK
* gaz
* fix docker image
2020-04-13 04:11:09 +00:00

51 lines
1.2 KiB
Go

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.
func (r *Service) goodbyeRPCHandler(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
defer func() {
if err := stream.Close(); err != nil {
log.WithError(err).Error("Failed to close stream")
}
}()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
setRPCStreamDeadlines(stream)
m, ok := msg.(*uint64)
if !ok {
return fmt.Errorf("wrong message type for goodbye, got %T, wanted *uint64", msg)
}
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)
}