The runner answered each call it did not take once its stop began with the typed fault stopping (#90). Callers built before mesh-sdk read it as the callee's refusal and ended their kept call, and no edge delivered the new answer after its readers. Now, once told to stop, the runner first drains every subscription it answers calls on (a tool's, a seat verb's and a seat proof's) and subscribes none again, then finishes the calls in flight within drain-bound. A call made after the stop meets no responders at once, a transport fault every caller of every mesh-sdk already waits out, until the new runner subscribes. A call that still reached it is answered nothing, as H-A-369 has it. The typed answer goes whole: the reply's error-kind, KindedError, ErrorKindStopping and the bundle's error data. Tests: 721/AC4 in the bus (a call and a proof after the stop meet no responders within a second, the call in flight is answered, a subject served after the stop is refused) and in launch (a call that still reaches the runner gets no answer and no typed fault); the whole test meets no responders while a call is still in flight, before the drain bound. Builds: design 47 H2 H-A-570 (1), (3) (mesh-tools' part), amending §813 (12) as H-A-369 has it
136 lines
4.7 KiB
Go
136 lines
4.7 KiB
Go
package launch
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// DrainBoundSetting is the tool runner module's setting of how long, once told to stop, it waits for the calls in flight in its
|
|
// bundles to finish, a duration text; DefaultDrainBound is its default (design 47 H2 §813 (12), H-A-342 (2), H-A-377).
|
|
const (
|
|
DrainBoundSetting = "drain-bound"
|
|
DefaultDrainBound = 30 * time.Second
|
|
)
|
|
|
|
// inFlight is one ask the runner made of a bundle and is still waiting on: the bundle, the verb (a tool's name, else the
|
|
// method), when it was asked, and closed when the drain cuts it.
|
|
type inFlight struct {
|
|
module, verb string
|
|
at time.Time
|
|
cut chan struct{}
|
|
}
|
|
|
|
// asks is every ask in flight in every bundle the runner launched, and whether the runner is draining: from then on it makes
|
|
// no new ask (design 47 H2 §813 (12)).
|
|
var asks = struct {
|
|
mu sync.Mutex
|
|
draining bool
|
|
next uint64
|
|
of map[uint64]*inFlight
|
|
// ended is signalled at each ask's end while draining, so the drain hears the last end instead of looking again.
|
|
ended chan struct{}
|
|
}{of: map[uint64]*inFlight{}}
|
|
|
|
// afterStop is an ask made after the stop began that still reached the runner: a call the bus handed it before its
|
|
// subscriptions were down, or a message a consumer hands on. It is not taken and its caller is answered nothing, never
|
|
// refused, so the caller's kept call paces and meets the new runner (design 47 H2 §813 (12) as H-A-369 has it); a call made
|
|
// once the subscriptions are down meets no responders instead (H-A-570 (1)).
|
|
type afterStop struct{ module, verb string }
|
|
|
|
func (e *afterStop) Error() string {
|
|
return fmt.Sprintf("%s's %s was not taken: the tool runner is stopping, and a call after its stop is answered nothing", e.module, e.verb)
|
|
}
|
|
|
|
// Unanswered says the call is left without an answer, so its kept call asks again and meets the new runner.
|
|
func (e *afterStop) Unanswered() bool { return true }
|
|
|
|
// cutCall is a call the drain cut at its bound. Its caller is answered nothing, so it sees a transport fault (§813 (12)): the
|
|
// bus reads Unanswered and sends no reply.
|
|
type cutCall struct{ module, verb string }
|
|
|
|
func (e *cutCall) Error() string {
|
|
return fmt.Sprintf("%s's %s was cut: it outlived the tool runner's drain bound at its stop", e.module, e.verb)
|
|
}
|
|
|
|
// Unanswered says the call is left without an answer, so its caller sees a transport fault.
|
|
func (e *cutCall) Unanswered() bool { return true }
|
|
|
|
// begin records an ask about to be made; once the runner drains it takes none, and its caller is answered nothing.
|
|
func begin(module, verb string) (uint64, *inFlight, error) {
|
|
asks.mu.Lock()
|
|
defer asks.mu.Unlock()
|
|
if asks.draining {
|
|
return 0, nil, &afterStop{module: module, verb: verb}
|
|
}
|
|
asks.next++
|
|
f := &inFlight{module: module, verb: verb, at: time.Now(), cut: make(chan struct{})}
|
|
asks.of[asks.next] = f
|
|
return asks.next, f, nil
|
|
}
|
|
|
|
// end is an ask's end, answered or not.
|
|
func end(id uint64) {
|
|
asks.mu.Lock()
|
|
defer asks.mu.Unlock()
|
|
delete(asks.of, id)
|
|
if asks.draining {
|
|
select {
|
|
case asks.ended <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
// Drain is the runner's stop (design 47 H2 §813 (12), H-A-342 (2), H-A-353), once it took down every subscription it answers
|
|
// calls on (the bus's StopAnswering, H-A-570 (1)): from now on no new ask is made of any bundle, the
|
|
// asks in flight are waited for within bound, and each still in flight at the bound is cut, with a journal line naming the
|
|
// bundle, the verb and the call's age; its caller is answered nothing. It returns once no ask is in flight or the bound
|
|
// passed; the runner then stops its bundles itself.
|
|
func Drain(bound time.Duration, logf func(string, ...any)) {
|
|
asks.mu.Lock()
|
|
asks.draining = true
|
|
asks.ended = make(chan struct{}, 1)
|
|
ended := asks.ended
|
|
asks.mu.Unlock()
|
|
timer := time.NewTimer(bound)
|
|
defer timer.Stop()
|
|
for {
|
|
asks.mu.Lock()
|
|
left := len(asks.of)
|
|
asks.mu.Unlock()
|
|
if left == 0 {
|
|
return
|
|
}
|
|
select {
|
|
case <-ended:
|
|
case <-timer.C:
|
|
asks.mu.Lock()
|
|
ids := make([]uint64, 0, len(asks.of))
|
|
for id := range asks.of {
|
|
ids = append(ids, id)
|
|
}
|
|
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
|
|
now := time.Now()
|
|
for _, id := range ids {
|
|
f := asks.of[id]
|
|
logf("[mesh-tools] at its stop the tool runner cut %s's call %s, %s old: it outlived the drain bound of %s, and its "+
|
|
"caller is answered nothing (design 47 H2 §813 (12))", f.module, f.verb, now.Sub(f.at).Round(time.Millisecond), bound)
|
|
close(f.cut)
|
|
delete(asks.of, id)
|
|
}
|
|
asks.mu.Unlock()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// undrain is a test's: the runner serves again as before its stop.
|
|
func undrain() {
|
|
asks.mu.Lock()
|
|
asks.draining = false
|
|
asks.of = map[uint64]*inFlight{}
|
|
asks.mu.Unlock()
|
|
}
|