-
Notifications
You must be signed in to change notification settings - Fork 196
/
context.go
198 lines (166 loc) · 6.24 KB
/
context.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2014 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eventhorizon
import (
"context"
"sync"
"time"
"github.com/google/uuid"
)
// DefaultNamespace is the namespace to use if not set in the context.
const DefaultNamespace = "default"
// DefaultMinVersionDeadline is the deadline to use when creating a min version
// context that waits.
const DefaultMinVersionDeadline = 10 * time.Second
// Strings used to marshal context values.
const (
namespaceKeyStr = "eh_namespace"
aggregateIDKeyStr = "eh_aggregate_id"
aggregateTypeKeyStr = "eh_aggregate_type"
commandTypeKeyStr = "eh_command_type"
)
func init() {
RegisterContextMarshaler(func(ctx context.Context, vals map[string]interface{}) {
if ns, ok := ctx.Value(namespaceKey).(string); ok {
vals[namespaceKeyStr] = ns
}
if aggregateID, ok := AggregateIDFromContext(ctx); ok {
vals[aggregateIDKeyStr] = aggregateID.String()
}
if aggregateType, ok := AggregateTypeFromContext(ctx); ok {
vals[aggregateTypeKeyStr] = string(aggregateType)
}
if commandType, ok := CommandTypeFromContext(ctx); ok {
vals[commandTypeKeyStr] = string(commandType)
}
})
RegisterContextUnmarshaler(func(ctx context.Context, vals map[string]interface{}) context.Context {
if ns, ok := vals[namespaceKeyStr].(string); ok {
ctx = NewContextWithNamespace(ctx, ns)
}
if aggregateIDStr, ok := vals[aggregateIDKeyStr].(string); ok {
if aggregateID, err := uuid.Parse(aggregateIDStr); err == nil {
ctx = NewContextWithAggregateID(ctx, aggregateID)
}
}
if aggregateType, ok := vals[aggregateTypeKeyStr].(string); ok {
ctx = NewContextWithAggregateType(ctx, AggregateType(aggregateType))
}
if commandType, ok := vals[commandTypeKeyStr].(string); ok {
ctx = NewContextWithCommandType(ctx, CommandType(commandType))
}
return ctx
})
}
type contextKey int
const (
namespaceKey contextKey = iota
aggregateIDKey
aggregateTypeKey
commandTypeKey
)
// AggregateIDFromContext return the command type from the context.
func AggregateIDFromContext(ctx context.Context) (uuid.UUID, bool) {
aggregateID, ok := ctx.Value(aggregateIDKey).(uuid.UUID)
return aggregateID, ok
}
// AggregateTypeFromContext return the command type from the context.
func AggregateTypeFromContext(ctx context.Context) (AggregateType, bool) {
aggregateType, ok := ctx.Value(aggregateTypeKey).(AggregateType)
return aggregateType, ok
}
// CommandTypeFromContext return the command type from the context.
func CommandTypeFromContext(ctx context.Context) (CommandType, bool) {
commandType, ok := ctx.Value(commandTypeKey).(CommandType)
return commandType, ok
}
// NewContextWithAggregateID adds a aggregate ID on the context.
func NewContextWithAggregateID(ctx context.Context, aggregateID uuid.UUID) context.Context {
return context.WithValue(ctx, aggregateIDKey, aggregateID)
}
// NewContextWithAggregateType adds a aggregate type on the context.
func NewContextWithAggregateType(ctx context.Context, aggregateType AggregateType) context.Context {
return context.WithValue(ctx, aggregateTypeKey, aggregateType)
}
// NewContextWithCommandType adds a command type on the context.
func NewContextWithCommandType(ctx context.Context, commandType CommandType) context.Context {
return context.WithValue(ctx, commandTypeKey, commandType)
}
// NamespaceFromContext returns the namespace from the context, or the default
// namespace.
func NamespaceFromContext(ctx context.Context) string {
if ns, ok := ctx.Value(namespaceKey).(string); ok {
return ns
}
return DefaultNamespace
}
// NewContextWithNamespace sets the namespace to use in the context. The
// namespace is used to determine which database.
func NewContextWithNamespace(ctx context.Context, namespace string) context.Context {
return context.WithValue(ctx, namespaceKey, namespace)
}
// Private context marshaling funcs.
var (
contextMarshalFuncs = []ContextMarshalFunc{}
contextMarshalFuncsMu = sync.RWMutex{}
contextUnmarshalFuncs = []ContextUnmarshalFunc{}
contextUnmarshalFuncsMu = sync.RWMutex{}
)
// ContextMarshalFunc is a function that marshalls any context values to a map,
// used for sending context on the wire.
type ContextMarshalFunc func(context.Context, map[string]interface{})
// RegisterContextMarshaler registers a marshaler function used by MarshalContext.
func RegisterContextMarshaler(f ContextMarshalFunc) {
contextMarshalFuncsMu.Lock()
defer contextMarshalFuncsMu.Unlock()
contextMarshalFuncs = append(contextMarshalFuncs, f)
}
// MarshalContext marshals a context into a map.
func MarshalContext(ctx context.Context) map[string]interface{} {
contextMarshalFuncsMu.RLock()
defer contextMarshalFuncsMu.RUnlock()
allVals := map[string]interface{}{}
for _, f := range contextMarshalFuncs {
vals := map[string]interface{}{}
f(ctx, vals)
for key, val := range vals {
if _, ok := allVals[key]; ok {
panic("duplicate context entry for: " + key)
}
allVals[key] = val
}
}
return allVals
}
// ContextUnmarshalFunc is a function that marshalls any context values to a map,
// used for sending context on the wire.
type ContextUnmarshalFunc func(context.Context, map[string]interface{}) context.Context
// RegisterContextUnmarshaler registers a marshaler function used by UnmarshalContext.
func RegisterContextUnmarshaler(f ContextUnmarshalFunc) {
contextUnmarshalFuncsMu.Lock()
defer contextUnmarshalFuncsMu.Unlock()
contextUnmarshalFuncs = append(contextUnmarshalFuncs, f)
}
// UnmarshalContext unmarshals a context from a map.
func UnmarshalContext(ctx context.Context, vals map[string]interface{}) context.Context {
contextUnmarshalFuncsMu.RLock()
defer contextUnmarshalFuncsMu.RUnlock()
if vals == nil {
return ctx
}
for _, f := range contextUnmarshalFuncs {
ctx = f(ctx, vals)
}
return ctx
}