|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// ContextStrategy is an interface that represents strategy for providing contexts for running MongoDB requests |
| 9 | +type ContextStrategy interface { |
| 10 | + // Append is the context used for EventStore.Append() calls |
| 11 | + Append() (context.Context, context.CancelFunc) |
| 12 | + // GetEventsFor is the context used for EventStore.GetEventsFor() calls |
| 13 | + GetEventsFor() (context.Context, context.CancelFunc) |
| 14 | + // FromVersion is the context used for EventStore.FromVersion() calls |
| 15 | + FromVersion() (context.Context, context.CancelFunc) |
| 16 | + // CountEventsFor is the context used for EventStore.CountEventsFor() calls |
| 17 | + CountEventsFor() (context.Context, context.CancelFunc) |
| 18 | + // CreateIndices is the context used for MongoDB EventStore implementation indices creation |
| 19 | + CreateIndices() (context.Context, context.CancelFunc) |
| 20 | +} |
| 21 | + |
| 22 | +// BackgroundContextStrategy is the ContextStrategy implementation that always returns Background context and noop cancel |
| 23 | +type BackgroundContextStrategy struct { |
| 24 | + ctx context.Context |
| 25 | +} |
| 26 | + |
| 27 | +// NewBackgroundContextStrategy instantiates new BackgroundContextStrategy |
| 28 | +func NewBackgroundContextStrategy() *BackgroundContextStrategy { |
| 29 | + return &BackgroundContextStrategy{ctx: context.Background()} |
| 30 | +} |
| 31 | + |
| 32 | +// Append is the context used for EventStore.Append() calls |
| 33 | +func (s *BackgroundContextStrategy) Append() (context.Context, context.CancelFunc) { |
| 34 | + return s.ctx, func() {} |
| 35 | +} |
| 36 | + |
| 37 | +// GetEventsFor is the context used for EventStore.GetEventsFor() calls |
| 38 | +func (s *BackgroundContextStrategy) GetEventsFor() (context.Context, context.CancelFunc) { |
| 39 | + return s.ctx, func() {} |
| 40 | +} |
| 41 | + |
| 42 | +// FromVersion is the context used for EventStore.FromVersion() calls |
| 43 | +func (s *BackgroundContextStrategy) FromVersion() (context.Context, context.CancelFunc) { |
| 44 | + return s.ctx, func() {} |
| 45 | +} |
| 46 | + |
| 47 | +// CountEventsFor is the context used for EventStore.CountEventsFor() calls |
| 48 | +func (s *BackgroundContextStrategy) CountEventsFor() (context.Context, context.CancelFunc) { |
| 49 | + return s.ctx, func() {} |
| 50 | +} |
| 51 | + |
| 52 | +// CreateIndices is the context used for MongoDB EventStore implementation indices creation |
| 53 | +func (s *BackgroundContextStrategy) CreateIndices() (context.Context, context.CancelFunc) { |
| 54 | + return s.ctx, func() {} |
| 55 | +} |
| 56 | + |
| 57 | +// TimeoutContextStrategy is the ContextStrategy implementation that returns configurable WithTimeout context and its cancel |
| 58 | +type TimeoutContextStrategy struct { |
| 59 | + append time.Duration |
| 60 | + getEventsFor time.Duration |
| 61 | + fromVersion time.Duration |
| 62 | + countEventsFor time.Duration |
| 63 | + createIndices time.Duration |
| 64 | +} |
| 65 | + |
| 66 | +// TimeoutContextStrategyOption is the options type to configure TimeoutContextStrategy creation |
| 67 | +type TimeoutContextStrategyOption func(s *TimeoutContextStrategy) |
| 68 | + |
| 69 | +// NewTimeoutContextStrategy instantiates new TimeoutContextStrategy |
| 70 | +func NewTimeoutContextStrategy(options ...TimeoutContextStrategyOption) *TimeoutContextStrategy { |
| 71 | + s := &TimeoutContextStrategy{ |
| 72 | + append: 5 * time.Second, |
| 73 | + getEventsFor: 30 * time.Second, |
| 74 | + fromVersion: 30 * time.Second, |
| 75 | + countEventsFor: 5 * time.Second, |
| 76 | + createIndices: 5 * time.Second, |
| 77 | + } |
| 78 | + |
| 79 | + for _, o := range options { |
| 80 | + o(s) |
| 81 | + } |
| 82 | + |
| 83 | + return s |
| 84 | +} |
| 85 | + |
| 86 | +// Append is the context used for EventStore.Append() calls |
| 87 | +func (s *TimeoutContextStrategy) Append() (context.Context, context.CancelFunc) { |
| 88 | + return context.WithTimeout(context.Background(), s.append) |
| 89 | +} |
| 90 | + |
| 91 | +// GetEventsFor is the context used for EventStore.GetEventsFor() calls |
| 92 | +func (s *TimeoutContextStrategy) GetEventsFor() (context.Context, context.CancelFunc) { |
| 93 | + return context.WithTimeout(context.Background(), s.getEventsFor) |
| 94 | +} |
| 95 | + |
| 96 | +// FromVersion is the context used for EventStore.FromVersion() calls |
| 97 | +func (s *TimeoutContextStrategy) FromVersion() (context.Context, context.CancelFunc) { |
| 98 | + return context.WithTimeout(context.Background(), s.fromVersion) |
| 99 | +} |
| 100 | + |
| 101 | +// CountEventsFor is the context used for EventStore.CountEventsFor() calls |
| 102 | +func (s *TimeoutContextStrategy) CountEventsFor() (context.Context, context.CancelFunc) { |
| 103 | + return context.WithTimeout(context.Background(), s.countEventsFor) |
| 104 | +} |
| 105 | + |
| 106 | +// CreateIndices is the context used for MongoDB EventStore implementation indices creation |
| 107 | +func (s *TimeoutContextStrategy) CreateIndices() (context.Context, context.CancelFunc) { |
| 108 | + return context.WithTimeout(context.Background(), s.createIndices) |
| 109 | +} |
| 110 | + |
| 111 | +// NewAppendTimeout is the TimeoutContextStrategy configuration option to set a timeout for Append call |
| 112 | +func NewAppendTimeout(timeout time.Duration) TimeoutContextStrategyOption { |
| 113 | + return func(s *TimeoutContextStrategy) { |
| 114 | + s.append = timeout |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// NewGetEventsForTimeout is the TimeoutContextStrategy configuration option to set a timeout for GetEventsFor call |
| 119 | +func NewGetEventsForTimeout(timeout time.Duration) TimeoutContextStrategyOption { |
| 120 | + return func(s *TimeoutContextStrategy) { |
| 121 | + s.getEventsFor = timeout |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// NewFromVersionTimeout is the TimeoutContextStrategy configuration option to set a timeout for FromVersion call |
| 126 | +func NewFromVersionTimeout(timeout time.Duration) TimeoutContextStrategyOption { |
| 127 | + return func(s *TimeoutContextStrategy) { |
| 128 | + s.fromVersion = timeout |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// NewCountEventsForTimeout is the TimeoutContextStrategy configuration option to set a timeout for CountEventsFor call |
| 133 | +func NewCountEventsForTimeout(timeout time.Duration) TimeoutContextStrategyOption { |
| 134 | + return func(s *TimeoutContextStrategy) { |
| 135 | + s.countEventsFor = timeout |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// NewCreateIndicesTimeout is the TimeoutContextStrategy configuration option to set a timeout for CreateIndices call |
| 140 | +func NewCreateIndicesTimeout(timeout time.Duration) TimeoutContextStrategyOption { |
| 141 | + return func(s *TimeoutContextStrategy) { |
| 142 | + s.createIndices = timeout |
| 143 | + } |
| 144 | +} |
0 commit comments