T2 drain at stop: finish the calls in flight within the drain bound, cut and say past it
Once told to stop, the tool runner makes no new ask of a bundle (a call after the stop is refused), waits for the asks in flight within the drain bound (30 s by default), cuts each still in flight at the bound with a journal line naming the bundle, the verb and the call's age, leaves its caller unanswered (a transport fault), and only then stops its bundles itself. Builds: design 47 H2 §813 (12), H-A-342 (2), H-A-353
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package launch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultDrainBound is how long the tool runner, once told to stop, waits for the calls in flight in its bundles to finish:
|
||||
// the setting drain-bound's default (design 47 H2 §813 (12), H-A-342 (2)).
|
||||
const 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{}}
|
||||
|
||||
// errStopping is an ask made after the stop: refused, the call made after the stop is not taken (§813 (12)).
|
||||
var errStopping = &Refused{Method: "", Message: "the tool runner is stopping: it takes no new call (design 47 H2 §813 (12))"}
|
||||
|
||||
// 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, or refuses it once the runner drains.
|
||||
func begin(module, verb string) (uint64, *inFlight, error) {
|
||||
asks.mu.Lock()
|
||||
defer asks.mu.Unlock()
|
||||
if asks.draining {
|
||||
return 0, nil, errStopping
|
||||
}
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user