V-2: W1.13 (views adopt): a reader adopts a follower a seat's client made, so its callers write no glue
Six readers of the catalog half each wrote the same glue (dev-G-links' finding): the follower's conditions and view-waits, the seat event's subscription, Keys/Get/Watch over its values and a wrap of the first-view tools. Reader.Adopt and ProcessReader.Adopt take the client's follower with its seat's event and handler: the reader writes its findings (unanswered, view-waits; Follow's followers now write view-waits too), reads and lists it as any first view, keeps its own Changed after the reader's, and hears the seat's event (stdio: <seat>.<event>; process: the durable's mesh.seat.<seat>.event.<event>, also on a shared hub) into the handler. A subtest of TestViews. Builds: design 47 G §262, §260 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
+107
-7
@@ -3,7 +3,9 @@ package views
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -39,8 +41,20 @@ type Reader struct {
|
||||
order []string
|
||||
byOwner map[string]map[string]*Follower // owner -> state -> follower
|
||||
watchers map[string][]func(key string, value []byte, gone bool)
|
||||
// adopted are the followers a seat's client made (Adopt), by "<owner>.<state>": their events are a seat's, heard by
|
||||
// the client's own handler, not an owner's views family.
|
||||
adopted map[string]adoption
|
||||
}
|
||||
|
||||
// adoption is how an adopted follower's events are heard: the seat's event, by its local name, and the client's handler.
|
||||
type adoption struct {
|
||||
seat, event string
|
||||
hear func(context.Context, []byte) error
|
||||
}
|
||||
|
||||
// local is an adopted follower's event as the stdio runtime subscribes it, `<seat>.<event>`.
|
||||
func (a adoption) local() string { return a.seat + "." + a.event }
|
||||
|
||||
func (r *Reader) logf(format string, args ...any) {
|
||||
if r.Logf != nil {
|
||||
r.Logf(format, args...)
|
||||
@@ -68,9 +82,57 @@ func (r *Reader) Follow(owner, state, tool string) *Follower {
|
||||
}
|
||||
return DecodeAnswer(raw)
|
||||
}
|
||||
r.wireLocked(name, f, nil)
|
||||
return f
|
||||
}
|
||||
|
||||
// Adopt takes a follower a seat's client made (its Ask a seat's verb, such as the catalog half's view) as one of the
|
||||
// reader's first views, so the client's callers write no glue: the reader sets its conditions (the unanswered first view
|
||||
// and view-waits, in the reader's findings), its log and clock, and hands each change to Watch, Changed and the
|
||||
// follower's own Changed, set before; Keys, Get, Values, Watch, Statuses and the two tools name it `<owner>.<state>`.
|
||||
// Its events are the seat's event (`<seat>.<event>`, as the reader's `consumes` names it), which Start subscribes and
|
||||
// hands to hear, the client's handler. Call it before Start; a state followed already is refused.
|
||||
func (r *Reader) Adopt(f *Follower, seat, event string, hear func(context.Context, []byte) error) error {
|
||||
if f == nil || f.Owner == "" || f.State == "" || seat == "" || event == "" || hear == nil {
|
||||
return errors.New("views: an adopted follower names its owner and state, and its seat's event and handler")
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.followers == nil {
|
||||
r.followers, r.byOwner = map[string]*Follower{}, map[string]map[string]*Follower{}
|
||||
}
|
||||
name := f.Owner + "." + f.State
|
||||
if r.followers[name] != nil {
|
||||
return fmt.Errorf("views: %s is followed already", name)
|
||||
}
|
||||
if f.Logf == nil {
|
||||
f.Logf = r.Logf
|
||||
}
|
||||
if f.Now == nil {
|
||||
f.Now = r.Now
|
||||
}
|
||||
if f.Bound == 0 {
|
||||
f.Bound = r.Bound
|
||||
}
|
||||
if f.Wait == 0 {
|
||||
f.Wait = r.Wait
|
||||
}
|
||||
r.wireLocked(name, f, f.Changed)
|
||||
if r.adopted == nil {
|
||||
r.adopted = map[string]adoption{}
|
||||
}
|
||||
r.adopted[name] = adoption{seat: seat, event: event, hear: hear}
|
||||
return nil
|
||||
}
|
||||
|
||||
// wireLocked makes f one of the reader's first views under name: its conditions are the reader's, and each change goes
|
||||
// to the watchers, Changed and own (the follower's own Changed, or nil). The caller holds mu.
|
||||
func (r *Reader) wireLocked(name string, f *Follower, own func(string, json.RawMessage, bool, time.Time)) {
|
||||
f.Raise = func(s Status) { r.raise(s) }
|
||||
f.Clear = func(s Status) { r.clear(s) }
|
||||
f.Changed = func(key string, value json.RawMessage, deleted bool, _ time.Time) {
|
||||
f.RaiseWaits = func(s Status) { r.waits(s, true) }
|
||||
f.ClearWaits = func(s Status) { r.waits(s, false) }
|
||||
f.Changed = func(key string, value json.RawMessage, deleted bool, at time.Time) {
|
||||
r.mu.Lock()
|
||||
ws := append([]func(string, []byte, bool){}, r.watchers[name]...)
|
||||
r.mu.Unlock()
|
||||
@@ -80,14 +142,36 @@ func (r *Reader) Follow(owner, state, tool string) *Follower {
|
||||
if r.Changed != nil {
|
||||
r.Changed(name, key, value, deleted)
|
||||
}
|
||||
if own != nil {
|
||||
own(key, value, deleted, at)
|
||||
}
|
||||
}
|
||||
r.followers[name] = f
|
||||
r.order = append(r.order, name)
|
||||
if r.byOwner[owner] == nil {
|
||||
r.byOwner[owner] = map[string]*Follower{}
|
||||
if r.byOwner[f.Owner] == nil {
|
||||
r.byOwner[f.Owner] = map[string]*Follower{}
|
||||
}
|
||||
r.byOwner[f.Owner][f.State] = f
|
||||
}
|
||||
|
||||
// waits writes (raised) or clears the reader's view-waits finding of s (design 47 *A part with no view says it waits*).
|
||||
func (r *Reader) waits(s Status, raised bool) {
|
||||
if raised {
|
||||
r.logf("%s has waited for %s's view %s since %s", r.Module, s.Owner, s.WaitsFor, s.WaitingSince.Format(time.RFC3339))
|
||||
}
|
||||
if r.Findings == nil {
|
||||
return
|
||||
}
|
||||
f := ViewWaitsFinding(r.Module, s, r.nowTime())
|
||||
var err error
|
||||
if raised {
|
||||
err = r.Findings.Put(f)
|
||||
} else {
|
||||
err = r.Findings.Delete(f)
|
||||
}
|
||||
if err != nil {
|
||||
r.logf("the view-waits finding of %s is not written: %v", s.Tool, err)
|
||||
}
|
||||
r.byOwner[owner][state] = f
|
||||
return f
|
||||
}
|
||||
|
||||
func (r *Reader) nowTime() time.Time {
|
||||
@@ -122,16 +206,21 @@ func (r *Reader) clear(s Status) {
|
||||
func (r *Reader) Start(ctx context.Context) {
|
||||
r.mu.Lock()
|
||||
names := append([]string(nil), r.order...)
|
||||
adopted := maps.Clone(r.adopted)
|
||||
r.mu.Unlock()
|
||||
for _, name := range names {
|
||||
f := r.followers[name]
|
||||
pattern, handle := f.Owner+"."+EventName(f.State), r.hear(ctx, f)
|
||||
if a, ok := adopted[name]; ok {
|
||||
pattern, handle = a.local(), r.hearAdopted(ctx, a)
|
||||
}
|
||||
go func() {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := stdio.Subscribe(f.Owner+"."+EventName(f.State), r.hear(ctx, f))
|
||||
err := stdio.Subscribe(pattern, handle)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
r.logf("not hearing %s's %s events yet (%v); asking again in %s", f.Owner, f.State, err, wait)
|
||||
r.logf("not hearing %s yet for %s's %s (%v); asking again in %s", pattern, f.Owner, f.State, err, wait)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
@@ -156,6 +245,17 @@ func (r *Reader) hear(ctx context.Context, f *Follower) func(stdio.Envelope) err
|
||||
}
|
||||
}
|
||||
|
||||
// hearAdopted is the handler of an adopted follower's seat event: the client's handler takes it, and what it cannot read is
|
||||
// said in the log.
|
||||
func (r *Reader) hearAdopted(ctx context.Context, a adoption) func(stdio.Envelope) error {
|
||||
return func(e stdio.Envelope) error {
|
||||
if err := a.hear(ctx, e.Body); err != nil {
|
||||
r.logf("views: %s is not taken: %v", e.Key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Ready is closed once every followed state had its first view.
|
||||
func (r *Reader) Ready() <-chan struct{} {
|
||||
r.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user