Files
mesh-host/internal/link/taken.go
T
jschoubben 213542192c 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)
2026-11-22 02:57:45 +01:00

69 lines
2.1 KiB
Go

package link
import (
"context"
"fmt"
"time"
"git.novox.be/novox/mesh-sdk/go/process"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
// The node-engine takes an ask once by its id (design 47 H2 §775, §820; H-A-364 (a)): the ids it took are kept in its own
// state taken, under `<seat>.<ask>`, each for the ask's bound, bound over the link it serves its seat on.
// TakenBucket is the node-engine's state `taken`, named by its holder as every state is.
const TakenBucket = "mesh-host_" + process.TakenState
// TakenBus is a link that binds the node-engine's state taken.
type TakenBus interface {
TakenKV(ctx context.Context) (jetstream.KeyValue, error)
}
func takenKV(ctx context.Context, conn *nats.Conn) (jetstream.KeyValue, error) {
js, err := jetstream.New(conn)
if err != nil {
return nil, err
}
kv, err := js.KeyValue(ctx, TakenBucket)
if err != nil {
return nil, fmt.Errorf("the state %s cannot be bound: %w", TakenBucket, err)
}
return kv, nil
}
func (l *natsLink) TakenKV(ctx context.Context) (jetstream.KeyValue, error) {
return takenKV(ctx, l.conn)
}
func (b OverNATS) TakenKV(ctx context.Context) (jetstream.KeyValue, error) {
return takenKV(ctx, b.Conn)
}
var (
_ TakenBus = (*natsLink)(nil)
_ TakenBus = OverNATS{}
)
// 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. 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
q.mu.Unlock()
t, ok := bus.(TakenBus)
if bus == nil || !ok {
return false, ErrNoLink
}
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)
}