@@ -56,21 +56,75 @@ const DefaultEventFlushInterval = 30 * time.Second
56
56
57
57
var pLogger = logging .GetLogger ("EventProcessor" )
58
58
59
+ // QPConfigOption is the QueuingProcessor options that give you the ability to add one more more options before the processor is initialized.
60
+ type QPConfigOption func (qp * QueueingEventProcessor )
61
+
62
+ // BatchSize sets the batch size as a config option to be passed into the NewProcessor method
63
+ func BatchSize (bsize int ) QPConfigOption {
64
+ return func (qp * QueueingEventProcessor ) {
65
+ qp .BatchSize = bsize
66
+ }
67
+ }
68
+
69
+ // QueueSize sets the queue size as a config option to be passed into the NewProcessor method
70
+ func QueueSize (qsize int ) QPConfigOption {
71
+ return func (qp * QueueingEventProcessor ) {
72
+ qp .MaxQueueSize = qsize
73
+ }
74
+ }
75
+
76
+ // FlushInterval sets the flush interval as a config option to be passed into the NewProcessor method
77
+ func FlushInterval (flushInterval time.Duration ) QPConfigOption {
78
+ return func (qp * QueueingEventProcessor ) {
79
+ qp .FlushInterval = flushInterval
80
+ }
81
+ }
82
+
83
+ // PQ sets the Processor Queue as a config option to be passed into the NewProcessor method
84
+ func PQ (q Queue ) QPConfigOption {
85
+ return func (qp * QueueingEventProcessor ) {
86
+ qp .Q = q
87
+ }
88
+ }
89
+ // PDispatcher sets the Processor Dispatcher as a config option to be passed into the NewProcessor method
90
+ func PDispatcher (d Dispatcher ) QPConfigOption {
91
+ return func (qp * QueueingEventProcessor ) {
92
+ qp .EventDispatcher = d
93
+ }
94
+ }
95
+
59
96
// NewEventProcessor returns a new instance of QueueingEventProcessor with queueSize and flushInterval
60
- func NewEventProcessor (exeCtx utils.ExecutionCtx , batchSize , queueSize int , flushInterval time. Duration ) * QueueingEventProcessor {
97
+ func NewEventProcessor (exeCtx utils.ExecutionCtx , options ... QPConfigOption ) * QueueingEventProcessor {
61
98
p := & QueueingEventProcessor {
62
- MaxQueueSize : queueSize ,
63
- FlushInterval : flushInterval ,
64
- Q : NewInMemoryQueue (queueSize ),
65
- EventDispatcher : NewQueueEventDispatcher (exeCtx .GetContext ()),
66
99
wg : exeCtx .GetWaitSync (),
67
100
}
68
- p .BatchSize = DefaultBatchSize
69
- if batchSize > 0 {
70
- p .BatchSize = batchSize
101
+
102
+ for _ , opt := range options {
103
+ opt (p )
104
+ }
105
+
106
+ if p .MaxQueueSize == 0 {
107
+ p .MaxQueueSize = defaultQueueSize
108
+ }
109
+
110
+ if p .FlushInterval == 0 {
111
+ p .FlushInterval = DefaultEventFlushInterval
112
+ }
113
+
114
+ if p .BatchSize == 0 {
115
+ p .BatchSize = DefaultBatchSize
116
+ }
117
+
118
+ if p .Q == nil {
119
+ p .Q = NewInMemoryQueue (p .MaxQueueSize )
120
+ }
121
+
122
+ if p .EventDispatcher == nil {
123
+ p .EventDispatcher = NewQueueEventDispatcher (exeCtx .GetContext ())
71
124
}
72
125
73
126
p .StartTicker (exeCtx .GetContext ())
127
+
74
128
return p
75
129
}
76
130
0 commit comments