Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): cleanup event processor #167

Merged
merged 10 commits into from
Nov 7, 2019
Merged

Conversation

thomaszurkan-optimizely
Copy link
Contributor

@thomaszurkan-optimizely thomaszurkan-optimizely commented Nov 6, 2019

Summary

  • When event batch size has been reached only start one batch event processing go routine.
  • When queue size is met, log a message and do not add to the queue.
  • Duration used was setting the time too far into the future by multiplying by second and then by milliseconds. Thank Mike D for catching this one.

Testing
This PR has unit tests for all the fixes above.


p.Q.Add(event)

if p.Q.Size() >= p.BatchSize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we flatten this out:

if p.Q.Size() < p.BatchSize || p.processing {
    return
}

p.processing = true
go func() {
    p.FlushEvents()
    p.processing = false
}()

@@ -44,6 +44,7 @@ type BatchEventProcessor struct {
Mux sync.Mutex
Ticker *time.Ticker
EventDispatcher Dispatcher
processing bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is like a poor man's Mutex :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you noticed that. Yeah, I set it and test it in the same thread and then set it back in the other thread. So, there is really no chance of collision. However, go lint disagreed. So, I went ahead and created a new util struct called AtomicProperty.

p.Q.Add(event)

if p.Q.Size() >= p.BatchSize {
if processing, ok := p.processing.Get().(bool); ok && !processing {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can use a semaphore.

if p.sem.TryAquire(1) {
    go func() {
        p.FlushEvents()
        p.sem.Release(1)
    }()
}

ref: https://medium.com/@deckarep/gos-extended-concurrency-semaphores-part-1-5eeabfa351ce

Copy link
Contributor

@mikecdavis mikecdavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one minor comment on the switcheroo :)

@@ -123,6 +128,11 @@ func NewBatchEventProcessor(options ...BPOptionConfig) *BatchEventProcessor {
p.BatchSize = DefaultBatchSize
}

if p.BatchSize > p.MaxQueueSize {
pLogger.Warning("Batch size is larger than queue size. Swapping")
p.BatchSize, p.MaxQueueSize = p.MaxQueueSize, p.BatchSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit sneaky and I'm not sure we want to swap parameters like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, it was a lint suggestion. i love it! :)

@thomaszurkan-optimizely thomaszurkan-optimizely merged commit dbac8f6 into master Nov 7, 2019
@thomaszurkan-optimizely thomaszurkan-optimizely deleted the fixEventProcessor branch November 7, 2019 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants