Files
mesh-catalog/modules/messenger/cmd/messenger/holdback.go
T
jschoubben 59704456c3 messenger: offer every ask when it is raised; hold none back for room
The operator channel kept at most three asks of one asker offered and held its oldest back behind newer ones, so asks
waited unseen (the pipeline's proposal behind two mailu asks). It keeps no count any more and holds nothing back: every
ask is offered when it is raised, and lapses at its expiry or ends at its answer. An ask held back before the change is
offered again at the router's next tick; ask-again offers one so and holds nothing back in its place.

Builds: design 47 G A-424
2026-11-15 15:52:48 +01:00

178 lines
5.4 KiB
Go

package main
import (
"fmt"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
"sort"
"time"
)
// An ask held back (novox/hq ADR 0666 §12, design 46 § 6) was one kept open behind newer asks of its asker, offered again when
// one of them ended. The channel holds none back any more (design 47 G A-424): every ask is offered when it is raised, and one
// held back before the change is offered at the router's start (offerAllHeldBack) and by ask-again. `held` is another state, an
// ask no ready channel can carry (ADR 0397).
// StateHeldBack is an ask kept open behind newer asks of its asker.
const StateHeldBack = "held-back"
// liveState is whether an ask has not ended: offered, held for want of a channel, or held back.
func liveState(state string) bool {
return state == StateOpen || state == StateHeld || state == StateHeldBack
}
// behindLocked are the keys of the newer offered asks of the asker of rec, oldest first; h.mu is held.
func (h *Holder) behindLocked(key string, rec AskRecord) []string {
var behind []string
for k, e := range h.asks {
if k != key && e.rec.Asker == rec.Asker && (e.rec.State == StateOpen || e.rec.State == StateHeld) && e.rec.Opened.After(rec.Opened) {
behind = append(behind, k)
}
}
sort.Slice(behind, func(i, j int) bool {
a, b := h.asks[behind[i]].rec, h.asks[behind[j]].rec
if !a.Opened.Equal(b.Opened) {
return a.Opened.Before(b.Opened)
}
return behind[i] < behind[j]
})
return behind
}
// pickLocked is the key of the asker's asks in one of the states, the oldest first (by opening, then key); "" when
// there is none, never except; h.mu is held.
func (h *Holder) pickLocked(asker, except string, states ...string) string {
pick := ""
for k, e := range h.asks {
if e.rec.Asker != asker || k == except {
continue
}
in := false
for _, s := range states {
in = in || e.rec.State == s
}
if !in {
continue
}
if pick == "" || e.rec.Opened.Before(h.asks[pick].rec.Opened) || (e.rec.Opened.Equal(h.asks[pick].rec.Opened) && k < pick) {
pick = k
}
}
return pick
}
// offeredCount is how many of the asker's asks are offered now: open, or held for want of a channel.
func (h *Holder) offeredCount(asker string) int {
h.mu.Lock()
defer h.mu.Unlock()
n := 0
for _, e := range h.asks {
if e.rec.Asker == asker && (e.rec.State == StateOpen || e.rec.State == StateHeld) {
n++
}
}
return n
}
// freshAsk is an ask's record read again from the store, where there is one.
func (h *Holder) freshAsk(key string) (AskRecord, uint64, bool) {
e := h.askOf(key)
if e == nil {
return AskRecord{}, 0, false
}
rec, rev := e.rec, e.rev
if h.Asks != nil {
if got, r, err := h.Asks.GetAsk(key); err == nil && got != nil {
rec, rev = *got, r
}
}
return rec, rev, true
}
// offerHeldBack offers the asker's held-back asks of before the change again (A-424): there is no room to wait for. The work
// lock is held.
func (h *Holder) offerHeldBack(asker string) {
for {
h.mu.Lock()
key := h.pickLocked(asker, "", StateHeldBack)
h.mu.Unlock()
if key == "" {
return
}
h.offerOneHeldBack(key)
if after := h.askOf(key); after == nil || after.rec.State == StateHeldBack {
return // not offered (its record changed meanwhile): left for the next start rather than tried forever
}
}
}
// offerAllHeldBack offers every ask held back before the change, at each tick of the router (A-424). The work lock is held.
func (h *Holder) offerAllHeldBack() {
h.mu.Lock()
askers := map[string]bool{}
for _, e := range h.asks {
if e.rec.State == StateHeldBack {
askers[e.rec.Asker] = true
}
}
h.mu.Unlock()
for asker := range askers {
h.offerHeldBack(asker)
}
}
// offerOneHeldBack offers one held-back ask again now.
func (h *Holder) offerOneHeldBack(key string) {
rec, rev, ok := h.freshAsk(key)
if !ok || rec.State != StateHeldBack {
return
}
now := h.Now()
rec.HeldBackSince = time.Time{}
rec.Tickets = h.ticketsFor(rec.Ask)
rec.State = StateOpen
if len(rec.Tickets) == 0 {
rec.Tickets, rec.State, rec.HeldSince, rec.HeldWhy = nil, StateHeld, now, h.holdWhy(rec.Ask)
}
if !h.saveHeld(key, rec, rev) {
return
}
h.Logf("[messenger] %s's ask %q (%s) is offered again", rec.Asker, rec.Ask.Headline, rec.Ask.ID)
if rec.State != StateOpen {
return
}
h.mu.Lock()
r := h.open[rec.Record]
h.mu.Unlock()
if r != nil && r.Origin == "ask" {
h.mu.Lock()
r.Answered, r.Ask = "", key
h.mu.Unlock()
h.offerAsk(r)
h.persist(r)
return
}
rec.Record = h.recordFor(rec.Asker, key, rec.Ask)
h.showAsk(key, rec, now)
}
// AskAgain is the seat operator-channel's verb ask-again (G §394, §450): it offers an ask held back before the channel stopped
// holding any back (A-424), and holds back nothing in its place. It answers the ask's state after it, or the error that says why
// it was not offered.
func (h *Holder) AskAgain(key string) oc.AskAgainAnswer {
h.work.Lock()
defer h.work.Unlock()
h.mu.Lock()
h.init()
h.mu.Unlock()
rec, _, ok := h.freshAsk(key)
switch {
case !ok:
return oc.AskAgainAnswer{Error: fmt.Sprintf("this router holds no ask %q: its key is <asker>.<id>, as operator-channel.asks lists", key)}
case rec.State != StateHeldBack:
return oc.AskAgainAnswer{Error: fmt.Sprintf("the ask %s is %s, not held back: only a held-back ask is offered again, and the channel holds none back any more", key, rec.State)}
}
h.offerOneHeldBack(key)
got, _, _ := h.freshAsk(key)
return oc.AskAgainAnswer{State: string(got.State)}
}