-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
177 lines (157 loc) · 3.64 KB
/
watch.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
165
166
167
168
169
170
171
172
173
174
175
176
177
package gitnogdwatchlist
import (
"context"
"fmt"
"strings"
"sync"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/nogproject/nog/backend/internal/nogfsopb"
"github.com/nogproject/nog/backend/pkg/uuid"
)
// `registryView` watches multiple registries to maintain a set of known repos.
type registryView struct {
lg Logger
registries []string
prefixes []string
origin pb.RegistryClient
mu sync.Mutex
repos map[uuid.I]bool
}
func newRegistryView(
lg Logger,
conn *grpc.ClientConn,
cfg *Config,
) *registryView {
var prefixes []string
for _, p := range cfg.Prefixes {
// Ensure trailing slash.
p = strings.TrimRight(p, "/") + "/"
prefixes = append(prefixes, p)
}
return ®istryView{
lg: lg,
registries: cfg.Registries,
prefixes: prefixes,
origin: pb.NewRegistryClient(conn),
repos: make(map[uuid.I]bool),
}
}
func (v *registryView) enableRepo(id uuid.I) {
v.mu.Lock()
v.repos[id] = true
v.mu.Unlock()
}
func (v *registryView) isKnownRepo(id uuid.I) bool {
v.mu.Lock()
_, ok := v.repos[id]
v.mu.Unlock()
return ok
}
func (v *registryView) watch(ctx context.Context) error {
var wg sync.WaitGroup
wg.Add(len(v.registries))
watchForever := func(r string) {
defer wg.Done()
var tail []byte
for {
var err error
tail, err = v.watchRegistry(ctx, r, tail)
if s, ok := status.FromError(err); ok {
if s.Code() == codes.Canceled {
return
}
}
wait := 20 * time.Second
afterEvent := "Epoch"
if tail != nil {
afterEvent = fmt.Sprintf("%x", tail)
}
v.lg.Errorw(
"Will retry watch registry.",
"module", "nogfsog2nd",
"err", err,
"registry", r,
"afterEvent", afterEvent,
"retryIn", wait,
)
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
}
}
for _, r := range v.registries {
go watchForever(r)
}
wg.Wait()
return ctx.Err()
}
func (v *registryView) watchRegistry(
ctx context.Context,
registry string, tail []byte,
) ([]byte, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
stream, err := v.origin.Events(ctx, &pb.RegistryEventsI{
Registry: registry,
Watch: true,
After: tail,
})
if err != nil {
return tail, err
}
for {
rsp, err := stream.Recv()
if err != nil {
return tail, err
}
for _, ev := range rsp.Events {
if err := v.processEvent(ev); err != nil {
return tail, err
}
tail = ev.Id
}
}
}
func (v *registryView) processEvent(ev *pb.RegistryEvent) error {
switch ev.Event {
case pb.RegistryEvent_EV_FSO_REPO_ADDED:
inf := ev.FsoRepoInfo
if !isBelowPrefix(v.prefixes, inf.GlobalPath) {
v.lg.Infow(
"Ignored repo with foreign prefix.",
"repo", inf.GlobalPath,
)
return nil
}
repoId, err := uuid.FromBytes(inf.Id)
if err != nil {
return err
}
v.enableRepo(repoId)
v.lg.Infow("Enabled repo.", "repoId", repoId.String())
// Ignore unrelated.
case pb.RegistryEvent_EV_FSO_REGISTRY_ADDED:
case pb.RegistryEvent_EV_FSO_ROOT_ADDED:
case pb.RegistryEvent_EV_FSO_ROOT_REMOVED:
case pb.RegistryEvent_EV_FSO_ROOT_UPDATED:
case pb.RegistryEvent_EV_FSO_REPO_NAMING_UPDATED:
case pb.RegistryEvent_EV_FSO_REPO_NAMING_CONFIG_UPDATED:
case pb.RegistryEvent_EV_FSO_REPO_ACCEPTED:
case pb.RegistryEvent_EV_FSO_REPO_REINIT_ACCEPTED:
case pb.RegistryEvent_EV_FSO_REPO_ENABLE_GITLAB_ACCEPTED:
case pb.RegistryEvent_EV_FSO_ROOT_ARCHIVE_RECIPIENTS_UPDATED:
case pb.RegistryEvent_EV_FSO_ROOT_SHADOW_BACKUP_RECIPIENTS_UPDATED:
default:
v.lg.Warnw(
"Ignored unknown registry event.",
"module", "nogfsog2nd",
"event", ev.Event.String(),
)
}
return nil
}