Files
mesh-delivery/modules/mesh-delivery-teller/cmd/mesh-delivery-teller/ends.go
T
jschoubben b39b0771a0 Subtask T: the teller says an assigner ask that ended at the channel without an answer as its event ask-ended (issue 1966)
An assigner ask that lapsed or was cancelled at the operator channel while its key still stands
was never asked again: the router drops a second ask under an id it keeps, and the assigner
only writes a new asking when the key is absent (note 151: the nats yes on novox and three
backup asks). The teller now says ask-ended (its module event, emits; body in its own package
tellerevents, ADR 0634 §1) when the router records such an end, and when a watch hands it a key
standing under an asking that ended so, which covers its start and the asks standing now. A
refusal, a replacement, an answer and an end by hand (teller_end_ask, kept in its own record)
are not said.

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-10 01:07:05 +01:00

107 lines
4.0 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"slices"
"time"
"github.com/nats-io/nats.go/jetstream"
"mesh-delivery-teller/internal/ends"
)
// EndAsk ends one ask of the teller's by hand (the tool teller_end_ask, issue 774): an ask open at the router is cancelled
// there and marked ended here, and one waiting for room is dropped from the wait, so it is asked no more under that router
// id. The ask is named by its router id or by the key of the gate's or the assigner's asks it was asked for (then its latest
// asking). An ask that is neither open nor waiting is said so and left: it holds no place at the router. The gate's or the
// assigner's key is not the teller's to take away: a later asking of it, under a new router id, is asked as any.
func (t *Teller) EndAsk(ask, why, by string) ends.Result {
t.mu.Lock()
defer t.mu.Unlock()
id, key := ask, ""
if s, rec, err := t.seenOf(ask); err != nil {
return ends.Result{Says: err.Error()}
} else if rec != nil {
key = s.Key
} else if g, grec, err := t.seenAt(gateRecordKey(ask)); err != nil {
return ends.Result{Says: err.Error()}
} else if grec != nil && g.ID != "" {
id, key = g.ID, ask
} else {
return ends.Result{Says: "the teller holds no ask under " + ask + ": name its router id, as operator-channel's asks show it, or the key of the gate's or the assigner's asks"}
}
r := ends.Result{ID: id, Key: key}
waited := false
if ws, rec, err := t.waiting(); err == nil && rec != nil {
if i := slices.IndexFunc(ws, func(x waitingAsk) bool { return x.ID == id }); i >= 0 {
if t.keepWaiting(slices.Delete(ws, i, i+1), rec) != nil {
r.Says = "the ask " + id + " waits for room, and the wait could not be written: it is not ended"
return r
}
waited = true
}
}
k, _, err := t.keptOf(id)
if err != nil {
r.Says = err.Error()
return r
}
if !open(k) {
r.Ended = waited
if waited {
r.Says = "the ask " + id + " waited for room at the router: it is dropped from the wait and asked no more"
} else {
r.Says = "the ask " + id + " is not open (answered, lapsed or ended already): it holds no place at the router, and nothing is done"
}
t.logf("%s: %s, by %s: %s", key, r.Says, by, why)
return r
}
t.cancelOpen(key, id)
if k, _, err := t.keptOf(id); err != nil || open(k) {
r.Says = "the ask " + id + " could not be cancelled at the router (see the teller's journal): it stays open"
return r
}
t.markHand(key, id)
r.Ended = true
r.Says = "the ask " + id + " is cancelled at the router, which frees its place; the next ask waiting for room is asked when the router says it ended"
t.logf("%s: the ask %s is ended by hand, by %s: %s", key, id, by, why)
return r
}
// endAsked answers one value of the state ends: one not yet answered is carried out and written back, by compare-and-set at
// its revision, with its result. One answered already (the teller's own write) is left.
func endAsked(ctx context.Context, t *Teller, kv jetstream.KeyValue, key string, rev uint64, value []byte) {
e, err := ends.Decode(value)
if err != nil {
t.logf("%s.%s holds what is not an end, left: %v", ends.State, key, err)
return
}
if e.Done() {
return
}
r := t.EndAsk(e.Ask, e.Why, e.By)
e.Result, e.Answered = &r, time.Now().UTC()
data, err := json.Marshal(e)
if err != nil {
return
}
if _, err := kv.Update(ctx, key, data, rev); err != nil && !errors.Is(err, jetstream.ErrKeyExists) && !errors.Is(err, jetstream.ErrKeyRevisionMismatch) {
t.logf("%s.%s: the answer could not be written: %v", ends.State, key, err)
}
}
// markHand keeps, beside the latest asking of key, that a person ended the asking id by hand, so the assigner is not told it
// ended at the channel (issue 1966). A record that names another asking is left.
func (t *Teller) markHand(key, id string) {
g, rec, err := t.seenAt(gateRecordKey(key))
if err != nil || rec == nil || g.ID != id || g.Hand {
return
}
g.Hand = true
if _, err := t.Store.Update(gateRecordKey(key), g, rec.Revision); err != nil {
t.logf("%s: that the ask %s was ended by hand could not be kept: %v", key, id, err)
}
}