H-A-458 (3): bind the taken write by the compose call's own context

The state taken's write was bound by a five-second bound of its own,
whatever the ask's deadline, so an ask whose caller had gone could still be
kept, and its repeat refused. takenWithin goes: Env.Taken carries the
compose call's context, Session.TakeOnce binds the state with it and keeps
nothing once it ended. Row: a take whose context ended is refused, and the
same id is then taken the first time.

Review round 1, finding: the taken bound.

Builds: design 47 H2 §775; H-A-364 (a), H-A-458 (3)
This commit is contained in:
2026-11-22 02:57:45 +01:00
parent 7d87dde0a4
commit 213542192c
5 changed files with 22 additions and 12 deletions
+11 -1
View File
@@ -138,10 +138,20 @@ func TestNatsTheRecoveryStepAsksHearsBoundsAndCatchesUpOverTheBus(t *testing.T)
// Design 47 H2 §775, H-A-364: over the same session, an ask of the seat is taken once in the state taken: the first time,
// and never again within its bound.
for i, want := range []bool{true, false} {
if took, err := session.TakeOnce(nodeengine.Seat, "ask-1", time.Minute); err != nil || took != want {
if took, err := session.TakeOnce(ctx, nodeengine.Seat, "ask-1", time.Minute); err != nil || took != want {
t.Fatalf("take %d of ask-1: %v, %v; want %v", i+1, took, err, want)
}
}
// H-A-458 (3): the write is bound by the ask's own context: an ask whose context ended keeps nothing, so the same id is
// taken the first time once asked again within a live one.
gone, end := context.WithCancel(ctx)
end()
if took, err := session.TakeOnce(gone, nodeengine.Seat, "ask-2", time.Minute); err == nil || took {
t.Fatalf("a take of ask-2 whose context ended: %v, %v; want refused", took, err)
}
if took, err := session.TakeOnce(ctx, nodeengine.Seat, "ask-2", time.Minute); err != nil || !took {
t.Fatalf("ask-2 asked again within a live context: %v, %v; want taken the first time", took, err)
}
events, stop, err := r.Hear(ctx, "restored", "db", "cache")
if err != nil {
+6 -7
View File
@@ -45,12 +45,10 @@ var (
_ TakenBus = OverNATS{}
)
// takenWithin bounds binding the state taken.
const takenWithin = 5 * time.Second
// TakeOnce takes the ask ask of seat once, in the state taken for bound (process.State.TakeOnce): true the first time it
// is given, false when it was taken within its bound. ErrNoLink when no link is held.
func (q *Session) TakeOnce(seat, ask string, bound time.Duration) (bool, error) {
// is given, false when it was taken within its bound. ErrNoLink when no link is held. The write is bound by ctx, the ask's
// own deadline (H-A-458 (3), P11): no bound of its own, and an ask whose context ended keeps nothing.
func (q *Session) TakeOnce(ctx context.Context, seat, ask string, bound time.Duration) (bool, error) {
q.init()
q.mu.Lock()
bus := q.bus
@@ -59,11 +57,12 @@ func (q *Session) TakeOnce(seat, ask string, bound time.Duration) (bool, error)
if bus == nil || !ok {
return false, ErrNoLink
}
ctx, cancel := context.WithTimeout(context.Background(), takenWithin)
defer cancel()
kv, err := t.TakenKV(ctx)
if err != nil {
return false, err
}
if err := ctx.Err(); err != nil {
return false, fmt.Errorf("the ask %s of %s is not kept in the state %s: its context ended: %w", ask, seat, TakenBucket, err)
}
return process.StateOver("mesh-host", process.TakenState, kv).TakeOnce(seat, ask, bound)
}
+1 -1
View File
@@ -159,7 +159,7 @@ func (s *Shadow) ComposeCandidate(ctx context.Context, ask nodeengine.ComposeCan
a, joined := s.asked.once(ctx, id, func() nodeengine.ComposeCandidateAnswer {
took := ""
if c.env.Taken != nil {
switch first, err := c.env.Taken(nodeengine.Seat, id, c.composeAskBound()); {
switch first, err := c.env.Taken(ctx, nodeengine.Seat, id, c.composeAskBound()); {
case err != nil:
took = fmt.Sprintf(", not kept in taken: %v", err)
case !first:
+1 -1
View File
@@ -88,7 +88,7 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
// Design 47 H2 §775, H-A-364: an ask with an id is taken once in the state taken, for compose-ask-bound.
var takes []string
taken := map[string]bool{}
env.Taken = func(seat, ask string, bound time.Duration) (bool, error) {
env.Taken = func(_ context.Context, seat, ask string, bound time.Duration) (bool, error) {
takes = append(takes, seat+"."+ask+" "+bound.String())
first := !taken[ask]
taken[ask] = true
+3 -2
View File
@@ -154,8 +154,9 @@ type Env struct {
// (design 08 control-verbs §1, H-A-314 (5)).
ComposeAskBound time.Duration
// Taken takes an ask of the seat seat once by its id (design 47 H2 §775, H-A-364 (a)): kept in the node-engine's state
// `taken` under `<seat>.<ask>` for bound; false when it was taken before. Nil keeps nothing.
Taken func(seat, ask string, bound time.Duration) (bool, error)
// `taken` under `<seat>.<ask>` for bound; false when it was taken before. Nil keeps nothing. Its write is bound by ctx,
// the compose call's own (H-A-458 (3)).
Taken func(ctx context.Context, seat, ask string, bound time.Duration) (bool, error)
// Registry asks the registry seats' verb versions (oci-artifact-registry, container-image-registry) in the engine's own
// name: a want naming its version is resolved to its artifacts' digests through it (design 47 G A-430a).
Registry meshcatalogclient.Call