-
Notifications
You must be signed in to change notification settings - Fork 1
/
permission.pb.query_watcher.go
218 lines (199 loc) · 6.22 KB
/
permission.pb.query_watcher.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Code generated by protoc-gen-goten-access
// Resource: Permission
// DO NOT EDIT!!!
package permission_access
import (
"context"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/cloudwan/goten-sdk/runtime/api/view"
"github.com/cloudwan/goten-sdk/runtime/api/watch_type"
permission_client "github.com/cloudwan/edgelq-sdk/iam/client/v1alpha2/permission"
permission "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/permission"
)
// QueryWatcher is a low-level, stateless watcher.
// Initial updates are sent in chunks. Once snapshot is complete, further
// changes are incremental - unless Reset flag is set, in which case another
// snapshot is received.
type QueryWatcher struct {
client permission_client.PermissionServiceClient
params *QueryWatcherParams
syncDeadline time.Time
identifier int
evtsChan chan *QueryWatcherEvent
resumeToken string
}
type QueryWatcherParams struct {
Filter *permission.Filter
View view.View
FieldMask *permission.Permission_FieldMask
OrderBy *permission.OrderBy
Cursor *permission.PagerCursor
ChunkSize int
PageSize int
WatchType watch_type.WatchType
RecoveryDeadline time.Duration
RetryTimeout time.Duration
}
type QueryWatcherEvent struct {
Identifier int
Changes []*permission.PermissionChange
Reset bool
LostSync bool
InSync bool
SnapshotSize int64
CheckSize bool
}
func NewQueryWatcher(id int, client permission_client.PermissionServiceClient,
params *QueryWatcherParams, evtsChan chan *QueryWatcherEvent) *QueryWatcher {
return &QueryWatcher{
client: client,
params: params,
identifier: id,
evtsChan: evtsChan,
}
}
func (qw *QueryWatcher) Run(ctx context.Context) error {
log := ctxlogrus.Extract(ctx).
WithField("query-watcher", "permission-query-watcher").
WithField("query-filter", qw.params.Filter.String()).
WithField("query-order-by", qw.params.OrderBy.String()).
WithField("query-cursor", qw.params.Cursor.String())
ctx = ctxlogrus.ToContext(ctx, log)
log.Infof("Running new query")
inSync := false
for {
stream, err := qw.client.WatchPermissions(ctx, &permission_client.WatchPermissionsRequest{
Type: qw.params.WatchType,
Filter: qw.params.Filter,
View: qw.params.View,
FieldMask: qw.params.FieldMask,
MaxChunkSize: int32(qw.params.ChunkSize),
OrderBy: qw.params.OrderBy,
ResumeToken: qw.resumeToken,
PageSize: int32(qw.params.PageSize),
PageToken: qw.params.Cursor,
})
if err != nil {
log.WithError(err).Errorf("watch initialization error")
} else {
pending := make([]*permission.PermissionChange, 0)
for {
resp, err := stream.Recv()
if err != nil {
log.WithError(err).Errorf("watch error")
break
} else {
var outputEvt *QueryWatcherEvent
// until we reach initial sync, we will send all the data as we get to minimize
// potential impact on memory (if receiver does not need state). Later on, we will
// collect changes and send once IsCurrent flag is sent. This is to handle soft reset
// flag. Changes after initial sync are however practically always small.
if inSync {
pending = append(pending, resp.GetPermissionChanges()...)
if resp.IsSoftReset {
log.Debugf("received soft reset after %d changes", len(pending))
pending = nil
} else if resp.IsHardReset {
log.Warnf("received hard reset after %d changes", len(pending))
qw.resumeToken = ""
inSync = false
pending = nil
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Reset: true,
}
} else if resp.GetSnapshotSize() >= 0 {
log.Debugf("received snapshot size info: %d", resp.GetSnapshotSize())
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
SnapshotSize: resp.GetSnapshotSize(),
CheckSize: true,
Changes: pending,
InSync: true,
}
} else if resp.GetIsCurrent() {
qw.syncDeadline = time.Time{}
if resp.GetResumeToken() != "" {
qw.resumeToken = resp.GetResumeToken()
}
if len(pending) > 0 {
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Changes: pending,
InSync: true,
}
}
pending = nil
}
} else {
if resp.IsCurrent {
log.Infof("query synchronized")
inSync = true
qw.syncDeadline = time.Time{}
if resp.GetResumeToken() != "" {
qw.resumeToken = resp.GetResumeToken()
}
}
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Changes: resp.GetPermissionChanges(),
SnapshotSize: resp.SnapshotSize,
Reset: resp.IsHardReset || resp.IsSoftReset,
InSync: inSync,
CheckSize: resp.SnapshotSize >= 0,
}
}
if outputEvt != nil {
select {
case <-ctx.Done():
return ctx.Err()
case qw.evtsChan <- outputEvt:
}
}
}
}
}
if ctx.Err() != nil {
return ctx.Err()
}
// if we disconnected during initial snapshot (we were not in sync), send a message to cancel all data
if !inSync {
evt := &QueryWatcherEvent{
Identifier: qw.identifier,
Reset: true,
}
select {
case <-ctx.Done():
return ctx.Err()
case qw.evtsChan <- evt:
}
}
if qw.syncDeadline.IsZero() && qw.params.RecoveryDeadline > 0 {
qw.syncDeadline = time.Now().UTC().Add(qw.params.RecoveryDeadline)
log.Infof("lost sync, scheduling recovery with timeout %s", qw.syncDeadline)
} else if !qw.syncDeadline.IsZero() && time.Now().UTC().After(qw.syncDeadline) {
log.Errorf("could not recover within %s, reporting lost sync", qw.syncDeadline)
evt := &QueryWatcherEvent{
Identifier: qw.identifier,
LostSync: true,
Reset: true,
}
qw.resumeToken = ""
inSync = false
select {
case <-ctx.Done():
return ctx.Err()
case qw.evtsChan <- evt:
}
}
backoff := time.After(qw.params.RetryTimeout)
select {
case <-backoff:
log.Debugf("after backoff %s", qw.params.RetryTimeout)
case <-ctx.Done():
log.Debugf("context done, reason: %s", ctx.Err())
return ctx.Err()
}
}
}