Merge branch 'feat/47-G-fix11-b5g3' into feat/47-G-join11
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
)
|
||||
|
||||
// FileField is the member of a chunked verb's answer that carries the file received: its whole bytes, base64 (design 47 G
|
||||
// A-249).
|
||||
const FileField = "file"
|
||||
|
||||
// AskAsChunked is AskAs of a seat's verb whose definition says `chunks` (design 47 G §681, A-237, A-249): it chooses the call
|
||||
// token (the body's `call`), hears the chunk subjects of that call before it asks, receives the chunks the verb's answer
|
||||
// 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) {
|
||||
seat, verb, ok := strings.Cut(strings.TrimPrefix(key, "seat:"), ".")
|
||||
if !strings.HasPrefix(key, "seat:") || !ok || strings.Contains(verb, "@") {
|
||||
return Answered{}, fmt.Errorf("%q is no seat:<seat>.<verb> of no one machine, which a chunked answer needs", key)
|
||||
}
|
||||
args := map[string]any{}
|
||||
if raw, err := json.Marshal(body); err != nil {
|
||||
return Answered{}, err
|
||||
} else if string(raw) != "null" {
|
||||
if err := json.Unmarshal(raw, &args); err != nil {
|
||||
return Answered{}, fmt.Errorf("the arguments of %s are not an object: %w", key, err)
|
||||
}
|
||||
}
|
||||
var bits [16]byte
|
||||
if _, err := rand.Read(bits[:]); err != nil {
|
||||
return Answered{}, err
|
||||
}
|
||||
send := buscalls.SeatChunks{Seat: seat, Verb: verb, Call: hex.EncodeToString(bits[:])}
|
||||
args["call"] = send.Call
|
||||
chunks := make(chan *nats.Msg, 4)
|
||||
sub, err := c.nc.ChanSubscribe(send.Chunks(), chunks)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return answered, err
|
||||
}
|
||||
fields := map[string]json.RawMessage{}
|
||||
if err := json.Unmarshal(answered.Result, &fields); err != nil {
|
||||
return answered, fmt.Errorf("%s answered what is no object: %w", key, err)
|
||||
}
|
||||
var count int64
|
||||
if raw, has := fields["chunks"]; has {
|
||||
if err := json.Unmarshal(raw, &count); err != nil {
|
||||
return answered, fmt.Errorf("%s answered chunks that is no count: %w", key, err)
|
||||
}
|
||||
}
|
||||
var file bytes.Buffer
|
||||
if count > 0 {
|
||||
next := func() (string, []byte, error) {
|
||||
select {
|
||||
case m := <-chunks:
|
||||
return m.Subject, m.Data, nil
|
||||
case <-time.After(buscalls.SeatAckWithin):
|
||||
return "", nil, buscalls.ErrSeatChunkMissed
|
||||
}
|
||||
}
|
||||
ack := func(subject string) error {
|
||||
_, err := c.nc.Request(subject, []byte("{}"), buscalls.SeatAckWithin)
|
||||
return err
|
||||
}
|
||||
if _, err := send.Receive(count, next, ack, &file); err != nil {
|
||||
return answered, fmt.Errorf("the chunks of %s: %w", key, err)
|
||||
}
|
||||
}
|
||||
if _, taken := fields[FileField]; taken {
|
||||
return answered, errors.New(key + " answers a member named " + FileField + " itself")
|
||||
}
|
||||
enc, _ := json.Marshal(base64.StdEncoding.EncodeToString(file.Bytes()))
|
||||
fields[FileField] = enc
|
||||
out, err := json.Marshal(fields)
|
||||
if err != nil {
|
||||
return answered, err
|
||||
}
|
||||
answered.Result = out
|
||||
return answered, nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package bus_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
|
||||
)
|
||||
|
||||
// Design 47 G A-249, §681: the runner asks a chunked verb with a call token of its own, receives the chunks the answer counts
|
||||
// (each acknowledged before the next) and answers the verb's own answer with the whole file, so the asker receives none.
|
||||
func TestAChunkedSeatAnswerIsReceivedByTheRunnerAndHandedWhole(t *testing.T) {
|
||||
url := mt.URL(t)
|
||||
holder := dial(t, url, "mesh-delivery-indexer", "anchor", "")
|
||||
asker := dial(t, url, "mesh-build-go", "anchor", "")
|
||||
raw, err := nats.Connect(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(raw.Close)
|
||||
file := make([]byte, buscalls.SeatChunkBytes*2+5)
|
||||
for i := range file {
|
||||
file[i] = byte(i)
|
||||
}
|
||||
// the holder's process: answers the count, then sends each chunk and waits for its acknowledgement
|
||||
stop, err := holder.Handle("seat:mesh-catalog.input", func(body json.RawMessage) (any, error) {
|
||||
var in struct{ Call, Digest string }
|
||||
if err := json.Unmarshal(body, &in); err != nil || in.Call == "" {
|
||||
t.Errorf("the verb was asked with no call token: %s (%v)", body, err)
|
||||
return nil, err
|
||||
}
|
||||
send := buscalls.SeatChunks{Seat: "mesh-catalog", Verb: "input", Call: in.Call}
|
||||
go func() {
|
||||
for n := int64(1); n <= 3; n++ {
|
||||
end := min(int64(len(file)), n*buscalls.SeatChunkBytes)
|
||||
data, _ := json.Marshal(buscalls.SeatChunk{N: n, Of: 3, Data: file[(n-1)*buscalls.SeatChunkBytes : end]})
|
||||
ackSub, _ := raw.SubscribeSync(send.Ack(n))
|
||||
_ = raw.Flush()
|
||||
if err := raw.Publish(send.Chunk(n), data); err != nil {
|
||||
return
|
||||
}
|
||||
m, err := ackSub.NextMsg(buscalls.SeatAckWithin)
|
||||
if err != nil {
|
||||
t.Errorf("chunk %d not acknowledged: %v", n, err)
|
||||
return
|
||||
}
|
||||
_ = m.Respond([]byte("{}"))
|
||||
}
|
||||
}()
|
||||
return map[string]any{"digest": in.Digest, "chunks": 3}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer stop()
|
||||
got, err := asker.AskAsChunked("mesh-build-go", "seat:mesh-catalog.input", map[string]any{"digest": "d1"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ans struct {
|
||||
Digest string `json:"digest"`
|
||||
Chunks int `json:"chunks"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
if err := json.Unmarshal(got.Result, &ans); err != nil || ans.Digest != "d1" || ans.Chunks != 3 {
|
||||
t.Fatalf("the answer %s: %v", got.Result, err)
|
||||
}
|
||||
if body, err := base64.StdEncoding.DecodeString(ans.File); err != nil || string(body) != string(file) {
|
||||
t.Errorf("the file handed is %d bytes, want the %d sent: %v", len(body), len(file), err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user