-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconsistency.go
More file actions
79 lines (69 loc) · 2.03 KB
/
Copy pathconsistency.go
File metadata and controls
79 lines (69 loc) · 2.03 KB
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
package rules
import (
"net/http"
"strings"
"time"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
)
const (
WatchDelayKey = "SpiceDB-Watch-Delay"
ConsistencyHeaderKey = "SpiceDB-Consistency"
FullyConsistentHeaderValue = "Full"
AtExactSnapshotHeaderValuePrefix = "Exact "
AtLeastAsFreshHeaderValuePrefix = "At-Least "
)
var MinimizeLatency = &v1.Consistency{
Requirement: &v1.Consistency_MinimizeLatency{MinimizeLatency: true},
}
var FullyConsistent = &v1.Consistency{
Requirement: &v1.Consistency_FullyConsistent{FullyConsistent: true},
}
// ConsistencyFromHeaders returns a consistency block given a consistency
// header. Defaults to Minimize Latency, otherwise returns the first match in order:
// - Fully Consistent
// - At Exact Snapshot
// - At Least As Fresh
// - Minimize Latency
func ConsistencyFromHeaders(headers http.Header) *v1.Consistency {
consistencyValue := headers.Get(ConsistencyHeaderKey)
if len(consistencyValue) == 0 {
return MinimizeLatency
}
switch consistencyValue[0] {
case 'F':
fallthrough
case 'f':
return FullyConsistent
case 'E':
fallthrough
case 'e':
return &v1.Consistency{
Requirement: &v1.Consistency_AtExactSnapshot{
AtExactSnapshot: &v1.ZedToken{Token: strings.TrimPrefix(consistencyValue, AtExactSnapshotHeaderValuePrefix)},
},
}
case 'A':
fallthrough
case 'a':
return &v1.Consistency{
Requirement: &v1.Consistency_AtLeastAsFresh{
AtLeastAsFresh: &v1.ZedToken{Token: strings.TrimPrefix(consistencyValue, AtLeastAsFreshHeaderValuePrefix)},
},
}
}
return MinimizeLatency
}
// WatchDelayFromHeaders returns a time duration from the delay header.
// This is used to delay sending the check request to SpiceDB on a watch event,
// so that non-fully-consistent modes can be used with watch.
func WatchDelayFromHeaders(headers http.Header) time.Duration {
delay := headers.Get(WatchDelayKey)
if len(delay) == 0 {
return 0
}
duration, err := time.ParseDuration(delay)
if err != nil {
return 0
}
return duration
}