|
1 | 1 | package subscription |
2 | 2 |
|
3 | | -// NOTE(ar3s3ru): Volatile subscriptions are currently disabled, since the |
4 | | -// support for Subscriptions is currently being deprecated. |
5 | | - |
6 | | -// import ( |
7 | | -// "context" |
8 | | -// "fmt" |
9 | | - |
10 | | -// "github.com/get-eventually/go-eventually/eventstore" |
11 | | -// ) |
12 | | - |
13 | | -// var _ Subscription = Volatile{} |
14 | | - |
15 | | -// // Volatile is a Subscription type that does not keep state of |
16 | | -// // the last Event processed or received, nor survives the Subscription |
17 | | -// // checkpoint between restarts. |
18 | | -// // |
19 | | -// // Use this Subscription type for volatile processes, such as projecting |
20 | | -// // realtime metrics, or when you're only interested in newer events |
21 | | -// // committed to the Event Store. |
22 | | -// type Volatile struct { |
23 | | -// SubscriptionName string |
24 | | -// Target TargetStream |
25 | | -// EventStore eventstore.Subscriber |
26 | | -// } |
27 | | - |
28 | | -// // Name is the name of the subscription. |
29 | | -// func (v Volatile) Name() string { return v.SubscriptionName } |
30 | | - |
31 | | -// // Start starts the Subscription by opening a subscribing Event Stream |
32 | | -// // using the subscription's Subscriber instance. |
33 | | -// func (v Volatile) Start(ctx context.Context, stream eventstore.EventStream) error { |
34 | | -// var err error |
35 | | - |
36 | | -// switch t := v.Target.(type) { |
37 | | -// case TargetStreamAll: |
38 | | -// err = v.EventStore.SubscribeToAll(ctx, stream) |
39 | | -// case TargetStreamType: |
40 | | -// err = v.EventStore.SubscribeToType(ctx, stream, t.Type) |
41 | | -// default: |
42 | | -// return fmt.Errorf("subscription.Volatile: unexpected target type") |
43 | | -// } |
44 | | - |
45 | | -// if err != nil { |
46 | | -// return fmt.Errorf("subscription.Volatile: event subscriber exited with error: %w", err) |
47 | | -// } |
48 | | - |
49 | | -// return nil |
50 | | -// } |
51 | | - |
52 | | -// // Checkpoint is a no-op operation, since the transient nature of the |
53 | | -// // Subscription does not require to persist its current state. |
54 | | -// func (Volatile) Checkpoint(ctx context.Context, event eventstore.Event) error { |
55 | | -// return nil |
56 | | -// } |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/get-eventually/go-eventually/eventstore" |
| 9 | + "github.com/get-eventually/go-eventually/eventstore/stream" |
| 10 | + "github.com/get-eventually/go-eventually/logger" |
| 11 | + "github.com/get-eventually/go-eventually/subscription/checkpoint" |
| 12 | +) |
| 13 | + |
| 14 | +var _ Subscription = &Volatile{} |
| 15 | + |
| 16 | +// Volatile is a Subscription type that does not keep state of |
| 17 | +// the last Event processed or received, nor survives the Subscription |
| 18 | +// checkpoint between restarts. |
| 19 | +// |
| 20 | +// Use this Subscription type for volatile processes, such as projecting |
| 21 | +// realtime metrics, or when you're only interested in newer events |
| 22 | +// committed to the Event Store. |
| 23 | +type Volatile struct { |
| 24 | + SubscriptionName string |
| 25 | + Target stream.Target |
| 26 | + Logger logger.Logger |
| 27 | + EventStore interface { |
| 28 | + eventstore.Streamer |
| 29 | + eventstore.SequenceNumberGetter |
| 30 | + } |
| 31 | + |
| 32 | + // PullEvery is the minimum interval between each streaming call to the Event Store. |
| 33 | + // |
| 34 | + // Defaults to DefaultPullInterval if unspecified or negative value |
| 35 | + // has been provided. |
| 36 | + PullEvery time.Duration |
| 37 | + |
| 38 | + // MaxInterval is the maximum interval between each streaming call to the Event Store. |
| 39 | + // Use this value to ensure a specific eventual consistency window. |
| 40 | + // |
| 41 | + // Defaults to DefaultMaxPullInterval if unspecified or negative value |
| 42 | + // has been provided. |
| 43 | + MaxInterval time.Duration |
| 44 | + |
| 45 | + // BufferSize is the size of buffered channels used as EventStreams |
| 46 | + // by the Subscription when receiving Events from the Event Store. |
| 47 | + // |
| 48 | + // Defaults to DefaultPullCatchUpBufferSize if unspecified or a negative |
| 49 | + // value has been provided. |
| 50 | + BufferSize int |
| 51 | +} |
| 52 | + |
| 53 | +// Name is the name of the subscription. |
| 54 | +func (v *Volatile) Name() string { return v.SubscriptionName } |
| 55 | + |
| 56 | +// Start starts the Subscription by opening a subscribing Event Stream |
| 57 | +// using the subscription's Subscriber instance. |
| 58 | +func (v *Volatile) Start(ctx context.Context, es eventstore.EventStream) error { |
| 59 | + latestSequenceNumber, err := v.EventStore.LatestSequenceNumber(ctx) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("subscription.Volatile: failed to get latest sequence number from event store: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + catchUpSubscription := &CatchUp{ |
| 65 | + SubscriptionName: v.SubscriptionName, |
| 66 | + Target: v.Target, |
| 67 | + EventStore: v.EventStore, |
| 68 | + Checkpointer: checkpoint.FixedCheckpointer{StartingFrom: latestSequenceNumber}, |
| 69 | + Logger: v.Logger, |
| 70 | + PullEvery: v.PullEvery, |
| 71 | + MaxInterval: v.MaxInterval, |
| 72 | + BufferSize: v.BufferSize, |
| 73 | + } |
| 74 | + |
| 75 | + if err := catchUpSubscription.Start(ctx, es); err != nil { |
| 76 | + return fmt.Errorf("subscription.Volatile: internal catch-up subscription exited with error: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// Checkpoint is a no-op operation, since the transient nature of the |
| 83 | +// Subscription does not require to persist its current state. |
| 84 | +func (*Volatile) Checkpoint(ctx context.Context, event eventstore.Event) error { |
| 85 | + return nil |
| 86 | +} |
0 commit comments