mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Move Miscellaneous Shared/ Packages Into Semantic Groups (#9624)
* remove shared network and ip util * forkutil * gaz * build * gaz * nogo * genrule * gaz Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
25
runtime/messagehandler/BUILD.bazel
Normal file
25
runtime/messagehandler/BUILD.bazel
Normal file
@@ -0,0 +1,25 @@
|
||||
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["messagehandler.go"],
|
||||
importpath = "github.com/prysmaticlabs/prysm/runtime/messagehandler",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"@com_github_libp2p_go_libp2p_pubsub//:go_default_library",
|
||||
"@com_github_sirupsen_logrus//:go_default_library",
|
||||
"@io_opencensus_go//trace:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
size = "small",
|
||||
srcs = ["messagehandler_test.go"],
|
||||
deps = [
|
||||
":go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_pubsub//:go_default_library",
|
||||
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
|
||||
],
|
||||
)
|
||||
61
runtime/messagehandler/messagehandler.go
Normal file
61
runtime/messagehandler/messagehandler.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Package messagehandler contains useful helpers for recovering
|
||||
// from panic conditions at runtime and logging their trace.
|
||||
package messagehandler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
const noMsgData = "message contains no data"
|
||||
|
||||
var log = logrus.WithField("prefix", "message-handler")
|
||||
|
||||
// SafelyHandleMessage will recover and log any panic that occurs from the
|
||||
// function argument.
|
||||
func SafelyHandleMessage(ctx context.Context, fn func(ctx context.Context, message *pubsub.Message) error, msg *pubsub.Message) {
|
||||
defer HandlePanic(ctx, msg)
|
||||
|
||||
// Fingers crossed that it doesn't panic...
|
||||
if err := fn(ctx, msg); err != nil {
|
||||
// Report any error on the span, if one exists.
|
||||
if span := trace.FromContext(ctx); span != nil {
|
||||
span.SetStatus(trace.Status{
|
||||
Code: trace.StatusCodeInternal,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HandlePanic returns a panic handler function that is used to
|
||||
// capture a panic.
|
||||
func HandlePanic(ctx context.Context, msg *pubsub.Message) {
|
||||
if r := recover(); r != nil {
|
||||
printedMsg := noMsgData
|
||||
if msg != nil {
|
||||
printedMsg = msg.String()
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"r": r,
|
||||
"msg": printedMsg,
|
||||
}).Error("Panicked when handling p2p message! Recovering...")
|
||||
|
||||
debug.PrintStack()
|
||||
|
||||
if ctx == nil {
|
||||
return
|
||||
}
|
||||
if span := trace.FromContext(ctx); span != nil {
|
||||
span.SetStatus(trace.Status{
|
||||
Code: trace.StatusCodeInternal,
|
||||
Message: fmt.Sprintf("Panic: %v", r),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
36
runtime/messagehandler/messagehandler_test.go
Normal file
36
runtime/messagehandler/messagehandler_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package messagehandler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
||||
"github.com/prysmaticlabs/prysm/runtime/messagehandler"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
logTest "github.com/sirupsen/logrus/hooks/test"
|
||||
)
|
||||
|
||||
func TestSafelyHandleMessage(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
|
||||
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
|
||||
panic("bad!")
|
||||
return nil
|
||||
}, &pubsub.Message{})
|
||||
|
||||
require.LogsContain(t, hook, "Panicked when handling p2p message!")
|
||||
}
|
||||
|
||||
func TestSafelyHandleMessage_NoData(t *testing.T) {
|
||||
hook := logTest.NewGlobal()
|
||||
|
||||
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
|
||||
panic("bad!")
|
||||
return nil
|
||||
}, nil)
|
||||
|
||||
entry := hook.LastEntry()
|
||||
if entry.Data["msg"] != "message contains no data" {
|
||||
t.Errorf("Message logged was not what was expected: %s", entry.Data["msg"])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user