Add a generic "event" event on ConsumerDispatcher

This commit is contained in:
Matias Fontanini
2017-07-04 18:23:42 -07:00
parent 08815e97c0
commit b7a0dce710
2 changed files with 18 additions and 7 deletions

View File

@@ -87,6 +87,11 @@ public:
*/ */
struct Throttle {}; struct Throttle {};
/**
* Tag to indicate there was some event processed (message, timeout, error, etc)
*/
struct Event {};
/** /**
* Constructs a consumer dispatcher over the given consumer * Constructs a consumer dispatcher over the given consumer
* *
@@ -117,10 +122,12 @@ private:
using OnErrorArgs = std::tuple<Error>; using OnErrorArgs = std::tuple<Error>;
using OnEofArgs = std::tuple<EndOfFile, TopicPartition>; using OnEofArgs = std::tuple<EndOfFile, TopicPartition>;
using OnTimeoutArgs = std::tuple<Timeout>; using OnTimeoutArgs = std::tuple<Timeout>;
using OnEventArgs = std::tuple<Event>;
static void handle_error(Error error); static void handle_error(Error error);
static void handle_eof(EndOfFile, const TopicPartition& /*topic_partition*/) { } static void handle_eof(EndOfFile, const TopicPartition& /*topic_partition*/) { }
static void handle_timeout(Timeout) { } static void handle_timeout(Timeout) { }
static void handle_event(Event) { }
template <typename Functor> template <typename Functor>
void handle_throttle(Throttle, const Functor& callback, Message msg) { void handle_throttle(Throttle, const Functor& callback, Message msg) {
@@ -250,7 +257,9 @@ private:
!std::is_same<type_not_found, !std::is_same<type_not_found,
typename find_type<OnTimeoutArgs, Functor>::type>::value || typename find_type<OnTimeoutArgs, Functor>::type>::value ||
!std::is_same<type_not_found, !std::is_same<type_not_found,
typename find_type<OnErrorArgs, Functor>::type>::value, typename find_type<OnErrorArgs, Functor>::type>::value ||
!std::is_same<type_not_found,
typename find_type<OnEventArgs, Functor>::type>::value,
"Callback doesn't match any of the expected signatures" "Callback doesn't match any of the expected signatures"
); );
} }
@@ -335,24 +344,26 @@ void BasicConsumerDispatcher<ConsumerType>::run(const Args&... args) {
const auto on_error = find_matching_functor<OnErrorArgs>(args..., &self::handle_error); const auto on_error = find_matching_functor<OnErrorArgs>(args..., &self::handle_error);
const auto on_eof = find_matching_functor<OnEofArgs>(args..., &self::handle_eof); const auto on_eof = find_matching_functor<OnEofArgs>(args..., &self::handle_eof);
const auto on_timeout = find_matching_functor<OnTimeoutArgs>(args..., &self::handle_timeout); const auto on_timeout = find_matching_functor<OnTimeoutArgs>(args..., &self::handle_timeout);
const auto on_event = find_matching_functor<OnEventArgs>(args..., &self::handle_event);
running_ = true; running_ = true;
while (running_) { while (running_) {
Message msg = consumer_.poll(); Message msg = consumer_.poll();
if (!msg) { if (!msg) {
on_timeout(Timeout{}); on_timeout(Timeout{});
continue;
} }
if (msg.get_error()) { else if (msg.get_error()) {
if (msg.is_eof()) { if (msg.is_eof()) {
on_eof(EndOfFile{}, { msg.get_topic(), msg.get_partition(), msg.get_offset() }); on_eof(EndOfFile{}, { msg.get_topic(), msg.get_partition(), msg.get_offset() });
} }
else { else {
on_error(msg.get_error()); on_error(msg.get_error());
} }
continue;
} }
process_message(on_message, std::move(msg), args...); else {
process_message(on_message, std::move(msg), args...);
}
on_event(Event{});
} }
} }

View File

@@ -49,8 +49,8 @@ ConsumerRunner::ConsumerRunner(Consumer& consumer, size_t expected, size_t parti
} }
} }
}, },
// Timeout callback // Every time there's any event callback
[&](ConsumerDispatcher::Timeout) { [&](ConsumerDispatcher::Event) {
if (expected > 0 && messages_.size() == expected) { if (expected > 0 && messages_.size() == expected) {
dispatcher.stop(); dispatcher.stop();
} }