|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Matias Fontanini |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above |
| 12 | + * copyright notice, this list of conditions and the following disclaimer |
| 13 | + * in the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + */ |
| 29 | + |
| 30 | +#include <vector> |
| 31 | +#include <memory> |
| 32 | +#include <boost/optional.hpp> |
| 33 | +#include <librdkafka/rdkafka.h> |
| 34 | +#include "macros.h" |
| 35 | +#include "message.h" |
| 36 | + |
| 37 | +#ifndef CPPKAFKA_QUEUE_H |
| 38 | +#define CPPKAFKA_QUEUE_H |
| 39 | + |
| 40 | +namespace cppkafka { |
| 41 | +/** |
| 42 | + * \brief Represents a rdkafka queue |
| 43 | + * |
| 44 | + * This is a simple wrapper over a rd_kafka_queue_t* |
| 45 | + */ |
| 46 | +class CPPKAFKA_API Queue { |
| 47 | +public: |
| 48 | + /** |
| 49 | + * \brief Creates a Queue object that doesn't take ownership of the handle |
| 50 | + * |
| 51 | + * \param handle The handle to be used |
| 52 | + */ |
| 53 | + static Queue make_non_owning(rd_kafka_queue_t* handle); |
| 54 | + |
| 55 | + /** |
| 56 | + * \brief Constructs an empty queue |
| 57 | + * |
| 58 | + * Note that using any methods except Queue::get_handle on an empty queue is undefined |
| 59 | + * behavior |
| 60 | + */ |
| 61 | + Queue(); |
| 62 | + |
| 63 | + /** |
| 64 | + * \brief Constructs a queue using a handle |
| 65 | + * |
| 66 | + * This will take ownership of the handle |
| 67 | + * |
| 68 | + * \param handle The handle to be used |
| 69 | + */ |
| 70 | + Queue(rd_kafka_queue_t* handle); |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the rdkakfa handle |
| 74 | + */ |
| 75 | + rd_kafka_queue_t* get_handle() const; |
| 76 | + |
| 77 | + /** |
| 78 | + * \brief Returns the length of the queue |
| 79 | + * |
| 80 | + * This translates to a call to rd_kafka_queue_length |
| 81 | + */ |
| 82 | + size_t get_length() const; |
| 83 | + |
| 84 | + /** |
| 85 | + * \brief Forward to another queue |
| 86 | + * |
| 87 | + * This translates to a call to rd_kafka_queue_forward |
| 88 | + */ |
| 89 | + void forward_to_queue(const Queue& forward_queue) const; |
| 90 | + |
| 91 | + /** |
| 92 | + * \brief Sets the timeout for consume operations |
| 93 | + * |
| 94 | + * This timeout is applied when calling consume() |
| 95 | + * |
| 96 | + * \param timeout The timeout to be set |
| 97 | + */ |
| 98 | + void set_consume_timeout(std::chrono::milliseconds timeout); |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets the configured timeout. |
| 102 | + * |
| 103 | + * \sa Queue::set_timeout |
| 104 | + */ |
| 105 | + std::chrono::milliseconds get_consume_timeout() const; |
| 106 | + |
| 107 | + /** |
| 108 | + * \brief Consume a message from this queue |
| 109 | + * |
| 110 | + * This translates to a call to rd_kafka_consume_queue using the configured timeout for this object |
| 111 | + * |
| 112 | + * \return A message |
| 113 | + */ |
| 114 | + Message consume() const; |
| 115 | + |
| 116 | + /** |
| 117 | + * \brief Consume a message from this queue |
| 118 | + * |
| 119 | + * Same as consume() but the specified timeout will be used instead of the configured one |
| 120 | + * |
| 121 | + * \param timeout The timeout to be used on this call |
| 122 | + * |
| 123 | + * \return A message |
| 124 | + */ |
| 125 | + Message consume(std::chrono::milliseconds timeout) const; |
| 126 | + |
| 127 | + /** |
| 128 | + * \brief Consumes a batch of messages from this queue |
| 129 | + * |
| 130 | + * This translates to a call to rd_kafka_consume_batch_queue using the configured timeout for this object |
| 131 | + * |
| 132 | + * \param max_batch_size The max number of messages to consume if available |
| 133 | + * |
| 134 | + * \return A list of messages. Could be empty if there's nothing to consume |
| 135 | + */ |
| 136 | + MessageList consume_batch(size_t max_batch_size) const; |
| 137 | + |
| 138 | + /** |
| 139 | + * \brief Consumes a batch of messages from this queue |
| 140 | + * |
| 141 | + * Same as Queue::consume_batch(size_t) but the specified timeout will be used instead of the configured one |
| 142 | + * |
| 143 | + * \param max_batch_size The max number of messages to consume if available |
| 144 | + * |
| 145 | + * \param timeout The timeout to be used on this call |
| 146 | + * |
| 147 | + * \return A list of messages. Could be empty if there's nothing to consume |
| 148 | + */ |
| 149 | + MessageList consume_batch(size_t max_batch_size, std::chrono::milliseconds timeout) const; |
| 150 | + |
| 151 | + /** |
| 152 | + * Indicates whether this queue is valid (not null) |
| 153 | + */ |
| 154 | + explicit operator bool() const { |
| 155 | + return handle_ != nullptr; |
| 156 | + } |
| 157 | + |
| 158 | +private: |
| 159 | + static const std::chrono::milliseconds DEFAULT_TIMEOUT; |
| 160 | + |
| 161 | + using HandlePtr = std::unique_ptr<rd_kafka_queue_t, decltype(&rd_kafka_queue_destroy)>; |
| 162 | + |
| 163 | + struct NonOwningTag { }; |
| 164 | + |
| 165 | + Queue(rd_kafka_queue_t* handle, NonOwningTag); |
| 166 | + |
| 167 | + // Members |
| 168 | + HandlePtr handle_; |
| 169 | + std::chrono::milliseconds timeout_ms_; |
| 170 | +}; |
| 171 | + |
| 172 | +using QueueList = std::vector<Queue>; |
| 173 | + |
| 174 | +} // cppkafka |
| 175 | + |
| 176 | +#endif //CPPKAFKA_QUEUE_H |
0 commit comments