Skip to content

Commit ab677b2

Browse files
committed
feat(mux): Add AckUnknownSubjects option
Adds the AckUnknownSubjects field to the HandlerMux struct. This controls the default behaviour when there is no registred handler for the incoming event subject.
1 parent 7e53a11 commit ab677b2

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

handler_mux.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ package cone
33
import "fmt"
44

55
func NewHandlerMux() *HandlerMux {
6-
return &HandlerMux{handlers: make(map[string]Handler)}
6+
return &HandlerMux{
7+
AckUnknownSubjects: false,
8+
handlers: make(map[string]Handler),
9+
}
710
}
811

912
type HandlerMux struct {
10-
handlers map[string]Handler
13+
AckUnknownSubjects bool
14+
handlers map[string]Handler
1115
}
1216

1317
func (h *HandlerMux) Handle(subject string, handler Handler) {
@@ -45,6 +49,9 @@ func (h *HandlerMux) Serve(r Response, e *Event) {
4549
func (h *HandlerMux) serveEvent(r Response, e *Event) error {
4650
handler, ok := h.handlers[e.Subject]
4751
if !ok {
52+
if h.AckUnknownSubjects {
53+
return r.Ack()
54+
}
4855
return r.Nak()
4956
}
5057

handler_mux_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ func TestServe(t *testing.T) {
151151
}
152152
})
153153

154+
t.Run("Unregistred event should ack if AckUnknownSubjects=true is set", func(t *testing.T) {
155+
c := cone.NewHandlerMux()
156+
c.AckUnknownSubjects = true
157+
r := conetest.NewRecorder()
158+
c.Serve(r, conetest.NewEvent("not.wanted", nil))
159+
if r.Result() != conetest.Ack {
160+
t.Errorf("Expected %s but got: %s", conetest.Ack, r.Result())
161+
}
162+
})
163+
154164
t.Run("Registred event should ack", func(t *testing.T) {
155165
c := cone.NewHandlerMux()
156166
c.HandleFunc("is.wanted", func(_ cone.Response, _ *cone.Event) {})

0 commit comments

Comments
 (0)