|
| 1 | +local librdkafka = require 'kafka.librdkafka' |
| 2 | +local ffi = require 'ffi' |
| 3 | + |
| 4 | +local KafkaConfig = {} |
| 5 | +KafkaConfig.__index = KafkaConfig |
| 6 | + |
| 7 | +--[[ |
| 8 | + Create configuration object or dublicate one. |
| 9 | + Result will be set up the defaults. |
| 10 | +
|
| 11 | + Please see CONFIGURATION.md for the default settings. |
| 12 | +]]-- |
| 13 | + |
| 14 | +function KafkaConfig.create(original_config) |
| 15 | + local config = { cb_ = {} } |
| 16 | + setmetatable(config, KafkaConfig) |
| 17 | + |
| 18 | + if original_config and original_config.kafka_conf_ then |
| 19 | + rawset(config, "kafka_conf_", librdkafka.rd_kafka_conf_dup(original_config.kafka_conf_)) |
| 20 | + config:set_delivery_cb(original_config.cb_.dr_cb_) |
| 21 | + config:set_stat_cb(original_config.cb_.stat_cb_) |
| 22 | + config:set_error_cb(original_config.cb_.error_cb_) |
| 23 | + config:set_log_cb(original_config.cb_.log_cb_) |
| 24 | + else |
| 25 | + rawset(config, "kafka_conf_", librdkafka.rd_kafka_conf_new()) |
| 26 | + end |
| 27 | + ffi.gc(config.kafka_conf_, function (config) |
| 28 | + librdkafka.rd_kafka_conf_destroy(config) |
| 29 | + end |
| 30 | + ) |
| 31 | + |
| 32 | + return config |
| 33 | +end |
| 34 | + |
| 35 | + |
| 36 | +--[[ |
| 37 | + Dump the configuration properties and values of `conf` to a map |
| 38 | + with "key", "value" pairs. |
| 39 | +]]-- |
| 40 | + |
| 41 | +function KafkaConfig:dump() |
| 42 | + assert(self.kafka_conf_ ~= nil) |
| 43 | + |
| 44 | + local size = ffi.new("size_t[1]") |
| 45 | + local dump = librdkafka.rd_kafka_conf_dump(self.kafka_conf_, size) |
| 46 | + ffi.gc(dump, function(d) librdkafka.rd_kafka_conf_dump_free(d, size[0]) end) |
| 47 | + |
| 48 | + local result = {} |
| 49 | + for i = 0, tonumber(size[0])-1,2 do |
| 50 | + result[ffi.string(dump[i])] = ffi.string(dump[i+1]) |
| 51 | + end |
| 52 | + |
| 53 | + return result |
| 54 | +end |
| 55 | + |
| 56 | + |
| 57 | +--[[ |
| 58 | + Sets a configuration property. |
| 59 | + In case of failure "error(errstr)" is called and 'errstr' |
| 60 | + is updated to contain a human readable error string. |
| 61 | +]]-- |
| 62 | + |
| 63 | +function KafkaConfig:__newindex(name, value) |
| 64 | + assert(self.kafka_conf_ ~= nil) |
| 65 | + |
| 66 | + local ERRLEN = 256 |
| 67 | + local errbuf = ffi.new("char[?]", ERRLEN) -- cdata objects are garbage collected |
| 68 | + |
| 69 | + if librdkafka.rd_kafka_conf_set(self.kafka_conf_, name, tostring(value), errbuf, ERRLEN) ~= librdkafka.RD_KAFKA_CONF_OK then |
| 70 | + error(ffi.string(errbuf)) |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | + |
| 75 | +--[[ |
| 76 | + Set delivery report callback in provided conf object. |
| 77 | + Format: callback_function(payload, errstr) |
| 78 | + 'payload' is the message payload |
| 79 | + 'errstr' nil if everything is ok or readable error description otherwise |
| 80 | +]]-- |
| 81 | + |
| 82 | +function KafkaConfig:set_delivery_cb(callback) |
| 83 | + assert(self.kafka_conf_ ~= nil) |
| 84 | + |
| 85 | + if callback then |
| 86 | + self.cb_.dr_cb_ = callback |
| 87 | + librdkafka.rd_kafka_conf_set_dr_cb(self.kafka_conf_, |
| 88 | + function(rk, payload, len, err) |
| 89 | + local errstr = nil |
| 90 | + if err ~= librdkafka.RD_KAFKA_RESP_ERR_NO_ERROR then |
| 91 | + errstr = ffi.string(librdkafka.rd_kafka_err2str(err)) |
| 92 | + end |
| 93 | + callback(ffi.string(payload, tonumber(len)), errstr) |
| 94 | + end) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | + |
| 99 | +--[[ |
| 100 | + Set statistics callback. |
| 101 | + The statistics callback is called from `KafkaProducer:poll` every |
| 102 | + `statistics.interval.ms` (needs to be configured separately). |
| 103 | + Format: callback_function(json) |
| 104 | + 'json' - String containing the statistics data in JSON format |
| 105 | +]]-- |
| 106 | + |
| 107 | +function KafkaConfig:set_stat_cb(callback) |
| 108 | + assert(self.kafka_conf_ ~= nil) |
| 109 | + |
| 110 | + if callback then |
| 111 | + self.cb_.stat_cb_ = callback |
| 112 | + librdkafka.rd_kafka_conf_set_stats_cb(self.kafka_conf_, |
| 113 | + function(rk, json, json_len) |
| 114 | + callback(ffi.string(json, json_len)) |
| 115 | + return 0 --librdkafka will immediately free the 'json' pointer. |
| 116 | + end) |
| 117 | + end |
| 118 | +end |
| 119 | + |
| 120 | + |
| 121 | +--[[ |
| 122 | + Set error callback. |
| 123 | + The error callback is used by librdkafka to signal critical errors |
| 124 | + back to the application. |
| 125 | + Format: callback_function(err_numb, reason) |
| 126 | +]]-- |
| 127 | + |
| 128 | +function KafkaConfig:set_error_cb(callback) |
| 129 | + assert(self.kafka_conf_ ~= nil) |
| 130 | + |
| 131 | + if callback then |
| 132 | + self.cb_.error_cb_ = callback |
| 133 | + librdkafka.rd_kafka_conf_set_error_cb(self.kafka_conf_, |
| 134 | + function(rk, err, reason) |
| 135 | + callback(tonumber(err), ffi.string(reason)) |
| 136 | + end) |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +--[[ |
| 141 | + Set logger callback. |
| 142 | + The default is to print to stderr. |
| 143 | + Alternatively the application may provide its own logger callback. |
| 144 | + Or pass 'callback' as nil to disable logging. |
| 145 | + Format: callback_function(level, fac, buf) |
| 146 | +]]-- |
| 147 | + |
| 148 | +function KafkaConfig:set_log_cb(callback) |
| 149 | + assert(self.kafka_conf_ ~= nil) |
| 150 | + |
| 151 | + if callback then |
| 152 | + self.cb_.log_cb_ = callback |
| 153 | + librdkafka.rd_kafka_conf_set_log_cb(self.kafka_conf_, |
| 154 | + function(rk, level, fac, buf) |
| 155 | + callback(tonumber(level), ffi.string(fac), ffi.string(buf)) |
| 156 | + end) |
| 157 | + end |
| 158 | +end |
| 159 | + |
| 160 | +return KafkaConfig |
0 commit comments