diff --git a/cmd/mesh-host/recovery_integration_test.go b/cmd/mesh-host/recovery_integration_test.go index 70956784..4c8d2cdb 100644 --- a/cmd/mesh-host/recovery_integration_test.go +++ b/cmd/mesh-host/recovery_integration_test.go @@ -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 { diff --git a/internal/link/taken.go b/internal/link/taken.go index 293285d2..5861e6b8 100644 --- a/internal/link/taken.go +++ b/internal/link/taken.go @@ -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) } diff --git a/internal/selfcompose/candidate.go b/internal/selfcompose/candidate.go index 0badb931..52fc655a 100644 --- a/internal/selfcompose/candidate.go +++ b/internal/selfcompose/candidate.go @@ -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: diff --git a/internal/selfcompose/candidate_test.go b/internal/selfcompose/candidate_test.go index 791a10ed..83433662 100644 --- a/internal/selfcompose/candidate_test.go +++ b/internal/selfcompose/candidate_test.go @@ -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 diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index 0850f3a3..cf265bfd 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -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 `.` for bound; false when it was taken before. Nil keeps nothing. - Taken func(seat, ask string, bound time.Duration) (bool, error) + // `taken` under `.` 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