Replaced termination callback with throwing exception

This commit is contained in:
Alexander Damian
2019-04-24 10:30:32 -04:00
parent e8c4397b66
commit 0c1119727b
6 changed files with 73 additions and 34 deletions

View File

@@ -34,6 +34,7 @@
#include <functional>
#include <thread>
#include "../consumer.h"
#include "../exceptions.h"
namespace cppkafka {
@@ -47,6 +48,14 @@ public:
static const TimeUnit DEFAULT_BACKOFF_STEP;
static const TimeUnit DEFAULT_MAXIMUM_BACKOFF;
static const size_t DEFAULT_MAXIMUM_RETRIES;
/**
* @brief Type which any functor must return.
*/
struct ReturnType {
bool abort_{true};
Error error_;
};
/**
* The backoff policy to use
@@ -119,11 +128,16 @@ public:
void perform(const Functor& callback) {
TimeUnit backoff = initial_backoff_;
size_t retries = maximum_retries_;
ReturnType rt;
while (retries--) {
auto start = std::chrono::steady_clock::now();
// If the callback returns true, we're done
if (callback()) {
return;
rt = callback();
if (rt.abort_) {
if (rt.error_) {
break; //terminal error
}
return; //success
}
auto end = std::chrono::steady_clock::now();
auto time_elapsed = end - start;
@@ -134,6 +148,8 @@ public:
// Increase out backoff depending on the policy being used
backoff = increase_backoff(backoff);
}
// No more retries left or we have a terminal error.
throw ConsumerException(rt.error_);
}
private:
TimeUnit increase_backoff(TimeUnit backoff);