Add offset commit and delivery report callbacks

This commit is contained in:
Matias Fontanini
2016-05-31 07:26:31 -07:00
parent fd2d616506
commit 82393b558e
13 changed files with 236 additions and 34 deletions

View File

@@ -0,0 +1,39 @@
#ifndef CPPKAFKA_CLONABLE_PTR_H
#define CPPKAFKA_CLONABLE_PTR_H
#include <memory>
namespace cppkafka {
template <typename T, typename Deleter, typename Cloner>
class ClonablePtr {
public:
ClonablePtr(T* ptr, const Deleter& deleter, const Cloner& cloner)
: handle_(ptr, deleter), cloner_(cloner) {
}
ClonablePtr(const ClonablePtr& rhs)
: handle_(rhs.cloner_(rhs.handle_.get()), rhs.handle_.get_deleter()), cloner_(rhs.cloner_) {
}
ClonablePtr& operator=(const ClonablePtr& rhs) {
handle_.reset(cloner_(rhs.handle_.get()));
}
ClonablePtr(ClonablePtr&&) = default;
ClonablePtr& operator=(ClonablePtr&&) = default;
~ClonablePtr() = default;
T* get() const {
return handle_.get();
}
private:
std::unique_ptr<T, Deleter> handle_;
Cloner cloner_;
};
} // cppkafka
#endif // CPPKAFKA_CLONABLE_PTR_H

View File

@@ -3,28 +3,40 @@
#include <memory>
#include <string>
#include <functional>
#include <librdkafka/rdkafka.h>
#include "topic_partition_list.h"
#include "clonable_ptr.h"
namespace cppkafka {
class Message;
class Configuration {
public:
using DeliveryReportCallback = std::function<void(const Message&)>;
using OffsetCommitCallback = std::function<void(rd_kafka_resp_err_t,
const TopicPartitionList& topic_partitions)>;
Configuration();
Configuration(const Configuration& rhs);
Configuration(Configuration&& rhs) noexcept = default;
Configuration& operator=(const Configuration& rhs);
Configuration& operator=(Configuration&& rhs) noexcept = default;
void set(const std::string& name, const std::string& value);
void set_delivery_report_callback(DeliveryReportCallback callback);
void set_offset_commit_callback(OffsetCommitCallback callback);
rd_kafka_conf_t* get_handle() const;
const DeliveryReportCallback& get_delivery_report_callback() const;
const OffsetCommitCallback& get_offset_commit_callback() const;
private:
using HandlePtr = std::unique_ptr<rd_kafka_conf_t, decltype(&rd_kafka_conf_destroy)>;
using HandlePtr = ClonablePtr<rd_kafka_conf_t, decltype(&rd_kafka_conf_destroy),
decltype(&rd_kafka_conf_dup)>;
Configuration(rd_kafka_conf_t* ptr);
static HandlePtr make_handle(rd_kafka_conf_t* ptr);
HandlePtr handle_;
DeliveryReportCallback delivery_report_callback_;
OffsetCommitCallback offset_commit_callback_;
};
} // cppkafka

View File

@@ -7,10 +7,10 @@
#include <functional>
#include "kafka_handle_base.h"
#include "message.h"
#include "configuration.h"
namespace cppkafka {
class Configuration;
class TopicConfiguration;
class Consumer : public KafkaHandleBase {
@@ -46,6 +46,7 @@ public:
TopicPartitionList get_offsets_position(const TopicPartitionList& topic_partitions);
TopicPartitionList get_subscription();
TopicPartitionList get_assignment();
const Configuration& get_configuration() const;
Message poll();
private:
@@ -59,6 +60,7 @@ private:
AssignmentCallback assignment_callback_;
RevocationCallback revocation_callback_;
RebalanceErrorCallback rebalance_error_callback_;
Configuration config_;
};
} // cppkafka

View File

@@ -11,6 +11,8 @@ namespace cppkafka {
class Message {
public:
static Message make_non_owning(rd_kafka_message_t* handle);
Message();
Message(rd_kafka_message_t* handle);
Message(const Message&) = delete;
@@ -32,6 +34,11 @@ public:
private:
using HandlePtr = std::unique_ptr<rd_kafka_message_t, decltype(&rd_kafka_message_destroy)>;
struct NonOwningTag { };
Message(rd_kafka_message_t* handle, NonOwningTag);
Message(HandlePtr handle);
HandlePtr handle_;
Buffer payload_;
Buffer key_;

View File

@@ -27,11 +27,15 @@ public:
void set_payload_policy(PayloadPolicy policy);
PayloadPolicy get_payload_policy() const;
const Configuration& get_configuration() const;
void produce(const Topic& topic, const Partition& partition, const Buffer& payload);
void produce(const Topic& topic, const Partition& partition, const Buffer& payload,
const Buffer& key);
void produce(const Topic& topic, const Partition& partition, const Buffer& payload,
const Buffer& key, void* user_data);
int poll();
private:
Configuration config_;
PayloadPolicy message_payload_policy_;

View File

@@ -15,6 +15,7 @@ using TopicPartitionList = std::vector<TopicPartition>;
TopicPartitionsListPtr convert(const std::vector<TopicPartition>& topic_partitions);
std::vector<TopicPartition> convert(const TopicPartitionsListPtr& topic_partitions);
std::vector<TopicPartition> convert(rd_kafka_topic_partition_list_t* topic_partitions);
TopicPartitionsListPtr make_handle(rd_kafka_topic_partition_list_t* handle);
} // cppkafka