11/****************************************************************************
2- * Copyright 2022, Optimizely, Inc. and contributors *
2+ * Copyright 2022-2023 , Optimizely, Inc. and contributors *
33 * *
44 * Licensed under the Apache License, Version 2.0 (the "License"); *
55 * you may not use this file except in compliance with the License. *
@@ -27,6 +27,7 @@ import (
2727 guuid "github.com/google/uuid"
2828 "github.com/optimizely/go-sdk/pkg/event"
2929 "github.com/optimizely/go-sdk/pkg/logging"
30+ "github.com/optimizely/go-sdk/pkg/odp/config"
3031 "github.com/optimizely/go-sdk/pkg/odp/utils"
3132 "golang.org/x/sync/semaphore"
3233)
@@ -39,7 +40,8 @@ const maxRetries = 3
3940
4041// Manager represents the event manager.
4142type 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 )
4345 IdentifyUser (apiKey , apiHost , userID string )
4446 ProcessEvent (apiKey , apiHost string , odpEvent Event ) bool
4547 FlushEvents (apiKey , apiHost string )
@@ -59,16 +61,6 @@ type BatchEventManager struct {
5961 logger logging.OptimizelyLogProducer
6062}
6163
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-
7264// WithQueueSize sets the queue size as a config option to be passed into the NewBatchEventManager method
7365// default value is 10000
7466func WithQueueSize (qsize int ) EMOptionFunc {
@@ -83,7 +75,11 @@ func WithQueueSize(qsize int) EMOptionFunc {
8375// default value is 1 second
8476func WithFlushInterval (flushInterval time.Duration ) EMOptionFunc {
8577 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+ }
8783 bm .flushInterval = flushInterval
8884 }
8985 }
@@ -147,11 +143,12 @@ func NewBatchEventManager(options ...EMOptionFunc) *BatchEventManager {
147143}
148144
149145// 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 ()) {
152149 return
153150 }
154- bm .startTicker (ctx , apiKey , apiHost )
151+ bm .startTicker (ctx , odpConfig )
155152}
156153
157154// IdentifyUser associates a full-stack userid with an established VUID
@@ -209,7 +206,11 @@ func (bm *BatchEventManager) ProcessEvent(apiKey, apiHost string, odpEvent Event
209206}
210207
211208// 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+ }
213214 // Make sure multiple go-routines dont reinitialize ticker
214215 bm .flushLock .Lock ()
215216 if bm .ticker != nil {
@@ -224,10 +225,10 @@ func (bm *BatchEventManager) startTicker(ctx context.Context, apiKey, apiHost st
224225 for {
225226 select {
226227 case <- bm .ticker .C :
227- bm .FlushEvents (apiKey , apiHost )
228+ bm .FlushEvents (odpConfig . GetAPIKey (), odpConfig . GetAPIHost () )
228229 case <- ctx .Done ():
229230 bm .logger .Debug ("BatchEventManager stopped, flushing events." )
230- bm .FlushEvents (apiKey , apiHost )
231+ bm .FlushEvents (odpConfig . GetAPIKey (), odpConfig . GetAPIHost () )
231232 return
232233 }
233234 }
0 commit comments