1
1
/****************************************************************************
2
- * Copyright 2022, Optimizely, Inc. and contributors *
2
+ * Copyright 2022-2023 , Optimizely, Inc. and contributors *
3
3
* *
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); *
5
5
* you may not use this file except in compliance with the License. *
@@ -27,6 +27,7 @@ import (
27
27
guuid "github.com/google/uuid"
28
28
"github.com/optimizely/go-sdk/pkg/event"
29
29
"github.com/optimizely/go-sdk/pkg/logging"
30
+ "github.com/optimizely/go-sdk/pkg/odp/config"
30
31
"github.com/optimizely/go-sdk/pkg/odp/utils"
31
32
"golang.org/x/sync/semaphore"
32
33
)
@@ -39,7 +40,8 @@ const maxRetries = 3
39
40
40
41
// Manager represents the event manager.
41
42
type Manager interface {
42
- Start (ctx context.Context , apiKey , apiHost string )
43
+ // odpConfig is required here since it can be updated anytime and ticker needs to be aware of latest changes
44
+ Start (ctx context.Context , odpConfig config.Config )
43
45
IdentifyUser (apiKey , apiHost , userID string )
44
46
ProcessEvent (apiKey , apiHost string , odpEvent Event ) bool
45
47
FlushEvents (apiKey , apiHost string )
@@ -59,16 +61,6 @@ type BatchEventManager struct {
59
61
logger logging.OptimizelyLogProducer
60
62
}
61
63
62
- // WithBatchSize sets the batch size as a config option to be passed into the NewBatchEventManager method
63
- // default value is 10
64
- func WithBatchSize (bsize int ) EMOptionFunc {
65
- return func (bm * BatchEventManager ) {
66
- if bsize > 0 {
67
- bm .batchSize = bsize
68
- }
69
- }
70
- }
71
-
72
64
// WithQueueSize sets the queue size as a config option to be passed into the NewBatchEventManager method
73
65
// default value is 10000
74
66
func WithQueueSize (qsize int ) EMOptionFunc {
@@ -83,7 +75,11 @@ func WithQueueSize(qsize int) EMOptionFunc {
83
75
// default value is 1 second
84
76
func WithFlushInterval (flushInterval time.Duration ) EMOptionFunc {
85
77
return func (bm * BatchEventManager ) {
86
- if flushInterval > 0 {
78
+ if flushInterval >= 0 {
79
+ // if flush interval is zero, send events immediately by setting batchSize to 1
80
+ if flushInterval == 0 {
81
+ bm .batchSize = 1
82
+ }
87
83
bm .flushInterval = flushInterval
88
84
}
89
85
}
@@ -147,11 +143,12 @@ func NewBatchEventManager(options ...EMOptionFunc) *BatchEventManager {
147
143
}
148
144
149
145
// Start does not do any initialization, just starts the ticker
150
- func (bm * BatchEventManager ) Start (ctx context.Context , apiKey , apiHost string ) {
151
- if ! bm .IsOdpServiceIntegrated (apiKey , apiHost ) {
146
+ // odpConfig is required here since it can be updated anytime and ticker needs to be aware of latest changes
147
+ func (bm * BatchEventManager ) Start (ctx context.Context , odpConfig config.Config ) {
148
+ if ! bm .IsOdpServiceIntegrated (odpConfig .GetAPIKey (), odpConfig .GetAPIHost ()) {
152
149
return
153
150
}
154
- bm .startTicker (ctx , apiKey , apiHost )
151
+ bm .startTicker (ctx , odpConfig )
155
152
}
156
153
157
154
// IdentifyUser associates a full-stack userid with an established VUID
@@ -209,7 +206,11 @@ func (bm *BatchEventManager) ProcessEvent(apiKey, apiHost string, odpEvent Event
209
206
}
210
207
211
208
// StartTicker starts new ticker for flushing events
212
- func (bm * BatchEventManager ) startTicker (ctx context.Context , apiKey , apiHost string ) {
209
+ func (bm * BatchEventManager ) startTicker (ctx context.Context , odpConfig config.Config ) {
210
+ // Do not start ticker if flushInterval is 0
211
+ if bm .flushInterval <= 0 {
212
+ return
213
+ }
213
214
// Make sure multiple go-routines dont reinitialize ticker
214
215
bm .flushLock .Lock ()
215
216
if bm .ticker != nil {
@@ -224,10 +225,10 @@ func (bm *BatchEventManager) startTicker(ctx context.Context, apiKey, apiHost st
224
225
for {
225
226
select {
226
227
case <- bm .ticker .C :
227
- bm .FlushEvents (apiKey , apiHost )
228
+ bm .FlushEvents (odpConfig . GetAPIKey (), odpConfig . GetAPIHost () )
228
229
case <- ctx .Done ():
229
230
bm .logger .Debug ("BatchEventManager stopped, flushing events." )
230
- bm .FlushEvents (apiKey , apiHost )
231
+ bm .FlushEvents (odpConfig . GetAPIKey (), odpConfig . GetAPIHost () )
231
232
return
232
233
}
233
234
}
0 commit comments