diff --git a/node-tools/internal/bus/bus.go b/node-tools/internal/bus/bus.go index 8e843af..9997853 100644 --- a/node-tools/internal/bus/bus.go +++ b/node-tools/internal/bus/bus.go @@ -32,9 +32,18 @@ import ( "github.com/novox/mesh-tools/node-tools/internal/wire" ) -// RequestTimeout is how long a call waits for its answer — what modules already expect. +// RequestTimeout is how long a call waits for its answer when its verb's definition names no `timeout`: the tool runner's own +// bound, what modules already expect (design 47 H §760, H-A-43). const RequestTimeout = 30 * time.Second +// waitOf is the bound a call waits: within where the verb's definition names one, else the tool runner's own. +func waitOf(within time.Duration) time.Duration { + if within > 0 { + return within + } + return RequestTimeout +} + // Credential is a broker credential as the mesh delivers it (novox/hq ADR 0120). type Credential struct { URL string `json:"url"` @@ -793,6 +802,12 @@ func (c *Conn) reachedAt(key string) (string, error) { // Ask calls a tool by key — or on a subject the mesh listed for it — and learns which machine // answered. Core request/reply: a tool call is never persisted (design 25 §3). func (c *Conn) Ask(key string, body any, on string) (Answered, error) { + return c.AskWithin(key, body, on, 0) +} + +// AskWithin is Ask that waits within for its answer, the `timeout` its verb's definition names (design 47 H §760, H-A-43); +// zero waits the tool runner's own bound, RequestTimeout. +func (c *Conn) AskWithin(key string, body any, on string, within time.Duration) (Answered, error) { subject := on if subject == "" { s, err := c.reachedAt(key) @@ -809,12 +824,12 @@ func (c *Conn) Ask(key string, body any, on string) (Answered, error) { // account publish with its own name alone; asked again on the subject that names no caller only when the // first reached nobody — nothing serves it yet, or the bus does not grant it yet — so no call is done twice. if called := CalledSubject(subject, c.cred.User); called != "" { - answered, fallBack, err := c.askCalled(called, data) + answered, fallBack, err := c.askCalled(called, data, within) if !fallBack { return answered, err } } - return c.askOnce(subject, data) + return c.askOnce(subject, data, within) } // ErrNoResponders is the bus's own refusal: nothing subscribes to the subject asked. It is told apart @@ -822,8 +837,8 @@ func (c *Conn) Ask(key string, body any, on string) (Answered, error) { var ErrNoResponders = errors.New("503 no responders") // askOnce asks once. -func (c *Conn) askOnce(subject string, data []byte) (Answered, error) { - msg, err := c.nc.Request(subject, data, RequestTimeout) +func (c *Conn) askOnce(subject string, data []byte, within time.Duration) (Answered, error) { + msg, err := c.nc.Request(subject, data, waitOf(within)) if err != nil { if errors.Is(err, nats.ErrNoResponders) { return Answered{}, ErrNoResponders @@ -1036,6 +1051,11 @@ func SeatToolSubject(seat, verb, scope, node string) string { // `.` another's, `seat:.[@]` a role's — resolved through what the // mesh issued that module to reach, as its own runtime resolved it, else the derived shape. func (c *Conn) AskAs(module, key string, body any) (Answered, error) { + return c.AskAsWithin(module, key, body, 0) +} + +// AskAsWithin is AskAs that waits within for its answer (design 47 H §760); zero waits RequestTimeout. +func (c *Conn) AskAsWithin(module, key string, body any, within time.Duration) (Answered, error) { name, wanted, _ := strings.Cut(key, "@") subject := "" if m := c.Membership(module); m != nil { @@ -1058,7 +1078,7 @@ func (c *Conn) AskAs(module, key string, body any) (Answered, error) { } subject = s } - return c.Ask(key, body, subject) + return c.AskWithin(key, body, subject, within) } // EventsStream is where every module's events land, and ConsumerOf the durable consumer the diff --git a/node-tools/internal/bus/bus_test.go b/node-tools/internal/bus/bus_test.go index a44a3e7..9e3f8bd 100644 --- a/node-tools/internal/bus/bus_test.go +++ b/node-tools/internal/bus/bus_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "testing" + "time" ) func TestSubjectsAreTheOnesTheTypeScriptRuntimeUses(t *testing.T) { @@ -60,3 +61,12 @@ func TestAnEventDeliveredAgainHasItsOwnKey(t *testing.T) { } } } + +// Design 47 H §760: a call waits the bound its verb's definition names, else the tool runner's own. +func TestWaitOfIsTheVerbsBoundElseTheRunnersOwn(t *testing.T) { + for within, want := range map[time.Duration]time.Duration{0: RequestTimeout, 40 * time.Second: 40 * time.Second, 600 * time.Second: 600 * time.Second} { + if got := waitOf(within); got != want { + t.Errorf("waitOf(%s) = %s, want %s", within, got, want) + } + } +} diff --git a/node-tools/internal/bus/caller.go b/node-tools/internal/bus/caller.go index b9fa39e..24e07fe 100644 --- a/node-tools/internal/bus/caller.go +++ b/node-tools/internal/bus/caller.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "sync" + "time" "github.com/nats-io/nats.go" ) @@ -150,9 +151,9 @@ func (c *Conn) publishRefused(err error) bool { // askCalled asks once on a caller-named subject. fallBack says the call reached nobody — nothing serves that // subject yet, or the bus refused this user its publish — so it may be asked on the subject that names no // caller without being done twice. -func (c *Conn) askCalled(subject string, data []byte) (answered Answered, fallBack bool, err error) { +func (c *Conn) askCalled(subject string, data []byte, within time.Duration) (answered Answered, fallBack bool, err error) { ctx, cancel := context.WithCancelCause(context.Background()) - ctx, stop := context.WithTimeout(ctx, RequestTimeout) + ctx, stop := context.WithTimeout(ctx, waitOf(within)) defer stop() c.calls.mu.Lock() if c.calls.pending == nil { diff --git a/node-tools/internal/bus/caller_bus_test.go b/node-tools/internal/bus/caller_bus_test.go index c46049d..6a6f051 100644 --- a/node-tools/internal/bus/caller_bus_test.go +++ b/node-tools/internal/bus/caller_bus_test.go @@ -216,3 +216,30 @@ func TestAFallBackForWantOfAResponderIsSaidOncePerSubject(t *testing.T) { t.Errorf("the fall-back was said %d times, want once: %v", n, said) } } + +// Design 47 H §760: a call waits the bound its verb's definition names, and ends there with the timeout; the caller of a verb whose +// definition names none waits the tool runner's own bound, RequestTimeout. +func TestACallWaitsTheBoundItsVerbNamesAndTimesOutThere(t *testing.T) { + url := grantedBus(t, map[string][]string{"anchor.node-tools": {"_INBOX.>"}, "shanks.node-tools": {"mesh.mod.*.tool.>"}}) + server := dial(t, url, "node-tools", "anchor", "anchor.node-tools") + release := make(chan struct{}) + stop, err := server.HandleSubjectCalled("mesh.mod.ledger.tool.slow", func(string, json.RawMessage) (any, error) { + <-release + return map[string]any{}, nil + }) + if err != nil { + t.Fatal(err) + } + defer stop() + defer close(release) + server.Flush() + asker := dial(t, url, "node-tools", "shanks", "shanks.node-tools") + began := time.Now() + _, err = asker.AskWithin("ledger.slow", map[string]any{}, "", 200*time.Millisecond) + if err == nil || err.Error() != "timeout" { + t.Fatalf("a call whose verb names a bound the holder outlasts: %v, want the timeout", err) + } + if took := time.Since(began); took > bus.RequestTimeout/2 { + t.Errorf("the call waited %s, want its own bound of 200ms, not the tool runner's %s", took, bus.RequestTimeout) + } +} diff --git a/node-tools/internal/bus/chunked.go b/node-tools/internal/bus/chunked.go index 7b9257b..3d7f846 100644 --- a/node-tools/internal/bus/chunked.go +++ b/node-tools/internal/bus/chunked.go @@ -25,6 +25,11 @@ const FileField = "file" // counts with buscalls.SeatChunks, each acknowledged before the next, and answers the verb's own answer with the whole // file added as FileField. The asking bundle receives no chunk itself. func (c *Conn) AskAsChunked(module, key string, body any) (Answered, error) { + return c.AskAsChunkedWithin(module, key, body, 0) +} + +// AskAsChunkedWithin is AskAsChunked that waits within for the verb's answer (design 47 H §760); zero waits RequestTimeout. +func (c *Conn) AskAsChunkedWithin(module, key string, body any, within time.Duration) (Answered, error) { // The verb may be asked of the machine that ran the ask (`@`, A-254): the chunk subjects name the seat, the verb and // the call alone. name, _, _ := strings.Cut(key, "@") @@ -52,7 +57,7 @@ func (c *Conn) AskAsChunked(module, key string, body any) (Answered, error) { return Answered{}, fmt.Errorf("the chunks of %s cannot be heard: %w", key, err) } defer func() { _ = sub.Unsubscribe() }() - answered, err := c.AskAs(module, key, args) + answered, err := c.AskAsWithin(module, key, args, within) if err != nil { return answered, err } diff --git a/node-tools/internal/runtime/caller_test.go b/node-tools/internal/runtime/caller_test.go index 40fb1ff..20d6532 100644 --- a/node-tools/internal/runtime/caller_test.go +++ b/node-tools/internal/runtime/caller_test.go @@ -152,6 +152,45 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { t.Error("a runner that holds no seat definitions asks no chunks") } }) + // Design 47 H §760: a bundle's ask waits the `timeout` its verb's definition names, whole seconds; a verb that names none, a + // seat the definitions do not hold, a module's tool and a runner that holds no definitions wait the runner's own bound (zero). + t.Run("AVerbsTimeoutIsTheBoundItsAskWaits", func(t *testing.T) { + forty, six := 40, 600 + defs := map[string]seatdef.Definition{"mesh-broker": {Verbs: []seatdef.Verb{{Name: "rates", Timeout: &forty}, {Name: "slow", Chunks: true, Timeout: &six}, {Name: "plain"}}}} + for _, row := range []struct { + key string + want time.Duration + way string + }{ + {"seat:mesh-broker.rates", 40 * time.Second, "plain"}, + {"seat:mesh-broker.rates@home-server", 40 * time.Second, "plain"}, + {"seat:mesh-broker.slow", 600 * time.Second, "chunked"}, + {"seat:mesh-broker.plain", 0, "plain"}, + {"seat:mesh-unread.rates", 0, "plain"}, + {"mesh-delivery-indexer.indexer", 0, "plain"}, + } { + var got time.Duration + var way string + c := &consumers{definitions: func() map[string]seatdef.Definition { return defs }, + askPlain: func(_, _ string, _ any, within time.Duration) (bus.Answered, error) { + got, way = within, "plain" + return bus.Answered{}, nil + }, + askChunked: func(_, _ string, _ any, within time.Duration) (bus.Answered, error) { + got, way = within, "chunked" + return bus.Answered{}, nil + }} + if _, err := (&moduleBus{all: c, module: "m"}).Ask(json.RawMessage(`{"key":"` + row.key + `","body":{}}`)); err != nil { + t.Fatal(err) + } + if got != row.want || way != row.way { + t.Errorf("Ask %s waited %s (%s), want %s (%s)", row.key, got, way, row.want, row.way) + } + } + if got := (&consumers{}).withinOf("seat:mesh-broker.rates"); got != 0 { + t.Errorf("a runner that holds no definitions waits %s, want its own bound", got) + } + }) // Design 47 G A-490m: a verb of a seat the list defines whose definition does not name it is read again once and never asked // without its call; a seat the list does not define, a module's tool and a bundle asking through moduleBus.Ask behave as before. t.Run("AVerbTheDefinitionDoesNotNameIsReadAgainAndNeverAskedWithoutItsCall", func(t *testing.T) { @@ -175,8 +214,14 @@ func TestTheSubjectThatNamesNoCallerStillWorksAndNamesNone(t *testing.T) { t.Run(row.name, func(t *testing.T) { held, reads, way := stale, 0, "" c := &consumers{definitions: func() map[string]seatdef.Definition { return held }, reread: func() error { reads++; return row.again(&held) }, - askPlain: func(_, _ string, _ any) (bus.Answered, error) { way = "plain"; return bus.Answered{}, nil }, - askChunked: func(_, _ string, _ any) (bus.Answered, error) { way = "chunked"; return bus.Answered{}, nil }} + askPlain: func(_, _ string, _ any, _ time.Duration) (bus.Answered, error) { + way = "plain" + return bus.Answered{}, nil + }, + askChunked: func(_, _ string, _ any, _ time.Duration) (bus.Answered, error) { + way = "chunked" + return bus.Answered{}, nil + }} _, err := (&moduleBus{all: c, module: "m"}).Ask(json.RawMessage(`{"key":"` + row.key + `","body":{}}`)) if way != row.wantWay || reads != row.wantReads || (row.wantErr == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), row.wantErr)) { t.Errorf("Ask %s: asked %q, %v after %d reads, want %q, %q after %d", row.key, way, err, reads, row.wantWay, row.wantErr, row.wantReads) diff --git a/node-tools/internal/runtime/gate.go b/node-tools/internal/runtime/gate.go index 8777b3d..289c94f 100644 --- a/node-tools/internal/runtime/gate.go +++ b/node-tools/internal/runtime/gate.go @@ -3,6 +3,7 @@ package runtime import ( "fmt" "strings" + "time" "git.novox.be/novox/mesh-sdk/go/approval" "git.novox.be/novox/mesh-sdk/go/seatdef" @@ -179,3 +180,23 @@ func (c *consumers) chunked(key string) bool { } return false } + +// withinOf is how long a call of key waits for its answer: the `timeout` the verb's definition names, in whole seconds (design 47 +// H §760, H-A-43); zero, which the bus reads as the tool runner's own bound, for a key that is no seat's verb, a seat the +// definitions do not hold, a verb they do not name and a verb that names none. +func (c *consumers) withinOf(key string) time.Duration { + if c.definitions == nil || !strings.HasPrefix(key, "seat:") { + return 0 + } + name, _, _ := strings.Cut(key, "@") + seat, verb, ok := strings.Cut(strings.TrimPrefix(name, "seat:"), ".") + if !ok { + return 0 + } + for _, v := range c.definitions()[seat].Verbs { + if v.Name == verb && v.Timeout != nil { + return time.Duration(*v.Timeout) * time.Second + } + } + return 0 +} diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index 5d8d1d3..ef8cad2 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -876,7 +876,7 @@ type consumers struct { rereadMu sync.Mutex rereading *rereadFlight // askPlain and askChunked are how a bundle's ask is sent (the bus connection's AskAs and AskAsChunked); a test gives its own. - askPlain, askChunked func(module, key string, body any) (bus.Answered, error) + askPlain, askChunked func(module, key string, body any, within time.Duration) (bus.Answered, error) } type moduleEvents struct { @@ -1007,7 +1007,7 @@ func (b *moduleBus) Ask(params json.RawMessage) (json.RawMessage, error) { } ask := b.all.askPlain if ask == nil { - ask = b.all.conn.AskAs + ask = b.all.conn.AskAsWithin } chunks, err := b.all.chunkedAsked(asked.Key) if err != nil { @@ -1015,11 +1015,11 @@ func (b *moduleBus) Ask(params json.RawMessage) (json.RawMessage, error) { } if chunks { if ask = b.all.askChunked; ask == nil { - ask = b.all.conn.AskAsChunked + ask = b.all.conn.AskAsChunkedWithin } // the runner receives the chunks of the answer; the bundle receives none (design 47 G A-249) } - answered, err := ask(b.module, asked.Key, body) + answered, err := ask(b.module, asked.Key, body, b.all.withinOf(asked.Key)) if err != nil { return nil, err }