@@ -20,6 +20,7 @@ type Registry struct {
20
20
21
21
hostsFlowsMux sync.Mutex
22
22
hostFlows map [string ]* Flow
23
+ notFoundFlows map [string ]struct {}
23
24
closeCh chan struct {}
24
25
closedCh chan struct {}
25
26
@@ -43,7 +44,20 @@ func NewRegistry(httpHost string, d flowstate.Driver, l *slog.Logger) *Registry
43
44
}
44
45
45
46
func (fr * Registry ) Flow (id flowstate.TransitionID ) (flowstate.Flow , error ) {
46
- return fr .fr .Flow (id )
47
+ f , err := fr .fr .Flow (id )
48
+
49
+ // slow path, flow not found locally, might not synced yet
50
+ if errors .Is (err , flowstate .ErrFlowNotFound ) {
51
+ stateCtx := & flowstate.StateCtx {}
52
+ if err := fr .d .GetStateByID (flowstate .GetStateByID (stateCtx , flowStateID (id ), 0 )); err != nil {
53
+ return nil , err
54
+ }
55
+
56
+ fr .setFlow (stateCtx .Current )
57
+ return fr .fr .Flow (id )
58
+ }
59
+
60
+ return f , nil
47
61
}
48
62
49
63
func (fr * Registry ) SetFlow (id flowstate.TransitionID , flow flowstate.Flow ) error {
@@ -162,52 +176,54 @@ func (fr *Registry) sync() bool {
162
176
163
177
for _ , state := range res .States {
164
178
fr .sinceRev = state .Rev
179
+ fr .setFlow (state )
180
+ }
165
181
166
- if state .Annotations [`flowstate.flow.http_host` ] == `` {
167
- fr .l .Warn ("state has no 'flowstate.flow.http_host' annotation set, skipping" , "state_id" , state .ID , "state_rev" , state .Rev )
168
- continue
169
- }
170
- httpHost := state .Annotations [`flowstate.flow.http_host` ]
182
+ return res .More
183
+ }
171
184
172
- // local flow, skip
173
- if httpHost == fr .httpHost {
174
- continue
175
- }
185
+ func (fr * Registry ) setFlow (state flowstate.State ) {
186
+ if state .Annotations [`flowstate.flow.http_host` ] == `` {
187
+ fr .l .Warn ("state has no 'flowstate.flow.http_host' annotation set, skipping" , "state_id" , state .ID , "state_rev" , state .Rev )
188
+ }
189
+ httpHost := state .Annotations [`flowstate.flow.http_host` ]
176
190
177
- if state .Annotations [`flowstate.flow.transition_id` ] == `` {
178
- fr .l .Warn ("flow state has no 'flowstate.flow.transition_id' annotation set, skipping" , "state_id" , state .ID , "state_rev" , state .Rev )
179
- continue
180
- }
181
- tsID := flowstate .TransitionID (state .Annotations [`flowstate.flow.transition_id` ])
191
+ // local flow, skip
192
+ if httpHost == fr .httpHost {
193
+ return
194
+ }
182
195
183
- if flowstate .Ended (state ) {
184
- if err := fr .fr .UnsetFlow (tsID ); err != nil {
185
- fr .l .Warn ("flow registry: unset flow failed" , "error" , err , "transition_id" , tsID , "state_id" , state .ID , "state_rev" , state .Rev )
186
- }
196
+ if state .Annotations [`flowstate.flow.transition_id` ] == `` {
197
+ fr .l .Warn ("flow state has no 'flowstate.flow.transition_id' annotation set, skipping" , "state_id" , state .ID , "state_rev" , state .Rev )
198
+ return
199
+ }
200
+ tsID := flowstate .TransitionID (state .Annotations [`flowstate.flow.transition_id` ])
187
201
188
- continue
202
+ if flowstate .Ended (state ) {
203
+ if err := fr .fr .UnsetFlow (tsID ); err != nil {
204
+ fr .l .Warn ("flow registry: unset flow failed" , "error" , err , "transition_id" , tsID , "state_id" , state .ID , "state_rev" , state .Rev )
189
205
}
190
206
191
- fr .hostsFlowsMux .Lock ()
192
-
193
- if fr .hostFlows == nil {
194
- fr .hostFlows = make (map [string ]* Flow )
195
- }
207
+ return
208
+ }
196
209
197
- f , ok := fr .hostFlows [httpHost ]
198
- if ! ok {
199
- f = New (httpHost )
200
- fr .hostFlows [httpHost ] = f
201
- }
210
+ fr .hostsFlowsMux .Lock ()
202
211
203
- fr .hostsFlowsMux .Unlock ()
212
+ if fr .hostFlows == nil {
213
+ fr .hostFlows = make (map [string ]* Flow )
214
+ }
204
215
205
- if err := fr .fr .SetFlow (tsID , f ); err != nil {
206
- fr .l .Warn ("flow registry: set flow failed" , "error" , err , "transition_id" , tsID , "state_id" , state .ID , "state_rev" , state .Rev )
207
- }
216
+ f , ok := fr .hostFlows [httpHost ]
217
+ if ! ok {
218
+ f = New (httpHost )
219
+ fr .hostFlows [httpHost ] = f
208
220
}
209
221
210
- return res .More
222
+ fr .hostsFlowsMux .Unlock ()
223
+
224
+ if err := fr .fr .SetFlow (tsID , f ); err != nil {
225
+ fr .l .Warn ("flow registry: set flow failed" , "error" , err , "transition_id" , tsID , "state_id" , state .ID , "state_rev" , state .Rev )
226
+ }
211
227
}
212
228
213
229
func flowStateID (tsID flowstate.TransitionID ) flowstate.StateID {
0 commit comments