@@ -105,6 +105,25 @@ function Consumer.create(config)
105105 return consumer
106106end
107107
108+ function Consumer :_get_topic_rd_config (config )
109+ local rd_config = librdkafka .rd_kafka_topic_conf_new ()
110+
111+ ffi .gc (rd_config , function (rd_config )
112+ librdkafka .rd_kafka_topic_conf_destroy (rd_config )
113+ end )
114+
115+ local ERRLEN = 256
116+ for key , value in pairs (config ) do
117+ local errbuf = ffi .new (" char[?]" , ERRLEN ) -- cdata objects are garbage collected
118+
119+ if librdkafka .rd_kafka_topic_conf_set (rd_config , key , value , errbuf , ERRLEN ) ~= librdkafka .RD_KAFKA_CONF_OK then
120+ return nil , ffi .string (errbuf )
121+ end
122+ end
123+
124+ return rd_config , nil
125+ end
126+
108127function Consumer :_get_consumer_rd_config ()
109128 local rd_config = librdkafka .rd_kafka_conf_new ()
110129
@@ -141,7 +160,6 @@ function Consumer:_get_consumer_rd_config()
141160
142161 librdkafka .rd_kafka_conf_set_consume_cb (rd_config ,
143162 function (rkmessage )
144- print (rkmessage )
145163 self ._output_ch :put (ConsumerMessage .create (rkmessage ))
146164 end )
147165
@@ -156,14 +174,20 @@ function Consumer:_get_consumer_rd_config()
156174 print (" log" , tonumber (level ), ffi .string (fac ), ffi .string (buf ))
157175 end )
158176
177+ local rd_topic_config , err = self :_get_topic_rd_config ({[" auto.offset.reset" ] = " earliest" })
178+ if err ~= nil then
179+ return nil , err
180+ end
181+
182+ librdkafka .rd_kafka_conf_set_default_topic_conf (rd_config , rd_topic_config )
183+
159184 return rd_config , nil
160185end
161186
162187function Consumer :_poll ()
163188 while true do
164- librdkafka .rd_kafka_poll (self ._rd_consumer , 10 )
165- local rd_message = librdkafka .rd_kafka_consumer_poll (self ._rd_consumer , 1000 )
166- print (rd_message )
189+ librdkafka .rd_kafka_poll (self ._rd_consumer , 1 )
190+ local rd_message = librdkafka .rd_kafka_consumer_poll (self ._rd_consumer , 1 )
167191 if rd_message ~= nil and rd_message .err ~= librdkafka .RD_KAFKA_RESP_ERR_NO_ERROR then
168192 -- FIXME: properly log this
169193 print (ffi .string (librdkafka .rd_kafka_err2str (rd_message .err )))
@@ -238,9 +262,7 @@ function Consumer:subscribe(topics)
238262
239263 local list = librdkafka .rd_kafka_topic_partition_list_new (# topics )
240264 for _ , topic in ipairs (topics ) do
241- print (topic , librdkafka .RD_KAFKA_PARTITION_UA )
242- -- librdkafka.rd_kafka_topic_partition_list_add(list, topic, librdkafka.RD_KAFKA_PARTITION_UA)
243- librdkafka .rd_kafka_topic_partition_list_add (list , topic , 0 )
265+ librdkafka .rd_kafka_topic_partition_list_add (list , topic , librdkafka .RD_KAFKA_PARTITION_UA )
244266 end
245267
246268 local err = nil
0 commit comments