diff --git a/go/process/event_hub.go b/go/process/event_hub.go index 5898c0ac..8719e9c0 100644 --- a/go/process/event_hub.go +++ b/go/process/event_hub.go @@ -66,22 +66,25 @@ func NewEventHub(logf func(string, ...any)) *EventHub { // Handle registers handle for the subjects matching pattern (`*` one token, `>` the rest). The handler acknowledges // the message itself. A pattern that overlaps one registered (one matches every subject of the other) is refused, -// so a message always has one taker. -// -// key, when given, is the key of the pattern's events (design 47 H2 H-A-683 (1)): one key's events are handled one at a -// time in the order heard and the events of different keys side by side, at most `events-at-once` keys at once; every pattern -// given none hands its events under one shared key, so a caller that gives none is handled one at a time as before. -func (h *EventHub) Handle(pattern string, handle func(jetstream.Msg), key ...EventKey) error { +// so a message always has one taker. Its events are handled under one shared key, one at a time (HandleByKey). +func (h *EventHub) Handle(pattern string, handle func(jetstream.Msg)) error { + return h.handleByKey(pattern, handle, nil) +} + +// HandleByKey is Handle for a pattern whose events are held by key (design 47 H2 H-A-683 (1), H-A-737 (1)): key is a function of +// a message's subject and body. One key's events are handled one at a time in the order heard and the events of different keys +// side by side, at most `events-at-once` keys at once; every pattern given no key (Handle) hands its events under one shared key. +func (h *EventHub) HandleByKey(pattern string, key EventKey, handle func(jetstream.Msg)) error { + if key == nil { + return fmt.Errorf("the events %q are given no key function", pattern) + } + return h.handleByKey(pattern, handle, key) +} + +func (h *EventHub) handleByKey(pattern string, handle func(jetstream.Msg), keyOf EventKey) error { if pattern == "" || handle == nil { return fmt.Errorf("an event handler needs a subject pattern and a handler") } - if len(key) > 1 { - return fmt.Errorf("the events %q are given more than one key function", pattern) - } - var keyOf EventKey - if len(key) == 1 { - keyOf = key[0] - } h.mu.Lock() defer h.mu.Unlock() for _, r := range h.routes { diff --git a/go/process/event_hub_integration_test.go b/go/process/event_hub_integration_test.go index b80da385..0e1b9561 100644 --- a/go/process/event_hub_integration_test.go +++ b/go/process/event_hub_integration_test.go @@ -369,13 +369,13 @@ func subTheHubKeepsAHeldMessageAliveWhileAnotherKeyIsHandled(t *testing.T) { } hub := process.NewEventHub(t.Logf) release, handled := make(chan struct{}), make(chan string, 4) - if err := hub.Handle("mesh.mod.m.event.>", func(m jetstream.Msg) { + if err := hub.HandleByKey("mesh.mod.m.event.>", func(subject string, _ []byte) string { return subject }, func(m jetstream.Msg) { if strings.HasSuffix(m.Subject(), ".slow") { <-release } _ = m.Ack() handled <- m.Subject() - }, func(subject string, _ []byte) string { return subject }); err != nil { + }); err != nil { t.Fatal(err) } if err := hub.Start(ctx, js, cred); err != nil { diff --git a/go/process/event_hub_test.go b/go/process/event_hub_test.go index 89d6e068..56a5ee9a 100644 --- a/go/process/event_hub_test.go +++ b/go/process/event_hub_test.go @@ -201,7 +201,7 @@ func TestTheHubHoldsEventsByKeyAndSaysThemInProgress(t *testing.T) { h := NewEventHub(logf) ticks := make(chan time.Time) h.tick = func(time.Duration) (<-chan time.Time, func()) { return ticks, func() {} } - if err := h.Handle("mesh.mod.m.event.>", handler, lastToken); err != nil { + if err := h.HandleByKey("mesh.mod.m.event.>", lastToken, handler); err != nil { t.Fatal(err) } if err := h.held.settle(); err != nil { @@ -289,7 +289,7 @@ func TestTheHubHoldsEventsByKeyAndSaysThemInProgress(t *testing.T) { t.Setenv(stdio.SettingWord(EventsAtOnceSetting), "1") release2 := make(chan struct{}) one := NewEventHub(logf) - if err := one.Handle("k.>", blockOn(release2), func(subject string, _ []byte) string { return subject }); err != nil { + if err := one.HandleByKey("k.>", func(subject string, _ []byte) string { return subject }, blockOn(release2)); err != nil { t.Fatal(err) } if err := one.held.settle(); err != nil { @@ -371,8 +371,8 @@ func TestAHandlingPastEventOverrunTellsItsHookWhileItRuns(t *testing.T) { } 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 { + if err := h.HandleByKey("mesh.mod.m.event.>", func(subject string, _ []byte) string { return subject[len("mesh.mod.m.event."):] }, + func(jetstream.Msg) { entered <- struct{}{}; <-release }); err != nil { t.Fatal(err) } var told [][]Handling @@ -427,3 +427,9 @@ func TestAHandlingPastEventOverrunTellsItsHookWhileItRuns(t *testing.T) { t.Errorf("an overrun of 500ms was accepted: %v", err) } } + +// Design 47 H2 H-A-737 (1): Handle keeps its earlier form, so a hub handed to a module's own interface (seatlist.ProcessBus's +// EventHandlers) is still one; holding by key is HandleByKey's. +var _ interface { + Handle(string, func(jetstream.Msg)) error +} = (*EventHub)(nil) diff --git a/go/views/process_reader_integration_test.go b/go/views/process_reader_integration_test.go index e13db902..f368af0e 100644 --- a/go/views/process_reader_integration_test.go +++ b/go/views/process_reader_integration_test.go @@ -385,8 +385,8 @@ func subAHandlingPastEventOverrunRaisesItsFindingAndTheNextStartClearsIt(t *test } hub := process.NewEventHub(t.Logf) release := make(chan struct{}) - if err := hub.Handle("mesh.mod.p.event.slow", func(m jetstream.Msg) { <-release; _ = m.Ack() }, - func(string, []byte) string { return "the-slow-key" }); err != nil { + if err := hub.HandleByKey("mesh.mod.p.event.slow", func(string, []byte) string { return "the-slow-key" }, + func(m jetstream.Msg) { <-release; _ = m.Ack() }); err != nil { t.Fatal(err) } found := &clearedFindings{put: make(chan string, 8), cleared: make(chan string, 8)}