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
This commit is contained in:
@@ -601,14 +601,14 @@ func TestAnAskIsRefusedInWordsToItsAsker(t *testing.T) {
|
||||
}
|
||||
}
|
||||
all := strings.Join(refused, "\n")
|
||||
// A fourth offered ask is never refused (ADR 0666 §12): it is taken and the asker's oldest is held back.
|
||||
// A fourth offered ask is never refused and none is held back (design 47 G A-424).
|
||||
for _, want := range []string{"d-1: the ask is refused: an ask that destroys lasts at most", "d-2: its words carry"} {
|
||||
if !strings.Contains(all, want) {
|
||||
t.Errorf("not refused: %q in\n%s", want, all)
|
||||
}
|
||||
}
|
||||
if strings.Contains(all, "d-6") || store.asks["mesh-delivery.d-6"].State != StateOpen || store.asks["mesh-delivery.d-3"].State != StateHeldBack {
|
||||
t.Errorf("the fourth ask was refused or the oldest not held back: d-6 %s, d-3 %s", store.asks["mesh-delivery.d-6"].State, store.asks["mesh-delivery.d-3"].State)
|
||||
if strings.Contains(all, "d-6") || store.asks["mesh-delivery.d-6"].State != StateOpen || store.asks["mesh-delivery.d-3"].State != StateOpen {
|
||||
t.Errorf("the fourth ask was refused or the oldest held back: d-6 %s, d-3 %s", store.asks["mesh-delivery.d-6"].State, store.asks["mesh-delivery.d-3"].State)
|
||||
}
|
||||
// A refused ask is kept as a closed record with its outcome and words (design 47 G §570), and `asks` lists it refused.
|
||||
if rec, kept := store.asks["mesh-delivery.d-1"]; !kept || rec.State != string(occ.OutcomeRefused) || rec.Warrant == nil ||
|
||||
|
||||
@@ -563,9 +563,9 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
h.refuseAsk(asker, a, "its whole words carry "+refusal.String()+", which may not leave the mesh on any channel: withhold it")
|
||||
return nil
|
||||
}
|
||||
// A fourth offered ask is taken, never refused: the asker's oldest offered one is held back once this one is recorded
|
||||
// (holdBackOldest). An ask with the same `about` replaces the open one, a held-back one too.
|
||||
open, replaced := 0, ""
|
||||
// Every ask is offered when it is raised: the channel keeps no count of an asker's open asks and holds none back for room
|
||||
// (design 47 G A-424). An ask with the same `about` replaces the open one, a held-back one of before too.
|
||||
replaced := ""
|
||||
h.mu.Lock()
|
||||
for k, e := range h.asks {
|
||||
if e.rec.Asker != asker || !liveState(e.rec.State) {
|
||||
@@ -573,14 +573,9 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
}
|
||||
if a.About != "" && e.rec.Ask.About == a.About {
|
||||
replaced = k
|
||||
continue
|
||||
}
|
||||
if e.rec.State != StateHeldBack {
|
||||
open++
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
holdBack := open >= occ.MostOpen
|
||||
rec := AskRecord{Asker: asker, Ask: a, State: StateOpen, Opened: now, Tickets: h.ticketsFor(a)}
|
||||
if len(rec.Tickets) == 0 {
|
||||
// Held, not refused (ADR 0397): the ask breaks no rule, and the operator is asked when a channel can carry it.
|
||||
@@ -611,9 +606,6 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
} else {
|
||||
h.keepAsk(key, rec, 0)
|
||||
}
|
||||
if holdBack {
|
||||
h.holdBackOldest(asker, key)
|
||||
}
|
||||
if rec.State == StateHeld {
|
||||
h.Logf("[messenger] %s asks %q (%s): held, not shown: %s", asker, a.Headline, a.ID, rec.HeldWhy)
|
||||
return nil
|
||||
|
||||
@@ -105,7 +105,8 @@ func TestAHeldAskExpiresAsAnyOpenAskDoes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAHeldAskCountsTowardTheOffersOfItsAskerAndTheOldestIsHeldBack(t *testing.T) {
|
||||
// Design 47 G A-424: four held asks of one asker are taken and held for want of a channel, and none is held back for room.
|
||||
func TestFourHeldAsksOfOneAskerAreHeldAndNoneHeldBack(t *testing.T) {
|
||||
h, _, _, c, store, w := newRouter(t)
|
||||
withoutVerifiedSender(h)
|
||||
for i := 1; i <= 4; i++ {
|
||||
@@ -114,8 +115,8 @@ func TestAHeldAskCountsTowardTheOffersOfItsAskerAndTheOldestIsHeldBack(t *testin
|
||||
submit(t, h, "mesh-delivery", a)
|
||||
c.pass(time.Second)
|
||||
}
|
||||
if got := w.of("mesh-delivery"); len(got) != 0 || store.asks["mesh-delivery.d-1"].State != StateHeldBack || store.asks["mesh-delivery.d-4"].State != StateHeld {
|
||||
t.Fatalf("%+v: a fourth is taken, never refused; the oldest is held back (%s)", got, store.asks["mesh-delivery.d-1"].State)
|
||||
if got := w.of("mesh-delivery"); len(got) != 0 || store.asks["mesh-delivery.d-1"].State != StateHeld || store.asks["mesh-delivery.d-4"].State != StateHeld {
|
||||
t.Fatalf("%+v: a fourth is taken, never refused, and none is held back (d-1 %s)", got, store.asks["mesh-delivery.d-1"].State)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,12 @@ import (
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// An ask held back (novox/hq ADR 0666 §12, design 46 § 6): an asker has at most MostOpen asks offered at once; a fourth
|
||||
// is offered and nothing is refused, so no ask is hidden: the asker's oldest offered ask is held back, its tickets
|
||||
// withdrawn and its message edited to say it waits behind newer asks, and it is offered again, with new tickets, when
|
||||
// one of the asker's offered asks ends. `held` is another state, an ask no ready channel can carry (ADR 0397).
|
||||
// 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"
|
||||
@@ -90,50 +88,35 @@ func (h *Holder) freshAsk(key string) (AskRecord, uint64, bool) {
|
||||
return rec, rev, true
|
||||
}
|
||||
|
||||
// holdBackOldest holds back the asker's oldest offered ask other than except (the newest, just taken): its tickets
|
||||
// are withdrawn, it stays open, and its own message is edited to say it waits. One held for want of a channel counts as offered
|
||||
// and is held back the same way. The work lock is held.
|
||||
func (h *Holder) holdBackOldest(asker, except string) {
|
||||
h.mu.Lock()
|
||||
key := h.pickLocked(asker, except, StateOpen, StateHeld)
|
||||
h.mu.Unlock()
|
||||
if key == "" {
|
||||
return
|
||||
// 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
|
||||
}
|
||||
}
|
||||
rec, rev, ok := h.freshAsk(key)
|
||||
if !ok || (rec.State != StateOpen && rec.State != StateHeld) {
|
||||
return
|
||||
}
|
||||
now := h.Now()
|
||||
rec.State, rec.HeldBackSince, rec.Tickets, rec.HeldSince, rec.HeldWhy = StateHeldBack, now, nil, time.Time{}, ""
|
||||
if !h.saveHeld(key, rec, rev) {
|
||||
return
|
||||
}
|
||||
h.Logf("[messenger] %s's ask %q (%s) is held back behind its newer asks: it is offered again when one ends", asker, rec.Ask.Headline, rec.Ask.ID)
|
||||
h.mu.Lock()
|
||||
r := h.open[rec.Record]
|
||||
h.mu.Unlock()
|
||||
if r == nil || r.Origin != "ask" {
|
||||
return // a condition's message is the newer ask's to show
|
||||
}
|
||||
h.mu.Lock()
|
||||
r.Answered = "Waits behind newer questions of " + asker + ".\nIt is offered again, with its answers, when one of them ends."
|
||||
h.mu.Unlock()
|
||||
h.edit(r, "answered", false)
|
||||
h.persist(r)
|
||||
}
|
||||
|
||||
// offerHeldBack offers the asker's oldest held-back ask again when a slot is free: new tickets, its message edited back
|
||||
// to the question. The work lock is held.
|
||||
func (h *Holder) offerHeldBack(asker string) {
|
||||
if h.offeredCount(asker) >= occ.MostOpen {
|
||||
return
|
||||
}
|
||||
// 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()
|
||||
key := h.pickLocked(asker, "", StateHeldBack)
|
||||
askers := map[string]bool{}
|
||||
for _, e := range h.asks {
|
||||
if e.rec.State == StateHeldBack {
|
||||
askers[e.rec.Asker] = true
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
if key != "" {
|
||||
h.offerOneHeldBack(key)
|
||||
for asker := range askers {
|
||||
h.offerHeldBack(asker)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,9 +155,9 @@ func (h *Holder) offerOneHeldBack(key string) {
|
||||
h.showAsk(key, rec, now)
|
||||
}
|
||||
|
||||
// AskAgain is the seat operator-channel's verb ask-again (G §394, §450): offer a held-back ask now, holding back the
|
||||
// asker's oldest offered ask in its place when the asker already has the most offered. It answers the ask's state after it,
|
||||
// or the error that says why it was not offered.
|
||||
// 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()
|
||||
@@ -186,10 +169,7 @@ func (h *Holder) AskAgain(key string) oc.AskAgainAnswer {
|
||||
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", key, rec.State)}
|
||||
}
|
||||
if h.offeredCount(rec.Asker) >= occ.MostOpen {
|
||||
h.holdBackOldest(rec.Asker, key)
|
||||
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)
|
||||
|
||||
@@ -9,100 +9,50 @@ import (
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// ADR 0666 §12, design 46 §6 and test 71: an asker has at most three asks offered at once; a fourth is offered and the
|
||||
// oldest is held back (its tickets withdrawn, its message edited, its record open), offered again with new tickets
|
||||
// when one of the three ends, offered now by messenger_ask_again; an ask with the same `about` replaces a held-back
|
||||
// one, and a Decline ends an ask whose asker may ask again at once.
|
||||
func TestNoAskIsHiddenTheOldestOfFourIsHeldBackAndOfferedAgain(t *testing.T) {
|
||||
h, tg, _, c, store, w := newRouter(t)
|
||||
// Design 47 G A-424: the channel keeps no count of an asker's open asks and holds none back: every ask is offered when it is
|
||||
// raised, five of one asker at once too, and none is refused. An ask held back before the change is offered again at the
|
||||
// router's next tick, with new tickets; ask-again offers one now and holds nothing back in its place.
|
||||
func TestEveryAskIsOfferedAndOneHeldBackBeforeIsOfferedAgain(t *testing.T) {
|
||||
h, _, _, c, store, w := newRouter(t)
|
||||
link(h, store, c, "42", c.now().Add(-2*time.Hour))
|
||||
ask := func(id, about string) {
|
||||
state := func(id string) string { return store.asks["mesh-delivery."+id].State }
|
||||
for i, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} {
|
||||
a := ackAsk(c, id)
|
||||
a.About = about
|
||||
a.About = "about-" + string(rune('1'+i))
|
||||
submit(t, h, "mesh-delivery", a)
|
||||
c.pass(time.Second)
|
||||
settle(h, c)
|
||||
}
|
||||
state := func(id string) string { return store.asks["mesh-delivery."+id].State }
|
||||
for i, id := range []string{"d-1", "d-2", "d-3"} {
|
||||
ask(id, "about-"+string(rune('1'+i)))
|
||||
}
|
||||
oldTicket := ""
|
||||
for tk := range store.asks["mesh-delivery.d-1"].Tickets {
|
||||
oldTicket = tk
|
||||
}
|
||||
ask("d-4", "about-4")
|
||||
// M1: the fourth reaches the channel, the oldest is held back, none is refused.
|
||||
if len(w.of("mesh-delivery")) != 0 || state("d-4") != StateOpen || state("d-1") != StateHeldBack {
|
||||
t.Fatalf("refused %+v; d-4 %s, d-1 %s", w.of("mesh-delivery"), state("d-4"), state("d-1"))
|
||||
}
|
||||
if rec := store.asks["mesh-delivery.d-1"]; len(rec.Tickets) != 0 || rec.HeldBackSince.IsZero() {
|
||||
t.Errorf("held back %+v: tickets withdrawn, since set", rec)
|
||||
}
|
||||
if m, ok := tg.edits["1"]; !ok || len(m.Actions) != 0 || !strings.Contains(m.Note, "Waits behind newer questions of mesh-delivery") {
|
||||
t.Errorf("its message was not edited to say it waits: %v %+v", ok, m)
|
||||
}
|
||||
if _, err := h.Chosen("telegram", choiceOf(oldTicket, "42", c.now())); err == nil || !strings.Contains(err.Error(), "waits behind newer questions") || len(w.of("mesh-delivery")) != 0 {
|
||||
t.Errorf("a tap of a withdrawn ticket: %v %+v", err, w.of("mesh-delivery"))
|
||||
}
|
||||
// M3: asks lists it with since when and the newer asks it waits behind; open includes it.
|
||||
q, _ := AsksQueryOf(map[string]any{"key": "mesh-delivery.d-1"})
|
||||
raw, _ := json.Marshal(q.Narrow(h.AsksNow()))
|
||||
for _, want := range []string{`"state":"held-back"`, `"held-since":"`, `"behind":["mesh-delivery.d-2","mesh-delivery.d-3","mesh-delivery.d-4"]`} {
|
||||
if !strings.Contains(string(raw), want) {
|
||||
t.Errorf("asks lacks %s: %s", want, raw)
|
||||
for _, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} {
|
||||
if state(id) != StateOpen || len(store.asks["mesh-delivery."+id].Tickets) == 0 {
|
||||
t.Fatalf("%s is %s with %d tickets: every ask is offered", id, state(id), len(store.asks["mesh-delivery."+id].Tickets))
|
||||
}
|
||||
}
|
||||
if q, _ = AsksQueryOf(nil); !strings.Contains(string(mustJSON(q.Narrow(h.AsksNow()))), "mesh-delivery.d-1") {
|
||||
t.Error("the open asks leave out the held-back one")
|
||||
}
|
||||
// M5: an ask with the same about replaces a held-back one; the three offered stay three, so the oldest of them is held back.
|
||||
ask("d-5", "about-1")
|
||||
if state("d-1") != string(occ.OutcomeReplaced) || state("d-2") != StateHeldBack || state("d-5") != StateOpen || state("d-3") != StateOpen {
|
||||
t.Fatalf("after d-5 about the held-back ask's thing: d-1 %s d-2 %s d-3 %s d-5 %s", state("d-1"), state("d-2"), state("d-3"), state("d-5"))
|
||||
}
|
||||
// M2: when one of the offered ends, the oldest held back is offered again with new tickets and its message asks again.
|
||||
if a := h.AskAgain("mesh-delivery.d-3"); !strings.Contains(a.Error, "not held back") {
|
||||
t.Errorf("ask-again of an offered ask: %+v", a)
|
||||
}
|
||||
if err := h.TakeAsk("cancel", "mesh-delivery", []byte(`{"id":"d-3"}`)); err != nil {
|
||||
// One held back before the change, as the store kept it.
|
||||
key := "mesh-delivery.d-2"
|
||||
got, rev, _ := store.GetAsk(key)
|
||||
got.State, got.Tickets, got.HeldBackSince = StateHeldBack, nil, c.now()
|
||||
rev, err := store.UpdateAsk(key, *got, rev)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settle(h, c)
|
||||
if rec := store.asks["mesh-delivery.d-2"]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() {
|
||||
t.Fatalf("d-2 after d-3 ended: %+v", rec)
|
||||
}
|
||||
if m := tg.edits["2"]; len(m.Actions) == 0 || strings.Contains(m.Note, "Waits behind") {
|
||||
t.Errorf("its message does not ask again: %+v", m)
|
||||
}
|
||||
// M4: ask-again by hand offers a held-back ask now, holding back the oldest offered one; the seat operator-channel's ask-again (G §450).
|
||||
ask("d-6", "about-6")
|
||||
ask("d-7", "about-7")
|
||||
held := ""
|
||||
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
|
||||
if state(id) == StateHeldBack {
|
||||
held = id
|
||||
}
|
||||
}
|
||||
if held == "" {
|
||||
t.Fatal("no ask is held back after two more")
|
||||
h.keepAsk(key, *got, rev)
|
||||
if a := h.AskAgain("mesh-delivery.d-1"); !strings.Contains(a.Error, "not held back") {
|
||||
t.Errorf("ask-again of an offered ask: %+v", a)
|
||||
}
|
||||
if a := h.AskAgain("mesh-delivery.d-none"); a.Error == "" || a.State != "" {
|
||||
t.Errorf("ask-again of an ask the router holds none of: %+v", a)
|
||||
}
|
||||
if a := h.AskAgain("mesh-delivery." + held); a.Error != "" || a.State != string(StateOpen) || state(held) != StateOpen {
|
||||
t.Fatalf("%s is %s; ask-again answered %+v", held, state(held), a)
|
||||
h.Tick()
|
||||
settle(h, c)
|
||||
if rec := store.asks[key]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() {
|
||||
t.Fatalf("the ask held back before the change, after a tick: %+v", rec)
|
||||
}
|
||||
n := 0
|
||||
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
|
||||
if state(id) == StateHeldBack {
|
||||
n++
|
||||
for _, id := range []string{"d-1", "d-3", "d-4", "d-5"} {
|
||||
if state(id) != StateOpen {
|
||||
t.Errorf("%s is %s after the held-back one was offered: nothing is held back in its place", id, state(id))
|
||||
}
|
||||
}
|
||||
if n != 2 || state("d-5") != StateHeldBack {
|
||||
t.Errorf("%d asks held back after ask-again (d-5 %s): the oldest offered one took its place", n, state("d-5"))
|
||||
}
|
||||
// A Decline ends an ask; its asker may ask again at once, and nothing is refused.
|
||||
for _, wt := range w.of("mesh-delivery") {
|
||||
if wt.Outcome == occ.OutcomeRefused {
|
||||
t.Errorf("refused: %+v", wt)
|
||||
|
||||
@@ -778,6 +778,7 @@ func (h *Holder) Tick() {
|
||||
h.mu.Unlock()
|
||||
sort.Slice(recs, func(i, j int) bool { return recs[i].Raised.Before(recs[j].Raised) })
|
||||
h.sweepAsks(now)
|
||||
h.offerAllHeldBack() // held back before the channel stopped holding any back, offered at the first tick (design 47 G A-424)
|
||||
h.releaseHeld()
|
||||
h.offerUnsent()
|
||||
h.breakGlass()
|
||||
|
||||
Reference in New Issue
Block a user