You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
865 B
48 lines
865 B
11 months ago
|
package correlation
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/hex"
|
||
|
)
|
||
|
|
||
|
const Key string = "correlation_id"
|
||
|
|
||
|
var Empty CorrelationID = CorrelationID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||
|
|
||
|
// Correlation ID is a byte array of length 16
|
||
|
type CorrelationID []byte
|
||
|
|
||
|
func (ci CorrelationID) String() string {
|
||
|
if ci == nil {
|
||
|
return hex.EncodeToString(Empty)
|
||
|
}
|
||
|
return hex.EncodeToString(ci)
|
||
|
}
|
||
|
|
||
|
func FromContext(ctx context.Context) CorrelationID {
|
||
|
if v := ctx.Value("correlation_id"); v != nil {
|
||
|
if c, ok := v.(CorrelationID); ok {
|
||
|
return c
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
type Sequencer interface {
|
||
|
Next() []byte
|
||
|
}
|
||
|
|
||
|
type Generator struct {
|
||
|
seq Sequencer
|
||
|
}
|
||
|
|
||
|
func NewGenerator(seq Sequencer) *Generator {
|
||
|
return &Generator{
|
||
|
seq: seq,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (g *Generator) Next() CorrelationID {
|
||
|
return CorrelationID(g.seq.Next())
|
||
|
}
|