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: 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 amends 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, 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): 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() }