Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handler.Before(), handler.After(), and handler.WithLock() options #154

Open
bounoable opened this issue Sep 23, 2023 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@bounoable
Copy link
Contributor

bounoable commented Sep 23, 2023

Package: github.com/modernice/goes/event/handler

handler.Before and handler.After

Note: need to think about function signatures for Before and After options. Receiving multiple events in the callbacks only makes sense if the Debounce option will be implemented (#153)

package example

func example(bus event.Bus) {
  h := handler.New(
    bus,

    handler.Before(func(_ context.Context, events []event.Event) error {
      log.Printf("Handling events:\n%v", events)
      return nil
    }),

    handler.After(func(_ context.Context, r handler.Result) error {
      log.Printf("Failed to handle events:\n%v", r.Events)
      log.Printf("Failed event: %v", r.Failed)
      log.Printf("Error: %v", f.Err)

      // either return an error so it will be returned in the handler's error channel
      return fmt.Errorf("wrapped error: %w", f.Err)

      // or return nil to not report the error in the error channel
      return nil
    }),
  )
}

handler.WithLock and handler.WithReadLock

handler.WithLock and handler.WithReadLock are wrappers for handler.Before() and handler.After() to lock/unlock a Mutex (or some type with Lock + Unlock or RLock + RUnlock methods) before/after handling events:

package example

func example1(bus event.Bus) {
  var mux sync.Mutex

  h := handler.New(bus, handler.WithLock(&mux))
}

func example2(bus event.Bus) {
  var mux sync.RWMutex

  h := handler.New(bus, handler.WithReadLock(&mux))
}

func example3(bus event.Bus) {
  var mux sync.RWMutex

  h := handler.New(bus, handler.WithLock(&mux))
}

WithLock implementation:

package handler

type Mutex interface {
  Lock()
  Unlock()
}

func WithLock(l Mutex) Option {
  return func(h *Handler) {
    Before(func(context.Context, []event.Event) error {
      l.Lock()
      return nil
    })(h)

    After(func(context.Context, handler.Result) error {
      l.Unlock()
      return err
    })(h)
  }
}
@bounoable bounoable added the enhancement New feature or request label Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant