The review found §794's two bounds fixed in code: the builder's 100 ms for saying a slow event's handling, and builder_health's five minutes for naming a consumer behind. Both become settings read at run time, slow-event (100ms) and events-behind-bound (5m), declared in the manifest's config with those defaults; a value that is no duration refuses the builder's start, and makes the health say it. Builds: design 47 H2 §794, H-A-461 (2)
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// slowEventSetting is the setting slow-event (design 47 H2 §794; H-A-461 (2), P11): how long the handling of one heard event
|
|
// may take before the builder says it in its journal, the line naming the event's subject, the part that took it and how long
|
|
// it took; defaultSlowEvent when none is set.
|
|
const (
|
|
slowEventSetting = "slow-event"
|
|
defaultSlowEvent = 100 * time.Millisecond
|
|
)
|
|
|
|
// slowEvent reads the setting slow-event.
|
|
func slowEvent() (time.Duration, error) {
|
|
d, err := stdio.SettingDuration(slowEventSetting, defaultSlowEvent)
|
|
if err != nil {
|
|
return 0, errors.New("the builder's " + err.Error())
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// eventTaker is one part of the builder that takes events off its durable consumer: take answers whether the message was its.
|
|
type eventTaker struct {
|
|
part string
|
|
take func(jetstream.Msg) bool
|
|
}
|
|
|
|
// takeEvent hands m to each taker in turn until one takes it, else to rest, and says the handling when it passed slow, the
|
|
// setting slow-event (design 47 H2 §794). now is the clock, a test's when set.
|
|
func takeEvent(m jetstream.Msg, takers []eventTaker, rest eventTaker, slow time.Duration, now func() time.Time, logf func(string, ...any)) {
|
|
if now == nil {
|
|
now = time.Now
|
|
}
|
|
start := now()
|
|
part := rest.part
|
|
for _, t := range takers {
|
|
if t.take(m) {
|
|
part = t.part
|
|
break
|
|
}
|
|
}
|
|
if part == rest.part {
|
|
rest.take(m)
|
|
}
|
|
if took := now().Sub(start); took > slow {
|
|
logf("the event %s took %s to handle, over %s: %s took it", m.Subject(), took.Round(time.Millisecond), slow, part)
|
|
}
|
|
}
|