forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configuration.go
104 lines (90 loc) · 2.11 KB
/
configuration.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
package handlers
import (
"context"
"log"
"sync/atomic"
"pixur.org/pixur/api"
"pixur.org/pixur/fe/server"
)
type testConfigKey struct{}
func ctxFromTestConfig(ctx context.Context, conf *feConfiguration) context.Context {
return context.WithValue(ctx, testConfigKey{}, conf)
}
func testConfigFromCtx(ctx context.Context) (conf *feConfiguration, ok bool) {
conf, ok = ctx.Value(testConfigKey{}).(*feConfiguration)
return
}
var globalConfig = &configurationFuture{
done: make(chan struct{}),
}
type feConfiguration struct {
beconf *api.BackendConfiguration
anoncap map[api.Capability_Cap]bool
}
func (fc *feConfiguration) denorm() {
if fc.beconf.AnonymousCapability != nil {
fc.anoncap = make(map[api.Capability_Cap]bool)
for _, c := range fc.beconf.AnonymousCapability.Capability {
fc.anoncap[c] = true
}
}
}
type configurationFuture struct {
val atomic.Value
done chan struct{}
}
func (cf *configurationFuture) Get(ctx context.Context) (*feConfiguration, error) {
if conf, ok := testConfigFromCtx(ctx); ok {
return conf, nil
}
for {
if conf := cf.val.Load(); conf != nil {
return conf.(*feConfiguration), nil
}
select {
case <-cf.done:
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func init() {
register(func(s *server.Server) error {
wbcc, err := s.Client.WatchBackendConfiguration(
context.TODO(), &api.WatchBackendConfigurationRequest{})
if err != nil {
return err
}
go func() {
ctx := wbcc.Context()
first := true
for {
select {
case <-ctx.Done():
return
default:
}
resp, err := wbcc.Recv()
if err != nil {
log.Println("Got error listening for config", err)
return
}
feconf := &feConfiguration{
beconf: resp.BackendConfiguration,
}
if resp.BackendConfiguration.AnonymousCapability != nil {
feconf.anoncap = make(map[api.Capability_Cap]bool)
for _, c := range resp.BackendConfiguration.AnonymousCapability.Capability {
feconf.anoncap[c] = true
}
}
globalConfig.val.Store(feconf)
if first {
first = false
close(globalConfig.done)
}
}
}()
return nil
})
}