S14 kept chunked call: CallChunksKept and CallFileKept pace the ask and carry its id on every chunk

The assigner's compose-candidate ask is chunked, and the one-shot CallFile ended at the
first no responders; the kept call's form now makes the chunked call too.

Builds: design 47 H2 §775, H-A-405 (1), H-A-351
This commit is contained in:
2026-11-22 00:55:25 +01:00
parent c99295c21d
commit 3f8f35a87a
2 changed files with 143 additions and 16 deletions
+63
View File
@@ -1,8 +1,11 @@
package process
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"strings"
"testing"
@@ -12,6 +15,7 @@ import (
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/busretry"
)
@@ -179,6 +183,65 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) {
t.Errorf("the holder read the ask %q, want %q, and none once its handler ended", read, ask)
}
}
// Design 47 H2 §775, H-A-405: a kept chunked call (CallChunksKept, CallFileKept) asks its verb through the kept-call
// form, carries its one ask id on every chunk's acknowledgement, and past its bound ends with the kept call's words.
send := buscalls.SeatChunks{Seat: "node-build-go", Verb: "output", Call: "c1"}
for _, row := range []struct {
name string
answer []error // per ask; the last repeats
bound time.Duration
asks int
chunks int64
unasked bool // ends unanswered past its bound
}{
{"no responders twice, then answered with two chunks", []error{nats.ErrNoResponders, nats.ErrNoResponders, nil}, 0, 3, 2, false},
{"answered at once with three chunks", []error{nil}, 0, 1, 3, false},
{"no responders past its bound", []error{nats.ErrNoResponders}, time.Nanosecond, 2, 0, true},
} {
asks := 0
request := func(string, []byte) (json.RawMessage, error) {
e := row.answer[min(asks, len(row.answer)-1)]
asks++
return json.RawMessage(fmt.Sprintf(`{"chunks":%d}`, row.chunks)), e
}
chunks := make(chan *nats.Msg, row.chunks)
var want bytes.Buffer
for n := int64(1); n <= row.chunks; n++ {
data := []byte(fmt.Sprintf("part %d;", n))
want.Write(data)
body, _ := json.Marshal(buscalls.SeatChunk{N: n, Of: row.chunks, Data: data})
chunks <- &nats.Msg{Subject: send.Chunk(n), Data: body}
}
var acks []string
ack := func(m *nats.Msg) error {
acks = append(acks, m.Subject+" "+m.Header.Get(stdio.AskHeader))
return nil
}
ask := KeptAsk{ID: "ask-7", Bound: row.bound}
asking := func() (json.RawMessage, error) {
return callKeptWith(keptOf("seat:node-build-go.output@ace", ask, nil), request, "called", "fallback", nil)
}
var got bytes.Buffer
_, err := receiveChunks(context.Background(), "seat:node-build-go.output@ace", send, chunks, ask.ID, asking, nil, ack, &got)
var unanswered *busretry.Unanswered
if row.unasked != errors.As(err, &unanswered) || asks != row.asks || (!row.unasked && err != nil) {
t.Errorf("%s: asked %d times (want %d), %v", row.name, asks, row.asks, err)
}
if row.unasked && (unanswered.Ask != "ask-7" || !strings.Contains(err.Error(), "node-build-go.output (ask ask-7) unanswered within its bound")) {
t.Errorf("%s: ended with %v, not the kept call's words", row.name, err)
}
if got.String() != want.String() || int64(len(acks)) != row.chunks {
t.Errorf("%s: received %q and acknowledged %d chunks, want %q and %d", row.name, got.String(), len(acks), want.String(), row.chunks)
}
for n, a := range acks {
if a != send.Ack(int64(n+1))+" ask-7" {
t.Errorf("%s: acknowledgement %d is %q, carrying not the ask's id in its header", row.name, n+1, a)
}
}
}
if _, err := (&Conn{}).CallChunksKept(context.Background(), KeptAsk{}, "seat:node-build-go.output", "c1", nil, nil, &bytes.Buffer{}); err == nil {
t.Error("a kept chunked call naming no ask id was made")
}
}
// A-107: AnswerSeat reads the caller from a call's last token (dots written `~`), "" for anything that is not a
+80 -16
View File
@@ -22,24 +22,77 @@ import (
// is taken), then receives the number of chunks the answer's member `chunks` names, in order, acknowledging each, into w.
// It answers the verb's answer. A chunk that does not come within buscalls.SeatAckWithin, or ctx ending, ends the receive.
func (c *Conn) CallChunks(ctx context.Context, key, call string, body any, accept func(answer json.RawMessage) error, w io.Writer) (json.RawMessage, error) {
a, err := calladdress.Parse(key)
return c.callChunks(ctx, key, call, "", func() (json.RawMessage, error) { return c.Call(key, body) }, accept, w)
}
// CallChunksKept is CallChunks made as a kept call (design 47 H2 §775; H-A-405): the verb is asked through the kept-call
// form (busretry.DoPaced: called again at the kept-call pace on a transport fault, ADR 0664 §1's classes, the call's own
// deadline and the kind away; one journal line per seat at the first fault, then at most once a minute), carrying ask's id
// in the header stdio.AskHeader on the call and on every chunk's acknowledgement, and stopping at ask's bound with the
// words CallKeptAsk gives (a *busretry.Unanswered naming every call). A kept chunked call carries an ask id: one naming
// none is refused before anything is published.
func (c *Conn) CallChunksKept(ctx context.Context, ask KeptAsk, key, call string, body any, accept func(answer json.RawMessage) error,
w io.Writer) (json.RawMessage, error) {
if ask.ID == "" {
return nil, fmt.Errorf("the kept call of %s names no ask id", key)
}
called, fallback, err := callSubjectsOn(key, "", c.callerToken())
if err != nil {
return nil, err
}
if a.Seat == "" {
return nil, fmt.Errorf("%q names no seat verb, so it sends no chunks", key)
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
if call == "" {
return nil, fmt.Errorf("the call of %s names no call token for its chunks", key)
asking := func() (json.RawMessage, error) {
return callKeptWith(keptOf(key, ask, nil), c.requestAsking(ask.ID), called, fallback, data)
}
return c.callChunks(ctx, key, call, ask.ID, asking, accept, w)
}
// callChunks hears the chunks of call before it asks through asking, then receives them (receiveChunks), acknowledging
// each with ask's id in stdio.AskHeader when it carries one.
func (c *Conn) callChunks(ctx context.Context, key, call, ask string, asking func() (json.RawMessage, error),
accept func(answer json.RawMessage) error, w io.Writer) (json.RawMessage, error) {
send, err := chunksOf(key, call)
if err != nil {
return nil, err
}
send := buscalls.SeatChunks{Seat: a.Seat, Verb: a.Verb, Call: call}
chunks := make(chan *nats.Msg, 4)
sub, err := c.nc.ChanSubscribe(send.Chunks(), chunks)
if err != nil {
return nil, fmt.Errorf("the chunks of %s cannot be heard: %w", key, err)
}
defer func() { _ = sub.Unsubscribe() }()
answer, err := c.Call(key, body)
ack := func(m *nats.Msg) error {
_, err := c.nc.RequestMsg(m, buscalls.SeatAckWithin)
return err
}
return receiveChunks(ctx, key, send, chunks, ask, asking, accept, ack, w)
}
// chunksOf is the chunks of call of the seat verb key names; a key naming no seat verb, or no call, is refused.
func chunksOf(key, call string) (buscalls.SeatChunks, error) {
a, err := calladdress.Parse(key)
if err != nil {
return buscalls.SeatChunks{}, err
}
if a.Seat == "" {
return buscalls.SeatChunks{}, fmt.Errorf("%q names no seat verb, so it sends no chunks", key)
}
if call == "" {
return buscalls.SeatChunks{}, fmt.Errorf("the call of %s names no call token for its chunks", key)
}
return buscalls.SeatChunks{Seat: a.Seat, Verb: a.Verb, Call: call}, nil
}
// receiveChunks asks through asking, hands its answer to accept, then receives the number of chunks the answer's member
// `chunks` names from chunks, in order, acknowledging each through ack with a message carrying ask's id in
// stdio.AskHeader (none when empty), into w.
func receiveChunks(ctx context.Context, key string, send buscalls.SeatChunks, chunks <-chan *nats.Msg, ask string,
asking func() (json.RawMessage, error), accept func(answer json.RawMessage) error, ack func(*nats.Msg) error,
w io.Writer) (json.RawMessage, error) {
answer, err := asking()
if err != nil {
return nil, err
}
@@ -66,11 +119,8 @@ func (c *Conn) CallChunks(ctx context.Context, key, call string, body any, accep
return "", nil, ctx.Err()
}
}
ack := func(subject string) error {
_, err := c.nc.Request(subject, []byte("{}"), buscalls.SeatAckWithin)
return err
}
if _, err := send.Receive(count.Chunks, next, ack, w); err != nil {
acked := func(subject string) error { return ack(askMsg(subject, []byte("{}"), ask)) }
if _, err := send.Receive(count.Chunks, next, acked, w); err != nil {
return answer, fmt.Errorf("the answer of %s: %w", key, err)
}
return answer, nil
@@ -80,18 +130,32 @@ func (c *Conn) CallChunks(ctx context.Context, key, call string, body any, accep
// G A-249, A-253): the verb's answer with the whole file added as its member `file` (base64), none when no chunk was sent. The
// call token is body's member `call`.
func (c *Conn) CallFile(key string, body any) (json.RawMessage, error) {
call := callOf(body)
var file bytes.Buffer
answer, err := c.CallChunks(context.Background(), key, call, body, nil, &file)
answer, err := c.CallChunks(context.Background(), key, callOf(body), body, nil, &file)
if err != nil {
return nil, err
}
return withFile(key, answer, file.Bytes())
}
// CallFileKept is CallFile made as a kept call, as CallChunksKept is CallChunks's (design 47 H2 §775; H-A-405).
func (c *Conn) CallFileKept(ask KeptAsk, key string, body any) (json.RawMessage, error) {
var file bytes.Buffer
answer, err := c.CallChunksKept(context.Background(), ask, key, callOf(body), body, nil, &file)
if err != nil {
return nil, err
}
return withFile(key, answer, file.Bytes())
}
// withFile is a chunked verb's answer with file added as its member `file` (base64), none when file is empty.
func withFile(key string, answer json.RawMessage, file []byte) (json.RawMessage, error) {
var whole map[string]any
if err := json.Unmarshal(answer, &whole); err != nil {
return nil, fmt.Errorf("%s answered what is no object: %w", key, err)
}
if file.Len() > 0 {
whole["file"] = base64.StdEncoding.EncodeToString(file.Bytes())
if len(file) > 0 {
whole["file"] = base64.StdEncoding.EncodeToString(file)
}
return json.Marshal(whole)
}