Refactor adapter
This commit is contained in:
18
log/id.go
18
log/id.go
@@ -3,13 +3,31 @@ package log
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
|
||||
"github.com/sagernet/sing/common/random"
|
||||
)
|
||||
|
||||
func init() {
|
||||
random.InitializeSeed()
|
||||
}
|
||||
|
||||
var idType = (*idContext)(nil)
|
||||
|
||||
type idContext struct {
|
||||
context.Context
|
||||
id uint32
|
||||
}
|
||||
|
||||
func (c *idContext) Value(key any) any {
|
||||
if key == idType {
|
||||
return c
|
||||
}
|
||||
return c.Context.Value(key)
|
||||
}
|
||||
|
||||
func ContextWithID(ctx context.Context) context.Context {
|
||||
if ctx.Value(idType) != nil {
|
||||
return ctx
|
||||
}
|
||||
return &idContext{ctx, rand.Uint32()}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user