Follow the runtime's watches through mesh-sdk's KeptState.Follow, not four loops of their own (issue 774)
mesh-provisioner, mesh-config, mesh-issues and mesh-healers each wrote a watch-again loop (`watched`). mesh-sdk 61b67d61 adds KeptState.Follow, which asks again with backoff and says each give-up, and its watches now answer a change at once and run the handler beside the runtime (the provisioner's "names no watch of this bundle"). The four modules use it; their loops are gone. Built and tested against the mesh-sdk group head through a go.work that is not committed: their mesh-sdk pins move to that head with the group.
This commit is contained in:
@@ -103,34 +103,22 @@ func (w *work) run(h *Holder) {
|
||||
}
|
||||
}
|
||||
|
||||
// watched asks a watch of the runtime again until it is taken: the runtime answers only once Serve runs.
|
||||
func watched(state string, handle func(stdio.StateChange) error) {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
time.Sleep(wait)
|
||||
if err := stdio.State(state).Watch("", handle); err != nil {
|
||||
logf("[%s] %s is not watched yet (%v); asking again in %s", Module, state, err, wait)
|
||||
continue
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := runtimeStore{}
|
||||
h := &Holder{Store: s, Compose: FromBus(), Asks: runtimeAsks{}, Now: time.Now, Logf: logf}
|
||||
w := &work{pending: map[string]bool{}, wake: make(chan struct{}, 1)}
|
||||
go w.run(h)
|
||||
go starting(h)
|
||||
go watched(PlacementRead, func(c stdio.StateChange) error { w.add(NodesOfPlacement(c.Key)...); return nil })
|
||||
go watched(WantsRead, func(c stdio.StateChange) error { w.add(NodesOfWant(c.Key)...); return nil })
|
||||
go watched(ModulesRead, func(c stdio.StateChange) error {
|
||||
go stdio.State(PlacementRead).Follow("", func(c stdio.StateChange) error { w.add(NodesOfPlacement(c.Key)...); return nil })
|
||||
go stdio.State(WantsRead).Follow("", func(c stdio.StateChange) error { w.add(NodesOfWant(c.Key)...); return nil })
|
||||
go stdio.State(ModulesRead).Follow("", func(c stdio.StateChange) error {
|
||||
if module, err := delivery.ParseModulesKey(c.Key); err == nil {
|
||||
w.add(h.NodesOfModule(module)...)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
for _, provider := range BoundReads {
|
||||
go watched(boundRead(provider), func(c stdio.StateChange) error {
|
||||
go stdio.State(boundRead(provider)).Follow("", func(c stdio.StateChange) error {
|
||||
if said, err := h.Rejudge(c.Key); err != nil {
|
||||
logf("[%s] %s is not judged again: %v", Module, c.Key, err)
|
||||
} else if said != "" {
|
||||
@@ -139,7 +127,7 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
go watched(Registry, func(c stdio.StateChange) error {
|
||||
go stdio.State(Registry).Follow("", func(c stdio.StateChange) error {
|
||||
if c.Op != "put" {
|
||||
if _, _, err := Publish(s); err != nil {
|
||||
logf("[%s] the registry is not written back: %v", Module, err)
|
||||
|
||||
Reference in New Issue
Block a user