backend/app/brahma/api/murder/internal/pkg/ctx.go

22 lines
597 B
Go
Raw Permalink Normal View History

2022-01-19 19:39:04 +08:00
package pkg
import (
"context"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"google.golang.org/grpc/metadata"
"time"
)
func TransFiberCtx2NewOutgoingContext(fCtx *fiber.Ctx, time time.Duration) (context.Context, context.CancelFunc) {
ctx := fCtx.UserContext()
md := metadata.MD{}
if value, ok := ctx.Value(fiber.HeaderXRequestID).(string); ok {
md = metadata.Pairs(fiber.HeaderXRequestID, value)
} else {
md = metadata.Pairs(fiber.HeaderXRequestID, utils.UUIDv4())
}
//3s 过期超时
return context.WithTimeout(metadata.NewOutgoingContext(ctx, md), time)
}