Skip to content

Commit

Permalink
Fix handler type of async event handler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Oct 16, 2020
1 parent 2a4812c commit 2249611
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions middleware/eventhandler/async/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,26 @@ import (
func NewMiddleware() (eh.EventHandlerMiddleware, chan Error) {
errCh := make(chan Error, 20)
return eh.EventHandlerMiddleware(func(h eh.EventHandler) eh.EventHandler {
return eh.EventHandlerFunc(func(ctx context.Context, event eh.Event) error {
go func() {
if err := h.HandleEvent(ctx, event); err != nil {
// Always try to deliver errors.
errCh <- Error{err, ctx, event}
}
}()
return nil
})
return &eventHandler{h, errCh}
}), errCh
}

type eventHandler struct {
eh.EventHandler
errCh chan Error
}

// HandleEvent implements the HandleEvent method of the EventHandler.
func (h *eventHandler) HandleEvent(ctx context.Context, event eh.Event) error {
go func() {
if err := h.EventHandler.HandleEvent(ctx, event); err != nil {
// Always try to deliver errors.
h.errCh <- Error{err, ctx, event}
}
}()
return nil
}

// Error is an async error containing the error and the event.
type Error struct {
Err error
Expand Down
2 changes: 1 addition & 1 deletion middleware/eventhandler/async/eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestEventHandler(t *testing.T) {
}
inner.RLock()
if !reflect.DeepEqual(inner.Events, []eh.Event{event}) {
t.Error("the event shoud have been handeled:", inner.Events)
t.Error("the event should have been handeled:", inner.Events)
}
inner.RUnlock()

Expand Down

0 comments on commit 2249611

Please sign in to comment.