A call the runner does not take once its stop began was answered nothing, so its caller's wait ended in a bare "timeout" and a build step's publish failed (main.440). It is now answered with error-kind stopping beside its words, on a tool's reply and a seat proof's alike; the calling runner keeps that kind and hands it to its bundle as the JSON-RPC error's data, so mesh-sdk's stdio tells it by its kind. A call cut at the drain bound stays unanswered (H-A-369). Builds: design 47 H2 H-A-542 (amends H-A-369)
138 lines
4.6 KiB
Go
138 lines
4.6 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{}}
|
|
|
|
// ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542,
|
|
// amending H-A-369): the answer's `error-kind`, so its caller asks again and meets the new runner; never silence.
|
|
const ErrorKindStopping = "stopping"
|
|
|
|
// afterStop is an ask made after the stop began: it is not taken, and its caller is answered the typed fault stopping
|
|
// (ErrorKind), never silence, so the caller asks again after a pause and meets the new runner (design 47 H2 §813 (12) as
|
|
// H-A-369 and H-A-542 amend it).
|
|
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; ask again, the new runner answers it", e.module, e.verb)
|
|
}
|
|
|
|
// ErrorKind is the typed fault the call is answered with: stopping.
|
|
func (e *afterStop) ErrorKind() string { return ErrorKindStopping }
|
|
|
|
// 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 stopping.
|
|
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): 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()
|
|
}
|