S3 away: read a holder's answer of the kind away as a transport fault
A holder answers error-kind away, with part and words, when a part it depends on is away; the kept call reads it from the answer and calls again at the pace. Go: busretry.ErrorKindAway, AwayAnswer, AwayIn, Away; TypeScript: ERROR_KIND_AWAY, awayAnswer, awayIn. Builds: design 47 H2 §776, §775; H-A-349 DQ-15, H-A-350 DMC-2, H-A-354
This commit is contained in:
+44
-2
@@ -15,6 +15,7 @@ package busretry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
@@ -122,8 +123,49 @@ func (u *Unanswered) Error() string {
|
||||
func (u *Unanswered) Unwrap() error { return u.Last }
|
||||
|
||||
// Fault says err is a transport fault of a kept call (§775): a request that reached no one, a call's own deadline
|
||||
// passing; a callee's refusal never is.
|
||||
func Fault(err error) bool { return TimedOut(err) || Undelivered(err) }
|
||||
// passing, or a holder's answer of the kind away (§776); a callee's refusal never is.
|
||||
func Fault(err error) bool {
|
||||
var away *Away
|
||||
return TimedOut(err) || Undelivered(err) || errors.As(err, &away)
|
||||
}
|
||||
|
||||
// ErrorKindAway is the seat error kind a holder answers when a part it depends on is away (design 47 H2 §776): its
|
||||
// answer's `error-kind`, beside `part` (what did not answer) and `words`. A caller reads it as a transport fault.
|
||||
const ErrorKindAway = "away"
|
||||
|
||||
// Away is a holder's answer of the kind away, read as an error: the part that did not answer and the holder's words.
|
||||
type Away struct {
|
||||
Part, Words string
|
||||
}
|
||||
|
||||
func (a *Away) Error() string {
|
||||
return fmt.Sprintf("away: %s does not answer: %s", a.Part, a.Words)
|
||||
}
|
||||
|
||||
// AwayAnswer is the seat error answer of the kind away a holder gives (§776): `error` and `error-kind` as each seat's
|
||||
// error answer has them, with `part` and `words`.
|
||||
func AwayAnswer(part, words string) map[string]string {
|
||||
return map[string]string{"error": (&Away{Part: part, Words: words}).Error(), "error-kind": ErrorKindAway,
|
||||
"part": part, "words": words}
|
||||
}
|
||||
|
||||
// AwayIn is the *Away a verb's answer says when its `error-kind` is away, else nil: an answer that is no object, or
|
||||
// of another kind, is the caller's to read.
|
||||
func AwayIn(answer json.RawMessage) *Away {
|
||||
var a struct {
|
||||
Error string `json:"error"`
|
||||
Kind string `json:"error-kind"`
|
||||
Part string `json:"part"`
|
||||
Words string `json:"words"`
|
||||
}
|
||||
if json.Unmarshal(answer, &a) != nil || a.Kind != ErrorKindAway {
|
||||
return nil
|
||||
}
|
||||
if a.Words == "" {
|
||||
a.Words = a.Error
|
||||
}
|
||||
return &Away{Part: a.Part, Words: a.Words}
|
||||
}
|
||||
|
||||
// DoPaced makes a kept call through try (design 47 H2 §775): on a transport fault (Fault) it calls again at k's pace,
|
||||
// each wait jittered, until the caller's bound, writing one journal line per seat at the first fault and then at most
|
||||
|
||||
@@ -2,6 +2,7 @@ package busretry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -170,6 +171,20 @@ func subAKeptCallIsPaced(t *testing.T) {
|
||||
t.Fatalf("%d tries, %v", tries, err)
|
||||
}
|
||||
})
|
||||
t.Run("away is a transport fault; its answer reads back", func(t *testing.T) {
|
||||
raw, _ := json.Marshal(AwayAnswer("the forge's store", "401 and the version read failed"))
|
||||
a := AwayIn(raw)
|
||||
if a == nil || a.Part != "the forge's store" || a.Words != "401 and the version read failed" || !Fault(a) ||
|
||||
!strings.Contains(string(raw), `"error-kind":"away"`) {
|
||||
t.Fatalf("%s read as %+v", raw, a)
|
||||
}
|
||||
if AwayIn([]byte(`{"error":"no","error-kind":"refused"}`)) != nil || AwayIn([]byte(`[1]`)) != nil || Fault(&Answered{Words: "away"}) {
|
||||
t.Fatal("a refusal reads as away")
|
||||
}
|
||||
if a := AwayIn([]byte(`{"error":"the store is down","error-kind":"away"}`)); a == nil || a.Words != "the store is down" {
|
||||
t.Fatalf("an away answer with no words reads %+v", a)
|
||||
}
|
||||
})
|
||||
t.Run("one line per seat a minute", func(t *testing.T) {
|
||||
fakeClock(t)
|
||||
logs := 0
|
||||
|
||||
+11
-1
@@ -123,7 +123,17 @@ func keptOf(key string, ask KeptAsk, open *busretry.Open) busretry.Kept {
|
||||
// callKeptWith is callWith for a kept call: busretry.DoPaced with no blackout probe.
|
||||
func callKeptWith(k busretry.Kept, request func(string, []byte) (json.RawMessage, error), called, fallback string,
|
||||
data []byte) (json.RawMessage, error) {
|
||||
return busretry.DoPaced(k, askBoth(request, called, fallback, data))
|
||||
ask := askBoth(request, called, fallback, data)
|
||||
return busretry.DoPaced(k, func() (json.RawMessage, error) {
|
||||
result, err := ask()
|
||||
if err == nil {
|
||||
// A holder's answer of the kind away is a transport fault (§776): called again at the pace.
|
||||
if away := busretry.AwayIn(result); away != nil {
|
||||
return result, away
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
})
|
||||
}
|
||||
|
||||
// askBoth asks called, then fallback when nothing answers it.
|
||||
|
||||
@@ -138,6 +138,27 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) {
|
||||
t.Errorf("%s: asked %d times (%v), want %d", row.name, asks, err, row.asks)
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §776: a holder's answer of the kind away is a transport fault, called again at the pace; a refusal
|
||||
// of another kind is the caller's answer at once.
|
||||
for _, row := range []struct {
|
||||
name string
|
||||
answers []string
|
||||
asks int
|
||||
}{
|
||||
{"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2},
|
||||
{"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1},
|
||||
} {
|
||||
asks := 0
|
||||
request := func(string, []byte) (json.RawMessage, error) {
|
||||
a := row.answers[min(asks, len(row.answers)-1)]
|
||||
asks++
|
||||
return json.RawMessage(a), nil
|
||||
}
|
||||
got, err := callKeptWith(busretry.Kept{Seat: "git", Verb: "merge-pull"}, request, "called", "fallback", nil)
|
||||
if asks != row.asks || err != nil || string(got) != row.answers[len(row.answers)-1] {
|
||||
t.Errorf("%s: asked %d times, %s, %v", row.name, asks, got, err)
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §775, H-A-351: a kept call carries its ask's id in the header, and a holder in a process reads it with
|
||||
// AskOf while its handler runs; a call carrying none reads empty.
|
||||
if m := askMsg("mesh.seat.mesh-builds.call.build", []byte(`{}`), "ask-9"); m.Header.Get(stdio.AskHeader) != "ask-9" {
|
||||
|
||||
Reference in New Issue
Block a user