|
| 1 | +package projection |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "golang.org/x/sync/errgroup" |
| 8 | + |
| 9 | + "github.com/get-eventually/go-eventually/eventstore" |
| 10 | + "github.com/get-eventually/go-eventually/logger" |
| 11 | + "github.com/get-eventually/go-eventually/subscription" |
| 12 | +) |
| 13 | + |
| 14 | +// DefaultRunnerBufferSize is the default size for the buffered channels |
| 15 | +// opened by a Runner instance, if not specified. |
| 16 | +const DefaultRunnerBufferSize = 32 |
| 17 | + |
| 18 | +type shouldCheckpointCtxKey struct{} |
| 19 | + |
| 20 | +func contextWithPreferredCheckpointStrategy(ctx context.Context, v *bool) context.Context { |
| 21 | + return context.WithValue(ctx, shouldCheckpointCtxKey{}, v) |
| 22 | +} |
| 23 | + |
| 24 | +// Checkpoint should be used in a projection.Apply method to signal |
| 25 | +// to a projection.Runner that the event processed should not checkpointed. |
| 26 | +// |
| 27 | +// NOTE: this is currently the default behavior of projection.Runner, so using |
| 28 | +// this method is not usually necessary. |
| 29 | +func Checkpoint(ctx context.Context) { |
| 30 | + if v, ok := ctx.Value(shouldCheckpointCtxKey{}).(*bool); ok { |
| 31 | + *v = true |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// DoNotCheckpoint should be used in a projection.Apply method to signal |
| 36 | +// to a projection.Runner that the event processed should not be checkpointed. |
| 37 | +func DoNotCheckpoint(ctx context.Context) { |
| 38 | + if v, ok := ctx.Value(shouldCheckpointCtxKey{}).(*bool); ok { |
| 39 | + *v = false |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Runner is an infrastructural component that orchestrates the state update |
| 44 | +// of an Applier or a Projection using the provided Subscription, |
| 45 | +// to subscribe to incoming events from the Event Store. |
| 46 | +type Runner struct { |
| 47 | + Applier Applier |
| 48 | + Subscription subscription.Subscription |
| 49 | + BufferSize int |
| 50 | + Logger logger.Logger |
| 51 | +} |
| 52 | + |
| 53 | +// Run starts listening to Events from the provided Subscription |
| 54 | +// and sinking them to the Applier instance to trigger state change. |
| 55 | +// |
| 56 | +// Run is a blocking call, that will exit when either the Applier returns an error, |
| 57 | +// or the Subscription stops. |
| 58 | +// |
| 59 | +// Run uses buffered channels to coordinate events communication between components, |
| 60 | +// using the value specified in BufferSize, if any, or DefaultRunnerBufferSize otherwise. |
| 61 | +// |
| 62 | +// To stop the Runner, cancel the provided context. |
| 63 | +// If the error returned upon exit is context.Canceled, that usually represent |
| 64 | +// a case of normal operation, so it could be treated as a non-error. |
| 65 | +func (r Runner) Run(ctx context.Context) error { |
| 66 | + if r.BufferSize == 0 { |
| 67 | + r.BufferSize = DefaultRunnerBufferSize |
| 68 | + } |
| 69 | + |
| 70 | + eventStream := make(chan eventstore.Event, r.BufferSize) |
| 71 | + toCheckpoint := make(chan eventstore.Event, r.BufferSize) |
| 72 | + |
| 73 | + group, ctx := errgroup.WithContext(ctx) |
| 74 | + |
| 75 | + group.Go(func() error { |
| 76 | + logger.Info(r.Logger, "Subscription started for projection runner") |
| 77 | + |
| 78 | + if err := r.Subscription.Start(ctx, eventStream); err != nil { |
| 79 | + return fmt.Errorf("projection.Runner: subscription exited with error: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | + }) |
| 84 | + |
| 85 | + group.Go(func() error { |
| 86 | + defer close(toCheckpoint) |
| 87 | + |
| 88 | + for event := range eventStream { |
| 89 | + // Default behavior for chechkpointing is to checkpoint every event processed. |
| 90 | + // |
| 91 | + // Users could use projection.DoNotCheckpoint(ctx) to specify whether they want the message |
| 92 | + // not to be checkpointed. |
| 93 | + checkpoint := true |
| 94 | + ctx := contextWithPreferredCheckpointStrategy(ctx, &checkpoint) //nolint:govet // Shadowing is fine. |
| 95 | + |
| 96 | + if err := r.Applier.Apply(ctx, event); err != nil { |
| 97 | + return fmt.Errorf("projection.Runner: failed to apply event to projection: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + if checkpoint { |
| 101 | + toCheckpoint <- event |
| 102 | + } |
| 103 | + |
| 104 | + logger.Info(r.Logger, "Skip checkpointing of event processed", |
| 105 | + logger.With("sequenceNumber", event.SequenceNumber), |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | + }) |
| 111 | + |
| 112 | + for event := range toCheckpoint { |
| 113 | + logger.Info(r.Logger, "Checkpointing processed event", |
| 114 | + logger.With("sequenceNumber", event.SequenceNumber), |
| 115 | + ) |
| 116 | + |
| 117 | + if err := r.Subscription.Checkpoint(ctx, event); err != nil { |
| 118 | + return fmt.Errorf("projection.Runner: failed to checkpoint event: %w", err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return group.Wait() |
| 123 | +} |
0 commit comments