-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransit.go
165 lines (137 loc) · 3.82 KB
/
transit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package context
import (
"bytes"
"context"
"encoding"
"github.com/deixis/spine/context/contextpb"
"github.com/google/uuid"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
// TransitFactory creates empty Transit instances
var TransitFactory = func() Transit {
return &transit{
ID: uuid.New().String(),
Stepper: newStepper(),
}
}
// A Transit is request context that goes beyond a process. It is composed of
// multiple `Leg`s.
type Transit interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
encoding.TextMarshaler
encoding.TextUnmarshaler
Leg
// Transmit returns a new instance of `Transmit` that can be serialised onto
// an outbound request.
Transmit() Transit
// Inject injects `Transit` for propagation within `carrier`.
// The actual type of `carrier` depends on the value of `format`.
// Inject(format interface{}, carrier interface{}) error
// Extract extracts `Transit` data from `carrier`.
// Extract(format interface{}, carrier interface{}) error
}
type transit struct {
ID string
Stepper *stepper
// Origin - Inbound node
}
// UUID returns the transit UUID
func (t *transit) UUID() string {
if t == nil {
return ""
}
return t.ID
}
// ShortID returns a partial representation of the transit ID (first 8 chars).
func (t *transit) ShortID() string {
if t == nil || len(t.ID) < 8 {
return ""
}
return string(t.ID[:8])
}
func (t *transit) Tick() uint {
return t.Stepper.Inc()
}
func (t *transit) Step() Step {
return t.Stepper
}
func (t *transit) Transmit() Transit {
return &transit{
ID: t.ID,
Stepper: t.Stepper.Child(),
}
}
func (t *transit) Inject(format interface{}, carrier interface{}) error {
return nil
}
func (t *transit) Extract(format interface{}, carrier interface{}) error {
return nil
}
func (t *transit) MarshalBinary() (data []byte, err error) {
return proto.Marshal(&contextpb.Context{
ID: t.ID,
Stepper: t.Stepper.String(),
})
}
func (t *transit) UnmarshalBinary(data []byte) error {
pb := &contextpb.Context{}
if err := proto.Unmarshal(data, pb); err != nil {
return ErrInvalidTransitBinary
}
t.ID = pb.ID
return t.Stepper.UnmarshalText([]byte(pb.Stepper))
}
var (
textMarshallerSep = []byte("#")
// ErrInvalidTransitBinary occurs when UnmarshalBinary is called on Transit
// with an invalid binary representation
ErrInvalidTransitBinary = errors.New("invalid transit binary representation")
// ErrInvalidTransitText occurs when UnmarshalText is called on Transit
// with an invalid textual representation
ErrInvalidTransitText = errors.New("invalid transit textual representation")
)
func (t *transit) MarshalText() (text []byte, err error) {
id := []byte(t.ID)
stepper, err := t.Stepper.MarshalText()
if err != nil {
return nil, err
}
return bytes.Join([][]byte{
id,
stepper,
}, textMarshallerSep), nil
}
func (t *transit) UnmarshalText(text []byte) error {
r := bytes.Split(text, textMarshallerSep)
if len(r) < 2 {
return ErrInvalidTransitText
}
t.ID = string(r[0])
t.Stepper = newStepper()
if err := t.Stepper.UnmarshalText(r[1]); err != nil {
return ErrInvalidTransitText
}
return nil
}
type transitKey struct{}
var activeTransitKey = transitKey{}
// TransitFromContext extracts `Transit` from context and returns `nil` when
// no instance of `Transit` can be found
func TransitFromContext(ctx context.Context) Transit {
val := ctx.Value(activeTransitKey)
if o, ok := val.(Transit); ok {
return o
}
return nil
}
// TransitWithContext injects `Transit` to context
func TransitWithContext(ctx context.Context, t Transit) context.Context {
return context.WithValue(ctx, activeTransitKey, t)
}
// NewTransitWithContext injects a new `Transit` to context
func NewTransitWithContext(ctx context.Context) (context.Context, Transit) {
tr := TransitFactory()
return context.WithValue(ctx, activeTransitKey, tr), tr
}