Skip to content

Commit da047e2

Browse files
refac: Use a single HTTP client throughout the HTTPEventDispatcher life-cycle (#199)
* refac: Use a single HTTP client throughout the HTTPEventDispatcher life-cycle
1 parent 2caaa0d commit da047e2

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

pkg/client/factory.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClien
6969
} else {
7070
appClient.ConfigManager = config.NewPollingProjectConfigManager(
7171
f.SDKKey,
72-
config.InitialDatafile(f.Datafile),
73-
config.PollingInterval(config.DefaultPollingInterval),
72+
config.WithInitialDatafile(f.Datafile),
73+
config.WithPollingInterval(config.DefaultPollingInterval),
7474
)
7575
}
7676

@@ -115,16 +115,16 @@ func (f OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClien
115115
// WithPollingConfigManager sets polling config manager on a client.
116116
func WithPollingConfigManager(sdkKey string, pollingInterval time.Duration, initDataFile []byte) OptionFunc {
117117
return func(f *OptimizelyFactory) {
118-
f.configManager = config.NewPollingProjectConfigManager(sdkKey, config.InitialDatafile(initDataFile),
119-
config.PollingInterval(pollingInterval))
118+
f.configManager = config.NewPollingProjectConfigManager(sdkKey, config.WithInitialDatafile(initDataFile),
119+
config.WithPollingInterval(pollingInterval))
120120
}
121121
}
122122

123123
// WithPollingConfigManagerRequester sets polling config manager on a client.
124124
func WithPollingConfigManagerRequester(requester utils.Requester, pollingInterval time.Duration, initDataFile []byte) OptionFunc {
125125
return func(f *OptimizelyFactory) {
126-
f.configManager = config.NewPollingProjectConfigManager("", config.InitialDatafile(initDataFile),
127-
config.PollingInterval(pollingInterval), config.Requester(requester))
126+
f.configManager = config.NewPollingProjectConfigManager("", config.WithInitialDatafile(initDataFile),
127+
config.WithPollingInterval(pollingInterval), config.WithRequester(requester))
128128
}
129129
}
130130

pkg/config/polling_manager.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,29 @@ type PollingProjectConfigManager struct {
6363
// OptionFunc is used to provide custom configuration to the PollingProjectConfigManager.
6464
type OptionFunc func(*PollingProjectConfigManager)
6565

66-
// Requester is an optional function, sets a passed requester
67-
func Requester(requester utils.Requester) OptionFunc {
66+
// WithRequester is an optional function, sets a passed requester
67+
func WithRequester(requester utils.Requester) OptionFunc {
6868
return func(p *PollingProjectConfigManager) {
6969
p.requester = requester
7070
}
7171
}
7272

73-
// DatafileTemplate is an optional function, sets a passed datafile URL template
74-
func DatafileTemplate(datafileTemplate string) OptionFunc {
73+
// WithDatafileURLTemplate is an optional function, sets a passed datafile URL template
74+
func WithDatafileURLTemplate(datafileTemplate string) OptionFunc {
7575
return func(p *PollingProjectConfigManager) {
7676
p.datafileURLTemplate = datafileTemplate
7777
}
7878
}
7979

80-
// PollingInterval is an optional function, sets a passed polling interval
81-
func PollingInterval(interval time.Duration) OptionFunc {
80+
// WithPollingInterval is an optional function, sets a passed polling interval
81+
func WithPollingInterval(interval time.Duration) OptionFunc {
8282
return func(p *PollingProjectConfigManager) {
8383
p.pollingInterval = interval
8484
}
8585
}
8686

87-
// InitialDatafile is an optional function, sets a passed datafile
88-
func InitialDatafile(datafile []byte) OptionFunc {
87+
// WithInitialDatafile is an optional function, sets a passed datafile
88+
func WithInitialDatafile(datafile []byte) OptionFunc {
8989
return func(p *PollingProjectConfigManager) {
9090
p.initDatafile = datafile
9191
}

pkg/config/polling_manager_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestNewPollingProjectConfigManagerWithOptions(t *testing.T) {
5050
sdkKey := "test_sdk_key"
5151

5252
exeCtx := utils.NewCancelableExecutionCtx()
53-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
53+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
5454
configManager.Start(sdkKey, exeCtx)
5555
mockRequester.AssertExpectations(t)
5656

@@ -71,7 +71,7 @@ func TestNewPollingProjectConfigManagerWithNull(t *testing.T) {
7171
sdkKey := "test_sdk_key"
7272

7373
exeCtx := utils.NewCancelableExecutionCtx()
74-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
74+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
7575
configManager.Start(sdkKey, exeCtx)
7676
mockRequester.AssertExpectations(t)
7777

@@ -89,7 +89,7 @@ func TestNewPollingProjectConfigManagerWithSimilarDatafileRevisions(t *testing.T
8989
sdkKey := "test_sdk_key"
9090

9191
exeCtx := utils.NewCancelableExecutionCtx()
92-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
92+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
9393
configManager.Start(sdkKey, exeCtx)
9494
mockRequester.AssertExpectations(t)
9595

@@ -117,7 +117,7 @@ func TestNewPollingProjectConfigManagerWithLastModifiedDates(t *testing.T) {
117117
sdkKey := "test_sdk_key"
118118

119119
exeCtx := utils.NewCancelableExecutionCtx()
120-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
120+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
121121
configManager.Start(sdkKey, exeCtx)
122122

123123
// Fetch valid config
@@ -147,7 +147,7 @@ func TestNewPollingProjectConfigManagerWithDifferentDatafileRevisions(t *testing
147147
sdkKey := "test_sdk_key"
148148

149149
exeCtx := utils.NewCancelableExecutionCtx()
150-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
150+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
151151
configManager.Start(sdkKey, exeCtx)
152152
mockRequester.AssertExpectations(t)
153153

@@ -174,7 +174,7 @@ func TestNewPollingProjectConfigManagerWithErrorHandling(t *testing.T) {
174174
sdkKey := "test_sdk_key"
175175

176176
exeCtx := utils.NewCancelableExecutionCtx()
177-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
177+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
178178
configManager.Start(sdkKey, exeCtx)
179179
mockRequester.AssertExpectations(t)
180180

@@ -205,7 +205,7 @@ func TestNewPollingProjectConfigManagerOnDecision(t *testing.T) {
205205
sdkKey := "test_sdk_key"
206206

207207
exeCtx := utils.NewCancelableExecutionCtx()
208-
configManager := NewPollingProjectConfigManager(sdkKey, Requester(mockRequester))
208+
configManager := NewPollingProjectConfigManager(sdkKey, WithRequester(mockRequester))
209209
configManager.Start(sdkKey, exeCtx)
210210

211211
var numberOfCalls = 0
@@ -239,7 +239,7 @@ func TestPollingInterval(t *testing.T) {
239239
sdkKey := "test_sdk_key"
240240

241241
exeCtx := utils.NewCancelableExecutionCtx()
242-
configManager := NewPollingProjectConfigManager(sdkKey, PollingInterval(5*time.Second))
242+
configManager := NewPollingProjectConfigManager(sdkKey, WithPollingInterval(5*time.Second))
243243
configManager.Start(sdkKey, exeCtx)
244244

245245
assert.Equal(t, configManager.pollingInterval, 5*time.Second)
@@ -249,7 +249,7 @@ func TestInitialDatafile(t *testing.T) {
249249

250250
sdkKey := "test_sdk_key"
251251
exeCtx := utils.NewCancelableExecutionCtx()
252-
configManager := NewPollingProjectConfigManager(sdkKey, InitialDatafile([]byte("test")))
252+
configManager := NewPollingProjectConfigManager(sdkKey, WithInitialDatafile([]byte("test")))
253253
configManager.Start(sdkKey, exeCtx)
254254

255255
assert.Equal(t, configManager.initDatafile, []byte("test"))
@@ -259,7 +259,7 @@ func TestDatafileTemplate(t *testing.T) {
259259

260260
sdkKey := "test_sdk_key"
261261
datafileTemplate := "https://localhost/v1/%s.json"
262-
configManager := NewPollingProjectConfigManager(sdkKey, DatafileTemplate(datafileTemplate))
262+
configManager := NewPollingProjectConfigManager(sdkKey, WithDatafileURLTemplate(datafileTemplate))
263263

264264
assert.Equal(t, datafileTemplate, configManager.datafileURLTemplate)
265265
}

pkg/event/dispatcher.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ type Dispatcher interface {
4141

4242
// HTTPEventDispatcher is the HTTP implementation of the Dispatcher interface
4343
type HTTPEventDispatcher struct {
44+
requester *utils.HTTPRequester
4445
}
4546

4647
// DispatchEvent dispatches event with callback
47-
func (*HTTPEventDispatcher) DispatchEvent(event LogEvent) (bool, error) {
48+
func (ed *HTTPEventDispatcher) DispatchEvent(event LogEvent) (bool, error) {
4849

49-
requester := utils.NewHTTPRequester()
50-
_, _, code, err := requester.Post(event.EndPoint, event.Event)
50+
_, _, code, err := ed.requester.Post(event.EndPoint, event.Event)
5151

5252
// also check response codes
5353
// resp.StatusCode == 400 is an error
@@ -140,7 +140,7 @@ func (ed *QueueEventDispatcher) flushEvents() {
140140

141141
// NewQueueEventDispatcher creates a Dispatcher that queues in memory and then sends via go routine.
142142
func NewQueueEventDispatcher(ctx context.Context) Dispatcher {
143-
dispatcher := &QueueEventDispatcher{eventQueue: NewInMemoryQueue(defaultQueueSize), Dispatcher: &HTTPEventDispatcher{}}
143+
dispatcher := &QueueEventDispatcher{eventQueue: NewInMemoryQueue(defaultQueueSize), Dispatcher: &HTTPEventDispatcher{requester: utils.NewHTTPRequester()}}
144144

145145
go func() {
146146
<-ctx.Done()

pkg/event/processor_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (m *MockDispatcher) DispatchEvent(event LogEvent) (bool, error) {
4141
}
4242

4343
func NewMockDispatcher(queueSize int, shouldFail bool) *MockDispatcher {
44-
return &MockDispatcher{Events:NewInMemoryQueue(queueSize), ShouldFail:shouldFail}
44+
return &MockDispatcher{Events: NewInMemoryQueue(queueSize), ShouldFail: shouldFail}
4545
}
4646

4747
func TestDefaultEventProcessor_ProcessImpression(t *testing.T) {
@@ -125,7 +125,7 @@ func TestDefaultEventProcessor_DefaultConfig(t *testing.T) {
125125
processor := NewBatchEventProcessor(
126126
WithEventDispatcher(NewMockDispatcher(100, false)),
127127
// here we are setting the timing interval so that we don't have to wait the default 30 seconds
128-
WithFlushInterval(500 * time.Millisecond))
128+
WithFlushInterval(500*time.Millisecond))
129129
processor.Start(exeCtx)
130130

131131
impression := BuildTestImpressionEvent()
@@ -158,7 +158,7 @@ func TestDefaultEventProcessor_DefaultConfig(t *testing.T) {
158158
func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
159159
exeCtx := utils.NewCancelableExecutionCtx()
160160
processor := NewBatchEventProcessor(
161-
WithFlushInterval(1 * time.Second),
161+
WithFlushInterval(1*time.Second),
162162
WithQueueSize(100),
163163
WithQueue(NewInMemoryQueue(100)),
164164
WithEventDispatcher(NewMockDispatcher(100, false)))
@@ -194,7 +194,7 @@ func TestDefaultEventProcessor_BatchSizeMet(t *testing.T) {
194194
exeCtx := utils.NewCancelableExecutionCtx()
195195
processor := NewBatchEventProcessor(
196196
WithBatchSize(2),
197-
WithFlushInterval(1000 * time.Millisecond),
197+
WithFlushInterval(1000*time.Millisecond),
198198
WithQueue(NewInMemoryQueue(2)),
199199
WithEventDispatcher(NewMockDispatcher(100, false)))
200200
processor.Start(exeCtx)
@@ -238,7 +238,7 @@ func TestDefaultEventProcessor_BatchSizeMet(t *testing.T) {
238238
func TestDefaultEventProcessor_BatchSizeLessThanQSize(t *testing.T) {
239239
processor := NewBatchEventProcessor(
240240
WithQueueSize(2),
241-
WithFlushInterval(1000 * time.Millisecond),
241+
WithFlushInterval(1000*time.Millisecond),
242242
WithQueue(NewInMemoryQueue(100)),
243243
WithEventDispatcher(NewMockDispatcher(100, false)))
244244

@@ -252,7 +252,7 @@ func TestDefaultEventProcessor_QSizeExceeded(t *testing.T) {
252252
processor := NewBatchEventProcessor(
253253
WithQueueSize(2),
254254
WithBatchSize(2),
255-
WithFlushInterval(1000 * time.Millisecond),
255+
WithFlushInterval(1000*time.Millisecond),
256256
WithQueue(NewInMemoryQueue(2)),
257257
WithEventDispatcher(NewMockDispatcher(100, true)))
258258
processor.Start(exeCtx)
@@ -404,7 +404,7 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
404404
processor := NewBatchEventProcessor(
405405
WithQueueSize(100),
406406
WithQueue(NewInMemoryQueue(100)),
407-
WithEventDispatcher(&HTTPEventDispatcher{}))
407+
WithEventDispatcher(&HTTPEventDispatcher{requester: utils.NewHTTPRequester()}))
408408

409409
processor.Start(exeCtx)
410410

@@ -456,11 +456,9 @@ func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
456456
type NoOpLogger struct {
457457
}
458458

459-
460459
func (l *NoOpLogger) Log(level logging.LogLevel, message string, fields map[string]interface{}) {
461460
}
462461

463-
464462
func (l *NoOpLogger) SetLogLevel(level logging.LogLevel) {
465463

466464
}
@@ -486,13 +484,13 @@ BenchmarkWithBatchSize/BatchSize60-8 3000000 504 ns/op
486484
BenchmarkWithQueue/InMemoryQueue-8 2000000 674 ns/op
487485
BenchmarkWithQueue/ChannelQueue-8 2000000 937 ns/op
488486
489-
*/
487+
*/
490488
func BenchmarkWithQueueSize(b *testing.B) {
491489
// no op logger added to keep out extra discarded events
492490
logging.SetLogger(&NoOpLogger{})
493491

494492
merges := []struct {
495-
name string
493+
name string
496494
qSize int
497495
}{
498496
{"QueueSize100", 100},
@@ -506,7 +504,7 @@ func BenchmarkWithQueueSize(b *testing.B) {
506504
for _, merge := range merges {
507505
var totalSent = 0
508506
var numberRun = 0
509-
b.Run(merge.name, func (b *testing.B) {
507+
b.Run(merge.name, func(b *testing.B) {
510508
if numberRun == 0 {
511509
numberRun = b.N
512510
}
@@ -523,7 +521,7 @@ func BenchmarkWithBatchSize(b *testing.B) {
523521
logging.SetLogger(&NoOpLogger{})
524522

525523
merges := []struct {
526-
name string
524+
name string
527525
batchSize int
528526
}{
529527
{"BatchSize10", 10},
@@ -535,7 +533,7 @@ func BenchmarkWithBatchSize(b *testing.B) {
535533
}
536534

537535
for _, merge := range merges {
538-
b.Run(merge.name, func (b *testing.B) {
536+
b.Run(merge.name, func(b *testing.B) {
539537
benchmarkProcessorWithBatchSize(merge.batchSize, b)
540538
})
541539
}
@@ -545,11 +543,11 @@ func BenchmarkWithBatchSize(b *testing.B) {
545543
func BenchmarkWithQueue(b *testing.B) {
546544
logging.SetLogger(&NoOpLogger{})
547545

548-
b.Run("InMemoryQueue", func (b *testing.B) {
546+
b.Run("InMemoryQueue", func(b *testing.B) {
549547
benchmarkProcessorWithQueue(NewInMemoryQueue(defaultQueueSize), b)
550548
})
551549

552-
b.Run("ChannelQueue", func (b *testing.B) {
550+
b.Run("ChannelQueue", func(b *testing.B) {
553551
benchmarkProcessorWithQueue(NewChanQueue(defaultQueueSize), b)
554552
})
555553

0 commit comments

Comments
 (0)