-
Notifications
You must be signed in to change notification settings - Fork 196
/
context.go
79 lines (63 loc) · 2.3 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
// 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 version
import (
"context"
"time"
eh "github.com/looplab/eventhorizon"
)
// DefaultMinVersionDeadline is the deadline to use when creating a min version
// context that waits.
var DefaultMinVersionDeadline = 10 * time.Second
func init() {
// Register the version context.
eh.RegisterContextMarshaler(func(ctx context.Context, vals map[string]interface{}) {
if v, ok := ctx.Value(minVersionKey).(int); ok {
vals[minVersionKeyStr] = v
}
})
eh.RegisterContextUnmarshaler(func(ctx context.Context, vals map[string]interface{}) context.Context {
if v, ok := vals[minVersionKeyStr].(int); ok {
return NewContextWithMinVersion(ctx, v)
}
// Support JSON-like marshaling of ints as floats.
if v, ok := vals[minVersionKeyStr].(float64); ok {
return NewContextWithMinVersion(ctx, int(v))
}
return ctx
})
}
type contextKey int
const (
minVersionKey contextKey = iota
)
// Strings used to marshal context values.
const (
minVersionKeyStr = "eh_minversion"
)
// MinVersionFromContext returns the min version from the context.
func MinVersionFromContext(ctx context.Context) (int, bool) {
minVersion, ok := ctx.Value(minVersionKey).(int)
return minVersion, ok
}
// NewContextWithMinVersion returns the context with min version set.
func NewContextWithMinVersion(ctx context.Context, minVersion int) context.Context {
return context.WithValue(ctx, minVersionKey, minVersion)
}
// NewContextWithMinVersionWait returns the context with min version and a
// default deadline set.
func NewContextWithMinVersionWait(ctx context.Context, minVersion int) (c context.Context, cancel func()) {
ctx = context.WithValue(ctx, minVersionKey, minVersion)
return context.WithTimeout(ctx, DefaultMinVersionDeadline)
}