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

rework of the event consumer #61

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

rework of the event consumer #61

wants to merge 8 commits into from

Conversation

ewollesen
Copy link
Contributor

As I was reading this code, the goroutine launch seemed really out of place, and the more I read, the more I thought it wasn't necessary, so then I sought to try and remove it. This is the result, and I think it's simpler and easier to read, so I'm submitting it for consideration. Maybe it's not worth messing with working code though.

I'd recommend looking at the individual commits in sequence, I think that might make it overall easier to see what is happening.

Take or leave the changes, it's not a big deal either way.

I've run it locally with the hydrophone, and checked that messages are still consumed, that all checked out. Of course it's used in other places as well, but that seemed like a good enough integration test before going any further.

Just makes it a little easier to find it without having to swap files.
Nothing was using it or looking for it.
I believe there was confusion about how this is called or should run. These
changes provide equivalent functionality, while being easier to read and
understand.

There are instructions from the library authors here:
https://github.com/IBM/sarama/blob/v1.38.1/consumer_group.go#L19

I think there was confusion around the steps detailed in the link above. So
let me clarify my understanding:

- Canceling the context passed to Consume is how the implementor of the
  sarama.ConsumerGroupHandler tells Consume to return. In other words, when
  we're shutting down, we cancel the context to get Consume to return. I base
  this off of #4 in the link.

- No goroutine needs to be launched on our side, the sarama library will
  launch the goroutines and those goroutines will call our implementation of
  ConsumeClaim(). This comes from #3.

- The warnings about thread-safety apply to our implementation of
  ConsumeClaim(). In other words, we need to be careful that s.consumer isn't
  nil, but since nothing in our code modifies s.consumer after instantiation,
  we're fine. This comes from #3.

- Since our own events.EventConsumer has to be able to respond to Stop()
  calls, we need to be able to break the for-loop in Start(), which is done by
  canceling the context that we pass to Consume(). The context.CancelFunc is
  guarded with a mutex for for extra thread-safety, so that if Stop() is called
  from multiple goroutines, we don't have any race conditions to worry about.

We can reason that the goroutine being previously launched was not necessary,
because there was only a single goroutine being launched, and the same
function that launched the goroutine then blocked, waiting for it to exit
before continuing.

Without the goroutine, the WaitGroup, stop channel and stopOnce are no longer
necessary.
SaramaConsumerGroup was implementing two different interfaces:
sarama.ConsumerGroupHandler, and events.EventConsumer. This changes separates
these two concerns, making it clearer the purpose of several methods that are
somewhat similarly named (Start, Stop, Cleanup, and Setup).

The two interfaces are worth separate implementations as they each have
different authors, and different purposes, with sarama.ConsumerGroupHandler
being a 3rd-party library concerned with Kafka message consumption, while
events.EventConsumer is homegrown and (despite its name) is concerned with
lifecycle management.

SeramaConsumerGroup is renamed to more clearly indicate its focus as an
implementation of EventConsumer.
The only method that uses consumerGroup is Start, so we can just instantiate
it in Start, and not have to worry about shared state.

After the consumerGroup instantiation is moved to Start, there's very little
going on in Initialize, so its remaining work is moved to
NewSaramaEventConsumer(), where it can fail sooner.
No need to keep it as a member, it's derived from the config that we're
keeping around anyway, and only used in a single function.
When the context is canceled, there's no guarantee that Consume() will return
context.Canceled. As a result, the context needs to be double-checked before
looping in order to shutdown cleanly.
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