S7 event-overrun: the hub times each handling, the finding event-overrun, ViewKinds names both
Builds: design 47 H2 H-A-683 (3), H-A-697 (1) (2) (3)
This commit is contained in:
+107
-3
@@ -18,6 +18,12 @@ import (
|
||||
const (
|
||||
EventsAtOnceSetting = "events-at-once"
|
||||
EventsWaitingSetting = "events-waiting"
|
||||
SlowEventSetting = "slow-event"
|
||||
EventOverrunSetting = "event-overrun"
|
||||
defaultSlowEvent = 100 * time.Millisecond
|
||||
defaultEventOverrun = 60 * time.Second
|
||||
leastEventOverrun = time.Second
|
||||
mostEventOverrun = time.Hour
|
||||
defaultEventsAtOnce = 8
|
||||
defaultEventsWaiting = 100
|
||||
mostEventsAtOnce = 64
|
||||
@@ -30,6 +36,13 @@ const (
|
||||
// (1)). One key's events are handled one at a time in the order heard, different keys side by side.
|
||||
type EventKey func(subject string, body []byte) string
|
||||
|
||||
// Handling is one handling of an event that runs past the setting event-overrun: its subject, its key and since when it ran
|
||||
// (design 47 H2 H-A-683 (3), H-A-697 (3)).
|
||||
type Handling struct {
|
||||
Subject, Key string
|
||||
Since time.Time
|
||||
}
|
||||
|
||||
// settingCount reads the setting key as a whole number from lo to hi, def when none is composed; one that is no such number
|
||||
// is an error naming the setting.
|
||||
func settingCount(key string, def, lo, hi int) (int, error) {
|
||||
@@ -59,11 +72,21 @@ type heldEvent struct {
|
||||
type eventHold struct {
|
||||
logf func(string, ...any)
|
||||
tick ticker
|
||||
// now and after are the clock and the timer of the timed handlings, a test's to replace: after calls f once d has passed and
|
||||
// answers what cancels it.
|
||||
now func() time.Time
|
||||
after func(d time.Duration, f func()) (cancel func())
|
||||
// overrun is told the handlings past the bound of event-overrun, oldest first, each time one passes it or ends.
|
||||
overrun func(past []Handling)
|
||||
|
||||
toldMu sync.Mutex // the hook of the overrun is told one at a time
|
||||
mu sync.Mutex
|
||||
room *sync.Cond // signalled when a message leaves
|
||||
atOnce int
|
||||
bound int
|
||||
slow time.Duration
|
||||
over time.Duration
|
||||
running map[*heldEvent]*timed
|
||||
queues map[string][]*heldEvent
|
||||
active map[string]bool
|
||||
waiting []string // keys with events waiting and no handling running, in the order heard
|
||||
@@ -73,7 +96,9 @@ type eventHold struct {
|
||||
}
|
||||
|
||||
func newEventHold(logf func(string, ...any), tick ticker) *eventHold {
|
||||
e := &eventHold{logf: logf, tick: tick, atOnce: defaultEventsAtOnce, bound: defaultEventsWaiting,
|
||||
e := &eventHold{logf: logf, tick: tick, atOnce: defaultEventsAtOnce, bound: defaultEventsWaiting, now: time.Now,
|
||||
slow: defaultSlowEvent, over: defaultEventOverrun, running: map[*heldEvent]*timed{},
|
||||
after: func(d time.Duration, f func()) func() { t := time.AfterFunc(d, f); return func() { t.Stop() } },
|
||||
queues: map[string][]*heldEvent{}, active: map[string]bool{}, ids: map[string]*heldEvent{}}
|
||||
e.room = sync.NewCond(&e.mu)
|
||||
return e
|
||||
@@ -89,8 +114,20 @@ func (e *eventHold) settle() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slow, err := stdio.SettingDuration(SlowEventSetting, defaultSlowEvent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
over, err := stdio.SettingDuration(EventOverrunSetting, defaultEventOverrun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if over < leastEventOverrun || over > mostEventOverrun {
|
||||
return fmt.Errorf("the setting %s (%s) holds %s, which is not from %s to %s", EventOverrunSetting, stdio.SettingWord(EventOverrunSetting),
|
||||
over, leastEventOverrun, mostEventOverrun)
|
||||
}
|
||||
e.mu.Lock()
|
||||
e.atOnce, e.bound, e.stopped = atOnce, bound, false
|
||||
e.atOnce, e.bound, e.slow, e.over, e.stopped = atOnce, bound, slow, over, false
|
||||
e.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
@@ -197,16 +234,83 @@ func (e *eventHold) run(key string) {
|
||||
}
|
||||
}
|
||||
|
||||
// handle runs a message's handler; a panic in it is said and the message is left unacknowledged, so the bus gives it again.
|
||||
// timed is one handling that runs: when it began, and whether it is past event-overrun.
|
||||
type timed struct {
|
||||
since time.Time
|
||||
past bool
|
||||
cancel func()
|
||||
}
|
||||
|
||||
// handle runs a message's handler, timed: past slow-event the journal says how long it took and which key's it was; while it
|
||||
// runs past event-overrun the hook of the overrun is told at once, and again when it ends (design 47 H2 H-A-683 (3), H-A-697
|
||||
// (3)). A panic in it is said and the message is left unacknowledged, so the bus gives it again.
|
||||
func (e *eventHold) handle(h *heldEvent) {
|
||||
e.mu.Lock()
|
||||
t := &timed{since: e.now()}
|
||||
over, slow := e.over, e.slow
|
||||
e.running[h] = t
|
||||
e.mu.Unlock()
|
||||
t.cancel = e.after(over, func() {
|
||||
e.mu.Lock()
|
||||
if _, ok := e.running[h]; !ok {
|
||||
e.mu.Unlock()
|
||||
return
|
||||
}
|
||||
t.past = true
|
||||
e.mu.Unlock()
|
||||
e.told()
|
||||
})
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
e.logf("events: the handler of %s failed: %v; the message is not acknowledged", h.subject, r)
|
||||
}
|
||||
t.cancel()
|
||||
e.mu.Lock()
|
||||
delete(e.running, h)
|
||||
took := e.now().Sub(t.since)
|
||||
wasPast := t.past
|
||||
e.mu.Unlock()
|
||||
if took > slow {
|
||||
e.logf("events: the event %s (key %s) took %s to handle, over %s", h.subject, displayKey(h.key), took.Round(time.Millisecond), slow)
|
||||
}
|
||||
if wasPast {
|
||||
e.told()
|
||||
}
|
||||
}()
|
||||
h.handle(h.msg)
|
||||
}
|
||||
|
||||
// pastLocked are the handlings past the bound, the one begun first first; the caller holds mu.
|
||||
func (e *eventHold) pastLocked() []Handling {
|
||||
var out []Handling
|
||||
for h, t := range e.running {
|
||||
if t.past {
|
||||
out = append(out, Handling{Subject: h.subject, Key: displayKey(h.key), Since: t.since})
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if !out[i].Since.Equal(out[j].Since) {
|
||||
return out[i].Since.Before(out[j].Since)
|
||||
}
|
||||
return out[i].Subject < out[j].Subject
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// told hands the handlings past the bound, as they are now, to the hook; one call at a time, each reading the handlings after the
|
||||
// one before it ended, so the hook's last word is the true one.
|
||||
func (e *eventHold) told() {
|
||||
if e.overrun == nil {
|
||||
return
|
||||
}
|
||||
e.toldMu.Lock()
|
||||
defer e.toldMu.Unlock()
|
||||
e.mu.Lock()
|
||||
past := e.pastLocked()
|
||||
e.mu.Unlock()
|
||||
e.overrun(past)
|
||||
}
|
||||
|
||||
// stop releases a part waiting for room.
|
||||
func (e *eventHold) stop() {
|
||||
e.mu.Lock()
|
||||
|
||||
@@ -29,6 +29,8 @@ type EventHub struct {
|
||||
bound []func(created time.Time)
|
||||
// unhandled are told each subject no handler took (OnUnhandled).
|
||||
unhandled []func(subject string)
|
||||
// overruns are told the handlings past event-overrun (OnOverrun).
|
||||
overruns []func(past []Handling)
|
||||
// kept are the module's durables on the kept seat events' streams it handles (StartKept).
|
||||
kept []jetstream.ConsumeContext
|
||||
// held is every message the hub took and has not seen end, across its durables (event_hold.go); feeds are the durables'
|
||||
@@ -51,6 +53,14 @@ func NewEventHub(logf func(string, ...any)) *EventHub {
|
||||
}
|
||||
h := &EventHub{logf: logf, tick: realTicker}
|
||||
h.held = newEventHold(logf, func(d time.Duration) (<-chan time.Time, func()) { return h.tick(d) })
|
||||
h.held.overrun = func(past []Handling) {
|
||||
h.mu.RLock()
|
||||
told := h.overruns
|
||||
h.mu.RUnlock()
|
||||
for _, f := range told {
|
||||
f(past)
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
@@ -103,6 +113,16 @@ func (h *EventHub) OnUnhandled(f func(subject string)) {
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// OnOverrun registers f, told the handlings of events that run past the setting event-overrun (1 second to 1 hour, 60 seconds by
|
||||
// default), the one begun first first, each time a handling passes that bound while it still runs and each time one that was
|
||||
// past it ends: an empty list says none is past it. The process reader wires it into the module's finding `event-overrun`
|
||||
// (design 47 H2 H-A-683 (3), H-A-697).
|
||||
func (h *EventHub) OnOverrun(f func(past []Handling)) {
|
||||
h.mu.Lock()
|
||||
h.overruns = append(h.overruns, f)
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// Patterns are the registered subject patterns, in order.
|
||||
func (h *EventHub) Patterns() []string {
|
||||
h.mu.RLock()
|
||||
|
||||
@@ -348,3 +348,82 @@ func TestTheHubHoldsEventsByKeyAndSaysThemInProgress(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-683 (3), H-A-697 (3): a handling past event-overrun tells the hook once while it runs, naming its subject, key
|
||||
// and since when, the oldest first, and once more when it ends with none past it; a handling past slow-event is said in the
|
||||
// journal with its key and how long it took; a setting outside 1 second to 1 hour is refused naming it.
|
||||
func TestAHandlingPastEventOverrunTellsItsHookWhileItRuns(t *testing.T) {
|
||||
now := time.Date(2026, 11, 25, 12, 0, 0, 0, time.UTC)
|
||||
var clock sync.Mutex
|
||||
var lines []string
|
||||
h := NewEventHub(func(f string, a ...any) { clock.Lock(); lines = append(lines, fmt.Sprintf(f, a...)); clock.Unlock() })
|
||||
h.held.now = func() time.Time { clock.Lock(); defer clock.Unlock(); return now }
|
||||
advance := func(d time.Duration) { clock.Lock(); now = now.Add(d); clock.Unlock() }
|
||||
var fire []func()
|
||||
h.held.after = func(d time.Duration, f func()) func() {
|
||||
if d != 60*time.Second {
|
||||
t.Errorf("the overrun is timed at %s, want the default 60s", d)
|
||||
}
|
||||
clock.Lock()
|
||||
fire = append(fire, f)
|
||||
clock.Unlock()
|
||||
return func() {}
|
||||
}
|
||||
release := make(chan struct{})
|
||||
entered := make(chan struct{}, 4)
|
||||
if err := h.Handle("mesh.mod.m.event.>", func(jetstream.Msg) { entered <- struct{}{}; <-release },
|
||||
func(subject string, _ []byte) string { return subject[len("mesh.mod.m.event."):] }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var told [][]Handling
|
||||
h.OnOverrun(func(past []Handling) { clock.Lock(); told = append(told, past); clock.Unlock() })
|
||||
if err := h.held.settle(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h.hearOn(nil)(&seqMsg{subject: "mesh.mod.m.event.first", seq: 1})
|
||||
<-entered
|
||||
advance(time.Second)
|
||||
h.hearOn(nil)(&seqMsg{subject: "mesh.mod.m.event.second", seq: 2})
|
||||
<-entered
|
||||
advance(59 * time.Second)
|
||||
clock.Lock()
|
||||
timers := fire
|
||||
clock.Unlock()
|
||||
for _, f := range timers {
|
||||
f()
|
||||
}
|
||||
clock.Lock()
|
||||
if len(told) != 2 || len(told[1]) != 2 || told[1][0].Subject != "mesh.mod.m.event.first" || told[1][0].Key != "first" ||
|
||||
!told[1][0].Since.Equal(now.Add(-60*time.Second)) || told[1][1].Key != "second" {
|
||||
t.Fatalf("the hook was told %+v", told)
|
||||
}
|
||||
clock.Unlock()
|
||||
close(release)
|
||||
deadline := time.After(5 * time.Second)
|
||||
for {
|
||||
clock.Lock()
|
||||
n, last := len(told), []Handling(nil)
|
||||
if n > 0 {
|
||||
last = told[n-1]
|
||||
}
|
||||
clock.Unlock()
|
||||
if n >= 4 && len(last) == 0 {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-deadline:
|
||||
t.Fatalf("the hook was told %d times, the last of %v", n, last)
|
||||
case <-time.After(5 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
clock.Lock()
|
||||
said := strings.Join(lines, "\n")
|
||||
clock.Unlock()
|
||||
if !strings.Contains(said, "the event mesh.mod.m.event.first (key first) took 1m0s to handle, over 100ms") {
|
||||
t.Errorf("the journal says %q", said)
|
||||
}
|
||||
t.Setenv(stdio.SettingWord(EventOverrunSetting), "500ms")
|
||||
if err := h.held.settle(); err == nil || !strings.Contains(err.Error(), EventOverrunSetting) {
|
||||
t.Errorf("an overrun of 500ms was accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user