-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
121 lines (107 loc) · 2.87 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
package feature
import (
"context"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/segmentio/fs"
)
// Store is similar to Cache, but automatically reloads when updates are made
// to the underlying file system.
type Store struct {
cache Cache
once sync.Once
join sync.WaitGroup
done chan struct{}
notify chan string
}
// Close closes the store, releasing all associated resources.
func (s *Store) Close() error {
s.once.Do(func() { close(s.done) })
s.join.Wait()
s.cache.Close()
return nil
}
// GateOpen returns true if a gate is opened for a given id.
func (s *Store) GateOpen(family, gate, collection, id string) bool {
return s.cache.GateOpen(family, gate, collection, id)
}
// LookupGates returns the list of open gates in a family for a given id.
func (s *Store) LookupGates(family, collection, id string) []string {
return s.cache.LookupGates(family, collection, id)
}
// The Open method opens the features at the mount point it was called on,
// returning a Store object exposing the state.
//
// The returned store holds operating system resources and therefore must be
// closed when the program does not need it anymore.
func (path MountPoint) Open() (*Store, error) {
notify := make(chan string)
if err := fs.Notify(notify, string(path)); err != nil {
return nil, err
}
c, err := path.Load()
if err != nil {
fs.Stop(notify)
return nil, err
}
s := &Store{
cache: Cache{tiers: c.tiers},
done: make(chan struct{}),
notify: notify,
}
s.join.Add(1)
go path.watch(s)
return s, nil
}
func (path MountPoint) watch(s *Store) {
defer s.join.Done()
defer fs.Stop(s.notify)
log.Printf("NOTICE feature - %s - watching for changes on the feature database", path)
for {
select {
case <-s.notify:
log.Printf("INFO feature - %s - reloading feature database after detecting update", path)
if err := fs.Notify(s.notify, string(path)); err != nil {
log.Printf("CRIT feature - %s - %s", path, err)
}
start := time.Now()
c, err := path.Load()
if err != nil {
log.Printf("ERROR feature - %s - %s", path, err)
} else {
log.Printf("NOTICE feature - %s - feature database reloaded in %gs", path, time.Since(start).Round(time.Millisecond).Seconds())
c = s.cache.swap(c)
c.Close()
}
case <-s.done:
return
}
}
}
// Wait blocks until the path exists or ctx is cancelled.
func (path MountPoint) Wait(ctx context.Context) error {
notify := make(chan string)
defer fs.Stop(notify)
for {
if err := fs.Notify(notify, filepath.Dir(string(path))); err != nil {
return err
}
_, err := os.Lstat(string(path))
if err == nil {
log.Printf("INFO feature - %s - feature database exists", path)
return nil
}
if !os.IsNotExist(err) {
return err
}
log.Printf("NOTICE feature - %s - waiting for feature database to be created", path)
select {
case <-notify:
case <-ctx.Done():
return ctx.Err()
}
}
}