/
store.go
164 lines (137 loc) · 3.41 KB
/
store.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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vine
import (
"sync"
"time"
v23 "v.io/v23"
"v.io/v23/context"
"v.io/v23/discovery"
"v.io/v23/rpc"
"v.io/v23/security"
idiscovery "v.io/x/ref/lib/discovery"
"v.io/x/ref/lib/timekeeper"
)
type store struct {
mu sync.Mutex
adinfoMap map[string]map[discovery.AdId]*idiscovery.AdInfo // GUARDED_BY(mu)
expirations map[discovery.AdId]time.Time // GUARDED_BY(mu)
updated *sync.Cond
updateSeq int // GUARDED_BY(mu)
clock timekeeper.TimeKeeper
stop func()
}
func (s *store) Add(_ *context.T, _ rpc.ServerCall, adinfo idiscovery.AdInfo, ttl time.Duration) error {
s.mu.Lock()
defer s.mu.Unlock()
// Added adinfo to empty key ("") as well to simplify wildcard scan.
for _, key := range []string{adinfo.Ad.InterfaceName, ""} {
adinfos := s.adinfoMap[key]
if adinfos == nil {
adinfos = make(map[discovery.AdId]*idiscovery.AdInfo)
s.adinfoMap[key] = adinfos
}
adinfos[adinfo.Ad.Id] = &adinfo
}
s.expirations[adinfo.Ad.Id] = s.clock.Now().Add(ttl)
s.updateSeq++
s.updated.Broadcast()
return nil
}
func (s *store) Delete(_ *context.T, _ rpc.ServerCall, id discovery.AdId) error {
s.mu.Lock()
defer s.mu.Unlock()
s.deleteLocked(id)
return nil
}
func (s *store) lookup(interfaceName string) map[discovery.AdId]idiscovery.AdInfo {
s.mu.Lock()
defer s.mu.Unlock()
adinfos := make(map[discovery.AdId]idiscovery.AdInfo, len(s.adinfoMap[interfaceName]))
for id, adinfo := range s.adinfoMap[interfaceName] {
adinfos[id] = *adinfo
}
return adinfos
}
func (s *store) deleteLocked(id discovery.AdId) {
for key, adinfos := range s.adinfoMap {
delete(adinfos, id)
if len(adinfos) == 0 {
delete(s.adinfoMap, key)
}
}
delete(s.expirations, id)
s.updateSeq++
s.updated.Broadcast()
}
func (s *store) listenToUpdates(ctx *context.T) <-chan struct{} {
ch := make(chan struct{})
go func() {
defer close(ch)
var updateSeqSeen int
for {
s.mu.Lock()
for updateSeqSeen == s.updateSeq {
s.updated.Wait()
}
updateSeqSeen = s.updateSeq
s.mu.Unlock()
select {
case ch <- struct{}{}:
case <-ctx.Done():
return
}
}
}()
return ch
}
func (s *store) flushExpired(ctx *context.T) {
updated := s.listenToUpdates(ctx)
for {
s.mu.Lock()
now := s.clock.Now()
var nextExpiry time.Time
for id, expiry := range s.expirations {
if expiry.After(now) {
if nextExpiry.IsZero() || expiry.Before(nextExpiry) {
nextExpiry = expiry
}
continue
}
s.deleteLocked(id)
}
s.mu.Unlock()
var timer <-chan time.Time
if !nextExpiry.IsZero() {
timer = s.clock.After(nextExpiry.Sub(now))
}
select {
case <-timer:
case <-updated:
case <-ctx.Done():
return
}
}
}
func newStore(ctx *context.T, name string, clock timekeeper.TimeKeeper) (*store, error) {
s := &store{
adinfoMap: make(map[string]map[discovery.AdId]*idiscovery.AdInfo),
expirations: make(map[discovery.AdId]time.Time),
clock: clock,
}
s.updated = sync.NewCond(&s.mu)
ctx, cancel := context.WithCancel(ctx)
// TODO(suharshs): Make the authorizer configurable?
_, server, err := v23.WithNewServer(ctx, name, StoreServer(s), security.AllowEveryone())
if err != nil {
cancel()
return nil, err
}
go s.flushExpired(ctx)
s.stop = func() {
cancel()
<-server.Closed()
}
return s, nil
}