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

Introduce Query Parameter in handler.WithStore() for Selective Startup Queries #124

Closed
bounoable opened this issue May 30, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@bounoable
Copy link
Contributor

bounoable commented May 30, 2023

This proposal is to enhance the handler.WithStore() function in the event handler by introducing a query options. This would permit us to constrain the events that are fetched during startup to, for example, a specific timeframe:

package example

import (
  "log"
  "github.com/modernice/goes/event/handler"
  "github.com/modernice/goes/event/query"
  "github.com/modernice/goes/event/query/time"
)

func example() {
  h := handler.New(bus, handler.WithStore(store, query.Time(
    time.After(time.Now().AddDate(0, 0, -7)), // Fetches only the "foo" events from the past 7 days
  ))

  event.HandleWith(h, handleEvent, "foo")

  errs, err := h.Run(context.TODO())
  // handle err

  for err := range errs {
    log.Println(err)
  }
}

func handleEvent(evt event.Event) {
  // event handling logic
}

Background & Motivation

Consider a Mailer that subscribes to events to dispatch emails. The Mailer must be resistent to crashes, so it has to check for missed events on service startup.

We can construct this Mailer as an event handler using the handler.Handler type and handler.WithStore() option provided by goes. The WithStore() option will fetch the events that are registered in the handler from the event store. This could potentially take a lot of time, depending on the size of the event store.

If this proposal would be implemented, startup performance could be greatly improved by narrowing down the event query's temporal scope, which would limit the timeframe for which the events are checked by the Mailer. This could greatly reduce startup time on server restarts.

Additionally, the handler.Handler can be upgraded to support periodic handler restarts, which guarantees that the handler captures any previously overlooked events, all while not excessively consuming computational resources when the event store is very large.

Considerations

The handler.WithStore() option could become difficult to modify in the future without breaking compatibility. We could adjust the design to handler.WithStore(store, handler.StartupQuery(query.New(...))). This may slightly compromise DX, but it would ensure the API is future-proof.

Could also deprecate handler.WithStore() and create a new handler.Startup() option. Would better describe the feature.

To-Do

  • Create another issue for mentioned "period restarts" feature
@bounoable bounoable added the enhancement New feature or request label May 30, 2023
@bounoable bounoable changed the title Introduce Query Parameter in handler.WithStore() for Selective Event Fetching Introduce Query Parameter in handler.WithStore() for Selective Startup Queries May 31, 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