S5 ledger-watch: stdio.WatchLedger and the bundle's mesh/ledger answer
Builds: design 47 H2 H-A-706 (DMC-13), H-A-717 (DQ-50)
This commit is contained in:
@@ -2,7 +2,9 @@ package stdio
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -29,3 +31,36 @@ func BusState(method string, params any) (json.RawMessage, error) {
|
||||
}
|
||||
return c.ask(method, params, 60*time.Second)
|
||||
}
|
||||
|
||||
// WatchLedger hands handle every current state of the instances of this bundle's own ledger that instances names (an
|
||||
// instance, with `*` for one name and `>` for the rest; empty is every instance), and then every change, in the order it
|
||||
// came, and returns once the current states have all been handled. The watch lasts as long as the process. handle runs
|
||||
// beside the runtime (follower.go): its time never counts against the runtime's bound on the current states, and an error
|
||||
// it returns is said on standard error. A watch the runtime refuses is given up, said, and a change the runtime still hands
|
||||
// to it is refused saying so. A change names the instance's expiry while it has ended (design 47 H2 H-A-706 on DMC-13,
|
||||
// H-A-717 DQ-50).
|
||||
func WatchLedger(ledger, instances string, handle func(buscalls.InstanceChange) error) error {
|
||||
if handle == nil {
|
||||
return errors.New("a watch needs a handler")
|
||||
}
|
||||
c, err := live()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := c.newID()
|
||||
f := newFollower("the watch "+id+" of the ledger "+ledger, handle, func(ch buscalls.InstanceChange) string { return ch.Instance })
|
||||
c.mu.Lock()
|
||||
c.ledgerWatches[id] = f
|
||||
c.mu.Unlock()
|
||||
if _, err := c.askAs(id, buscalls.LedgerWatch, buscalls.LedgerWatchRequest{Ledger: ledger, Instances: instances, Watch: id}, 60*time.Second); err != nil {
|
||||
c.mu.Lock()
|
||||
delete(c.ledgerWatches, id)
|
||||
c.gaveUp(id, fmt.Sprintf("the ledger %s at %s: %v", ledger, time.Now().Format(time.RFC3339), err))
|
||||
c.mu.Unlock()
|
||||
sayf("the watch %s of the ledger %s is given up: %v", id, ledger, err)
|
||||
return err
|
||||
}
|
||||
// The runtime hands every current state over before it answers the watch: each is in the follower by now.
|
||||
<-f.handled()
|
||||
return nil
|
||||
}
|
||||
|
||||
+19
-1
@@ -20,6 +20,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -83,6 +84,8 @@ type channel struct {
|
||||
watches map[string]*follower[StateChange]
|
||||
// provisioned are this bundle's watches of its values, by the id each was asked with (provision.go).
|
||||
provisioned map[string]*follower[ProvisionChange]
|
||||
// ledgerWatches are this bundle's watches of its ledgers, by the id each was asked with (busstate.go).
|
||||
ledgerWatches map[string]*follower[buscalls.InstanceChange]
|
||||
// givenUp are the watches this bundle gave up, by id, each with why (follower.go).
|
||||
givenUp map[string]string
|
||||
// logWatches are this bundle's watches of a log, by the id each was asked with (log.go).
|
||||
@@ -221,7 +224,7 @@ func ServeOn(name string, tools []Tool, in io.Reader, out io.Writer) error {
|
||||
byName[t.Name] = t
|
||||
}
|
||||
c := &channel{enc: json.NewEncoder(out), waiting: map[string]chan message{}, watches: map[string]*follower[StateChange]{},
|
||||
provisioned: map[string]*follower[ProvisionChange]{}, logWatches: map[string]func(LogEntry) error{},
|
||||
provisioned: map[string]*follower[ProvisionChange]{}, ledgerWatches: map[string]*follower[buscalls.InstanceChange]{}, logWatches: map[string]func(LogEntry) error{},
|
||||
takers: map[string]func(Work) error{}, provers: map[string]func(string, json.RawMessage) (any, error){}}
|
||||
currentMu.Lock()
|
||||
current = c
|
||||
@@ -367,6 +370,21 @@ func ServeOn(name string, tools []Tool, in io.Reader, out io.Writer) error {
|
||||
}
|
||||
f.take(*p.Change)
|
||||
go c.say(map[string]any{"id": id, "result": map[string]any{}})
|
||||
case buscalls.LedgerWatched:
|
||||
// One change to an instance of a ledger this bundle watches (WatchLedger), named by the watch's id: taken into the
|
||||
// watch's follower in the order it came and answered at once, as a state's change is, so a slow handler never counts
|
||||
// against the runtime's bound on the current states (design 47 H2 H-A-706 on DMC-13).
|
||||
var change buscalls.InstanceChange
|
||||
_ = json.Unmarshal(m.Params, &change)
|
||||
c.mu.Lock()
|
||||
f := c.ledgerWatches[change.Watch]
|
||||
c.mu.Unlock()
|
||||
if f == nil || change.Instance == "" {
|
||||
c.say(map[string]any{"id": id, "error": map[string]any{"code": -32602, "message": c.noWatch(buscalls.LedgerWatched, change.Watch)}})
|
||||
continue
|
||||
}
|
||||
f.take(change)
|
||||
go c.say(map[string]any{"id": id, "result": map[string]any{}})
|
||||
case "mesh/log":
|
||||
// One entry of a log this bundle watches (log.go), named by the watch's id, answered once its handler has
|
||||
// taken it, in order: the runtime sends the next only after this answer.
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -495,3 +496,89 @@ func TestAskCallsOnceAndFaultsAtItsBound(t *testing.T) {
|
||||
}
|
||||
_ = inW.Close()
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-706 on DMC-13: a bundle's ledger watch is asked with `mesh/ledger.watch` under an id it keeps, each
|
||||
// `mesh/ledger` change naming that id is handed to its handler in the order it came (the current states first, before the
|
||||
// watch is answered), and a change naming no watch of it is refused as a state's is.
|
||||
func TestABundlesLedgerWatchIsHandedItsChangesInOrder(t *testing.T) {
|
||||
servesAlone(t)
|
||||
t.Setenv(ServedAs, "gate")
|
||||
inR, inW := io.Pipe()
|
||||
outR, outW := io.Pipe()
|
||||
seen := make(chan string, 8)
|
||||
watched := make(chan error, 1)
|
||||
go func() {
|
||||
_ = ServeOn("", []Tool{{Name: "start", Run: func(map[string]any) (any, error) {
|
||||
go func() {
|
||||
watched <- WatchLedger("records", "record/>", func(c buscalls.InstanceChange) error {
|
||||
seen <- c.Instance
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
return "started", nil
|
||||
}}}, inR, outW)
|
||||
}()
|
||||
lines := bufio.NewScanner(outR)
|
||||
next := func() map[string]any {
|
||||
done := make(chan map[string]any, 1)
|
||||
go func() {
|
||||
if lines.Scan() {
|
||||
var m map[string]any
|
||||
_ = json.Unmarshal(lines.Bytes(), &m)
|
||||
done <- m
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case m := <-done:
|
||||
return m
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("the bundle said nothing")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) }
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": "start"}})
|
||||
got := map[string]map[string]any{}
|
||||
for len(got) < 2 {
|
||||
m := next()
|
||||
if m["method"] != nil {
|
||||
got[m["method"].(string)] = m
|
||||
} else {
|
||||
got["answer"] = m
|
||||
}
|
||||
}
|
||||
watch := got[buscalls.LedgerWatch]
|
||||
if p := watch["params"].(map[string]any); p["ledger"] != "records" || p["instances"] != "record/>" || p["watch"] != watch["id"] {
|
||||
t.Fatalf("asked to watch %v", p)
|
||||
}
|
||||
for i, instance := range []string{"record/a", "record/b"} {
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": 50 + i, "method": buscalls.LedgerWatched, "params": map[string]any{
|
||||
"watch": watch["id"], "instance": instance, "state": map[string]any{}, "sequence": 1, "current": true}})
|
||||
if r := next(); r["id"] != float64(50+i) || r["error"] != nil {
|
||||
t.Fatalf("a current state was answered %v", r)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case err := <-watched:
|
||||
t.Fatalf("the watch returned before it was answered: %v", err)
|
||||
default:
|
||||
}
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": watch["id"], "result": map[string]any{}})
|
||||
if err := <-watched; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": 60, "method": buscalls.LedgerWatched, "params": map[string]any{
|
||||
"watch": watch["id"], "instance": "record/c", "state": map[string]any{}, "sequence": 2}})
|
||||
if r := next(); r["id"] != float64(60) || r["error"] != nil {
|
||||
t.Fatalf("a change was answered %v", r)
|
||||
}
|
||||
if order := []string{<-seen, <-seen, <-seen}; strings.Join(order, " ") != "record/a record/b record/c" {
|
||||
t.Fatalf("handled in the order %v", order)
|
||||
}
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": 61, "method": buscalls.LedgerWatched, "params": map[string]any{
|
||||
"watch": "bundle-999", "instance": "record/d", "state": map[string]any{}}})
|
||||
if r := next(); r["id"] != float64(61) || r["error"] == nil {
|
||||
t.Fatalf("a change naming no watch of it was answered %v", r)
|
||||
}
|
||||
_ = inW.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user