S9 kept-call-hook: a kept call's hook at five minutes waited and at its answer

Builds: design 47 H2 H-A-708 (4)
This commit is contained in:
2026-11-25 17:06:52 +01:00
parent e6da0aeb5f
commit b4f9429115
2 changed files with 45 additions and 2 deletions
+23 -2
View File
@@ -120,13 +120,27 @@ func jittered(w time.Duration) time.Duration {
// Kept names one kept call of the paced form (design 47 H2 §775): the seat and the verb it asks (named in the journal
// line), the ask's id (named among the asks waiting), the caller's bound (zero is Bound) and the pace (zero is
// KeptPace). Open, when given, makes it asked again only while its key is open (DoKept). Logf is the journal; nil
// writes to standard error.
// writes to standard error. Hook, when given, is called when the call has waited WaitSaid, with its last fault and since
// when it waited, and once more when it is answered (design 47 H2 H-A-708 (4)).
type Kept struct {
Seat, Verb, Ask string
Bound time.Duration
Pace Pace
Open *Open
Logf func(format string, args ...any)
Hook func(Waited)
}
// WaitSaid is how long a kept call waits before its hook says so (design 47 H2 H-A-708 (4)).
const WaitSaid = 5 * time.Minute
// Waited is what a kept call's hook is told: the call's seat, verb and ask, since when it waited, its last fault's words,
// and whether it is answered (the call ended, by an answer of any kind that is no transport fault).
type Waited struct {
Seat, Verb, Ask string
Since time.Time
Last string
Answered bool
}
// Try is one call of a kept call that met a transport fault: when it was made and the words it met.
@@ -231,7 +245,7 @@ func DoPaced[T any](k Kept, try func() (T, error)) (T, error) {
}
start, wait := now(), pace.First
var tries []Try
waiting := false
waiting, said := false, false
defer func() {
if waiting {
journal.done(k.Seat, k.Ask)
@@ -241,9 +255,16 @@ func DoPaced[T any](k Kept, try func() (T, error)) (T, error) {
at := now()
v, err := try()
if !Fault(err) || (k.Open != nil && !k.Open.stillOpen()) {
if said {
k.Hook(Waited{Seat: k.Seat, Verb: k.Verb, Ask: k.Ask, Since: start, Last: tries[len(tries)-1].Words, Answered: true})
}
return v, err
}
tries = append(tries, Try{At: at, Words: err.Error()})
if !said && k.Hook != nil && now().Sub(start) >= WaitSaid {
said = true
k.Hook(Waited{Seat: k.Seat, Verb: k.Verb, Ask: k.Ask, Since: start, Last: err.Error()})
}
if !waiting {
journal.wait(k.Seat, k.Ask)
waiting = true
+22
View File
@@ -164,6 +164,28 @@ func subAKeptCallIsPaced(t *testing.T) {
}
}
})
t.Run("the hook is told at five minutes waited, once, and at the answer", func(t *testing.T) {
fakeClock(t)
tries, told := 0, []Waited{}
v, err := DoPaced(Kept{Seat: "mesh-builds", Verb: "build", Ask: "a9", Bound: time.Hour, Logf: func(string, ...any) {},
Hook: func(w Waited) { told = append(told, w) }}, func() (int, error) {
if tries++; tries <= 40 {
return 0, nats.ErrNoResponders
}
return 3, nil
})
if v != 3 || err != nil || len(told) != 2 || told[0].Answered || !told[1].Answered || told[0].Seat != "mesh-builds" ||
told[0].Verb != "build" || told[0].Last == "" || !told[0].Since.Equal(told[1].Since) {
t.Fatalf("%d, %v, told %+v", v, err, told)
}
// Answered within the five minutes: the hook is never told.
tries, told = 0, nil
_, _ = DoPaced(Kept{Seat: "s", Verb: "v", Logf: func(string, ...any) {}, Hook: func(w Waited) { told = append(told, w) }},
func() (int, error) { tries++; return 1, map[bool]error{true: nats.ErrNoResponders}[tries < 3] })
if len(told) != 0 {
t.Fatalf("told %+v for a call answered in time", told)
}
})
t.Run("a refusal returns at once", func(t *testing.T) {
slept := fakeClock(t)
tries := 0