modernice/goes

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

Closed this issue · 0 comments

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