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:
Raul Jordan
2021-09-17 14:20:50 -05:00
committed by GitHub
parent 6308678a89
commit d2f74615ab
90 changed files with 190 additions and 193 deletions

View 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",
],
)

View 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),
})
}
}
}

View 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"])
}
}