diff --git a/go/busretry/busretry.go b/go/busretry/busretry.go index 5a053704..2c9a1693 100644 --- a/go/busretry/busretry.go +++ b/go/busretry/busretry.go @@ -63,9 +63,32 @@ const ( // between two lives (a restart takes 20 to 50 s) is met within one minute without a call a second. var DefaultPace = Pace{First: time.Second, Max: 30 * time.Second} -// KeptPace is the pace of every kept call this process makes: DefaultPace until the process reads its settings. +// KeptPace is the pace of every kept call this process makes: DefaultPace until the process reads its settings +// (ReadPace, at process.Connect and at a bundle's stdio.Serve). var KeptPace = DefaultPace +// ReadPace sets KeptPace from the module's settings PaceFirstSetting and PaceMaxSetting, read through read (the one +// settings reader, stdio.SettingDuration, which answers its default for a setting not composed), so a module's +// declared pace paces its kept calls (§775). A setting that is no duration, or a most below the first, is an error +// naming it, and KeptPace stays. +func ReadPace(read func(key string, def time.Duration) (time.Duration, error)) error { + first, err := read(PaceFirstSetting, KeptPace.First) + if err != nil { + return err + } + most, err := read(PaceMaxSetting, KeptPace.Max) + if err != nil { + return err + } + if most < first { + return fmt.Errorf("the setting %s (%s) is below %s (%s)", PaceMaxSetting, most, PaceFirstSetting, first) + } + if read := (Pace{First: first, Max: most}); read != KeptPace { + KeptPace = read + } + return nil +} + // The clock and the sleep the paced form waits on; a test of this package replaces both, so nothing sleeps. var ( now = time.Now diff --git a/go/busretry/busretry_test.go b/go/busretry/busretry_test.go index 215b5848..423f5f9e 100644 --- a/go/busretry/busretry_test.go +++ b/go/busretry/busretry_test.go @@ -185,6 +185,34 @@ func subAKeptCallIsPaced(t *testing.T) { t.Fatalf("an away answer with no words reads %+v", a) } }) + t.Run("the pace is the module's settings", func(t *testing.T) { + was := KeptPace + t.Cleanup(func() { KeptPace = was }) + for _, c := range []struct { + set map[string]time.Duration + bad bool + want Pace + }{ + {set: nil, want: was}, + {set: map[string]time.Duration{PaceFirstSetting: 2 * time.Second, PaceMaxSetting: time.Minute}, want: Pace{First: 2 * time.Second, Max: time.Minute}}, + {set: map[string]time.Duration{PaceFirstSetting: time.Minute, PaceMaxSetting: time.Second}, bad: true, want: was}, + {set: map[string]time.Duration{PaceFirstSetting: -1}, bad: true, want: was}, + } { + KeptPace = was + err := ReadPace(func(key string, def time.Duration) (time.Duration, error) { + if d, ok := c.set[key]; ok { + if d < 0 { + return 0, fmt.Errorf("the setting %s is no duration", key) + } + return d, nil + } + return def, nil + }) + if (err != nil) != c.bad || KeptPace != c.want { + t.Errorf("settings %v: pace %+v, %v", c.set, KeptPace, err) + } + } + }) t.Run("one line per seat a minute", func(t *testing.T) { fakeClock(t) logs := 0 diff --git a/go/caller_test.go b/go/caller_test.go index 81ec0f1c..0a724cac 100644 --- a/go/caller_test.go +++ b/go/caller_test.go @@ -121,6 +121,10 @@ func TestAToolReadsTheCallerTheRuntimeNamed(t *testing.T) { if _, err := SettingDuration("kept-call-pace-first", time.Second); err == nil || !strings.Contains(err.Error(), "kept-call-pace-first") { t.Errorf("a value that is no duration read as %v", err) } + // §775: a bundle reads its kept-call pace from the same settings at its start, and refuses one that is no duration. + if err := ServeOn("b", nil, strings.NewReader(""), io.Discard); err == nil || !strings.Contains(err.Error(), "kept-call-pace-first") { + t.Errorf("a bundle served with a pace that is no duration: %v", err) + } if MetaCall != buscalls.MetaCall { t.Errorf("the call id's place is %q here and %q in buscalls", MetaCall, buscalls.MetaCall) } diff --git a/go/process/account_test.go b/go/process/account_test.go index 884b922f..0676b3d9 100644 --- a/go/process/account_test.go +++ b/go/process/account_test.go @@ -14,6 +14,9 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/busretry" ) // 1171/AC2: given an account file broker- in the controller's form {url, fingerprint, node, @@ -342,6 +345,12 @@ func subTheOwnBuildIsTheFingerprintTheProcessWasStartedWith(t *testing.T) { t.Errorf("%s: the process's own build is not said at its start: %+v", name, said) } } + // Design 47 H2 §775, §781: Connect reads the module's kept-call pace from its settings, and refuses one that is no + // duration, naming it. + t.Setenv(stdio.SettingWord(busretry.PaceFirstSetting), "soon") + if _, err := Connect(Account{URL: "nats://127.0.0.1:1", User: "n1.m"}); err == nil || !strings.Contains(err.Error(), "kept-call-pace-first") { + t.Errorf("a pace that is no duration connected: %v", err) + } } // TestReadAccountYieldsEveryFieldAndToleratesClaims runs the tests of this package that were merged to keep the module within its test budget (design 47 G §419), each as a subtest. diff --git a/go/process/conn.go b/go/process/conn.go index 2b6ecaf5..bb4c0597 100644 --- a/go/process/conn.go +++ b/go/process/conn.go @@ -12,6 +12,9 @@ import ( "sync" "github.com/nats-io/nats.go" + + stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/busretry" ) // Conn is one process's own connection to the bus (novox/hq ADR 0436 clause 4): dialled with the account @@ -54,6 +57,9 @@ var refusedPublish = regexp.MustCompile(`(?i)Publish to "(\S+)"`) // at once. func Connect(account Account, opts ...nats.Option) (*Conn, error) { OwnBuild() + if err := busretry.ReadPace(stdio.SettingDuration); err != nil { + return nil, fmt.Errorf("the kept-call pace of the process account %s: %w", account.User, err) + } c := &Conn{account: account, pending: map[string]map[uint64]context.CancelCauseFunc{}} nc, err := nats.Connect(account.URL, c.options(opts)...) if err != nil { diff --git a/go/stdio.go b/go/stdio.go index 7e6e71a3..4fb2ee17 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -26,6 +26,7 @@ import ( "sync" "time" + "git.novox.be/novox/mesh-sdk/go/busretry" "git.novox.be/novox/mesh-sdk/go/calladdress" ) @@ -191,6 +192,9 @@ func ServeOn(name string, tools []Tool, in io.Reader, out io.Writer) error { if name == "" { name = "bundle" } + if err := busretry.ReadPace(SettingDuration); err != nil { + return fmt.Errorf("the kept-call pace of %s: %w", name, err) + } byName := map[string]Tool{} for _, t := range tools { byName[t.Name] = t