kafka 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rubocop.yml +210 -0
  4. data/.travis.yml +45 -0
  5. data/CHANGELOG.md +3 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +182 -0
  10. data/Rakefile +69 -0
  11. data/examples/consumer.rb +55 -0
  12. data/examples/producer.rb +46 -0
  13. data/ext/Rakefile +69 -0
  14. data/kafka.gemspec +39 -0
  15. data/lib/kafka/admin.rb +141 -0
  16. data/lib/kafka/config.rb +145 -0
  17. data/lib/kafka/consumer.rb +87 -0
  18. data/lib/kafka/error.rb +44 -0
  19. data/lib/kafka/ffi/admin/admin_options.rb +121 -0
  20. data/lib/kafka/ffi/admin/config_entry.rb +97 -0
  21. data/lib/kafka/ffi/admin/config_resource.rb +101 -0
  22. data/lib/kafka/ffi/admin/delete_topic.rb +19 -0
  23. data/lib/kafka/ffi/admin/new_partitions.rb +77 -0
  24. data/lib/kafka/ffi/admin/new_topic.rb +91 -0
  25. data/lib/kafka/ffi/admin/result.rb +66 -0
  26. data/lib/kafka/ffi/admin/topic_result.rb +32 -0
  27. data/lib/kafka/ffi/admin.rb +16 -0
  28. data/lib/kafka/ffi/broker_metadata.rb +32 -0
  29. data/lib/kafka/ffi/client.rb +640 -0
  30. data/lib/kafka/ffi/config.rb +382 -0
  31. data/lib/kafka/ffi/consumer.rb +342 -0
  32. data/lib/kafka/ffi/error.rb +25 -0
  33. data/lib/kafka/ffi/event.rb +215 -0
  34. data/lib/kafka/ffi/group_info.rb +75 -0
  35. data/lib/kafka/ffi/group_list.rb +27 -0
  36. data/lib/kafka/ffi/group_member_info.rb +52 -0
  37. data/lib/kafka/ffi/message/header.rb +205 -0
  38. data/lib/kafka/ffi/message.rb +205 -0
  39. data/lib/kafka/ffi/metadata.rb +58 -0
  40. data/lib/kafka/ffi/opaque.rb +81 -0
  41. data/lib/kafka/ffi/opaque_pointer.rb +73 -0
  42. data/lib/kafka/ffi/partition_metadata.rb +61 -0
  43. data/lib/kafka/ffi/producer.rb +144 -0
  44. data/lib/kafka/ffi/queue.rb +65 -0
  45. data/lib/kafka/ffi/topic.rb +32 -0
  46. data/lib/kafka/ffi/topic_config.rb +126 -0
  47. data/lib/kafka/ffi/topic_metadata.rb +42 -0
  48. data/lib/kafka/ffi/topic_partition.rb +43 -0
  49. data/lib/kafka/ffi/topic_partition_list.rb +167 -0
  50. data/lib/kafka/ffi.rb +624 -0
  51. data/lib/kafka/poller.rb +28 -0
  52. data/lib/kafka/producer/delivery_report.rb +120 -0
  53. data/lib/kafka/producer.rb +127 -0
  54. data/lib/kafka/version.rb +8 -0
  55. data/lib/kafka.rb +11 -0
  56. metadata +159 -0
data/lib/kafka/ffi.rb ADDED
@@ -0,0 +1,624 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ require "kafka/error"
5
+
6
+ module Kafka
7
+ # Module FFI provides both a (mostly) complete set of low level function
8
+ # calls into librdkafka as well as a set of slightly higher level
9
+ # abstractions and objects that make working with the API easier. It is still
10
+ # required to know enough about using librdkafka to use the abstractions
11
+ # safely (see the introduction below).
12
+ #
13
+ # All exposed functions on Kafka::FFI are named to match the functions
14
+ # exposed by librdkafka (see rdkafka.h).
15
+ #
16
+ # See: https://github.com/edenhill/librdkafka/blob/master/INTRODUCTION.md
17
+ # See: https://github.com/edenhill/librdkafka/blob/master/src/rdkafka.h
18
+ module FFI
19
+ extend ::FFI::Library
20
+
21
+ ffi_lib [
22
+ File.expand_path("../../ext/librdkafka.so", __dir__),
23
+ File.expand_path("../../ext/librdkafka.dylib", __dir__),
24
+ ]
25
+
26
+ # Returns the loaded version of librdkafka
27
+ #
28
+ # @return [String] Version string
29
+ def self.version
30
+ rd_kafka_version_str
31
+ end
32
+
33
+ # Returns the set of features available from librdkafka
34
+ #
35
+ # @return [Array<String>] List of available features
36
+ def self.features
37
+ conf = Config.new
38
+ conf.get("builtin.features").split(",")
39
+ ensure
40
+ conf.destroy
41
+ end
42
+
43
+ # kafka_type is passed to rd_kafka_new to specify the role of the
44
+ # connection.
45
+ #
46
+ # @see rdkafka.h rd_kafka_type_t
47
+ enum :kafka_type, [
48
+ :producer, 0,
49
+ :consumer, 1,
50
+ ]
51
+
52
+ # config_result is return from many operations on Config and TopicConfig.
53
+ #
54
+ # @see rdkafka.h rd_kafka_conf_res_t
55
+ enum :config_result, [
56
+ :unknown, -2, # Unknown configuration name.
57
+ :invalid, -1, # Invalid configuration value.
58
+ :ok, 0, # Configuration okay
59
+ ]
60
+
61
+ # Response Errors
62
+ RD_KAFKA_RESP_ERR__TIMED_OUT = -185
63
+ RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS = -175
64
+ RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS = -174
65
+ RD_KAFKA_RESP_ERR__NO_OFFSET = -168
66
+ RD_KAFKA_RESP_ERR__NOENT = -156
67
+ RD_KAFKA_RESP_ERR__FATAL = -150
68
+
69
+ # @see rdkafka.h rd_kafka_resp_err_t
70
+ enum :error_code, [
71
+ :ok, 0,
72
+ ]
73
+
74
+ # @see rdkafka.h rd_kafka_cert_type_t
75
+ enum :cert_type, [
76
+ :public, 0,
77
+ :private, 1,
78
+ :ca, 2,
79
+ :_cnt, 3,
80
+ ]
81
+
82
+ # @see rdkafka.h rd_kafka_cert_enc_t
83
+ enum :cert_enc, [
84
+ :pkcs12, 0,
85
+ :der, 1,
86
+ :pem, 2,
87
+ :_cnt, 3,
88
+ ]
89
+
90
+ RD_KAFKA_OFFSET_BEGINNING = -2
91
+ RD_KAFKA_OFFSET_END = -1
92
+ RD_KAFKA_OFFSET_STORED = -1000
93
+ RD_KAFKA_OFFSET_INVALID = -1001
94
+
95
+ RD_KAFKA_EVENT_NONE = 0x00
96
+ RD_KAFKA_EVENT_DR = 0x01
97
+ RD_KAFKA_EVENT_FETCH = 0x02
98
+ RD_KAFKA_EVENT_LOG = 0x04
99
+ RD_KAFKA_EVENT_ERROR = 0x08
100
+ RD_KAFKA_EVENT_REBALANCE = 0x10
101
+ RD_KAFKA_EVENT_OFFSET_COMMIT = 0x20
102
+ RD_KAFKA_EVENT_STATS = 0x40
103
+ RD_KAFKA_EVENT_CREATETOPICS_RESULT = 100
104
+ RD_KAFKA_EVENT_DELETETOPICS_RESULT = 101
105
+ RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT = 102
106
+ RD_KAFKA_EVENT_ALTERCONFIGS_RESULT = 103
107
+ RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT = 104
108
+ RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH = 0x100
109
+
110
+ # @see rd_kafka_event_type_t
111
+ enum :event_type, [
112
+ :none, RD_KAFKA_EVENT_NONE,
113
+ :dr, RD_KAFKA_EVENT_DR,
114
+ :delivery, RD_KAFKA_EVENT_DR, # Alias for dr (delivery report)
115
+ :fetch, RD_KAFKA_EVENT_FETCH,
116
+ :log, RD_KAFKA_EVENT_LOG,
117
+ :error, RD_KAFKA_EVENT_ERROR,
118
+ :rebalance, RD_KAFKA_EVENT_REBALANCE,
119
+ :offset_commit, RD_KAFKA_EVENT_OFFSET_COMMIT,
120
+ :stats, RD_KAFKA_EVENT_STATS,
121
+ :create_topics, RD_KAFKA_EVENT_CREATETOPICS_RESULT,
122
+ :delete_topics, RD_KAFKA_EVENT_DELETETOPICS_RESULT,
123
+ :create_partitions, RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT,
124
+ :alter_configs, RD_KAFKA_EVENT_ALTERCONFIGS_RESULT,
125
+ :describe_configs, RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT,
126
+ :oauth_bearer_token_refresh, RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH,
127
+ ]
128
+
129
+ RD_KAFKA_MSG_STATUS_NOT_PERSISTED = 0
130
+ RD_KAFKA_MSG_STATUS_POSSIBLY_PERSISTED = 1
131
+ RD_KAFKA_MSG_STATUS_PERSISTED = 2
132
+
133
+ # @see rdkafka.h rd_kafka_message_status
134
+ enum :message_status, [
135
+ :not_presisted, RD_KAFKA_MSG_STATUS_NOT_PERSISTED,
136
+ :possibly_persisted, RD_KAFKA_MSG_STATUS_POSSIBLY_PERSISTED,
137
+ :persisted, RD_KAFKA_MSG_STATUS_PERSISTED,
138
+ ]
139
+
140
+ # Flags for rd_kafka_produce, rd_kafka_producev, and
141
+ # rd_kafka_produce_batch.
142
+ #
143
+ # @see rdkafka.h
144
+ RD_KAFKA_MSG_F_FREE = 0x01
145
+ RD_KAFKA_MSG_F_COPY = 0x02
146
+ RD_KAFKA_MSG_F_BLOCK = 0x04
147
+ RD_KAFKA_MSG_F_PARTITION = 0x04
148
+
149
+ # Flags for rd_kafka_purge
150
+ #
151
+ # @see rdkafka.h
152
+ RD_KAFKA_PURGE_F_QUEUE = 0x01
153
+ RD_KAFKA_PURGE_F_INFLIGHT = 0x02
154
+ RD_KAFKA_PURGE_F_NON_BLOCKING = 0x04
155
+
156
+ # rd_kafka_producev va-arg vtype constants.
157
+ RD_KAFKA_VTYPE_END = 0
158
+ RD_KAFKA_VTYPE_TOPIC = 1
159
+ RD_KAFKA_VTYPE_RKT = 2
160
+ RD_KAFKA_VTYPE_PARTITION = 3
161
+ RD_KAFKA_VTYPE_VALUE = 4
162
+ RD_KAFKA_VTYPE_KEY = 5
163
+ RD_KAFKA_VTYPE_OPAQUE = 6
164
+ RD_KAFKA_VTYPE_MSGFLAGS = 7
165
+ RD_KAFKA_VTYPE_TIMESTAMP = 8
166
+ RD_KAFKA_VTYPE_HEADER = 9
167
+ RD_KAFKA_VTYPE_HEADERS = 10
168
+
169
+ # Use for partition when it should be assigned by the configured
170
+ # partitioner.
171
+ RD_KAFKA_PARTITION_UA = -1
172
+
173
+ # Enum of va-arg vtypes for calling rd_kafka_producev
174
+ #
175
+ # @see rdkafka.h rd_kafka_producev
176
+ enum :vtype, [
177
+ :end, RD_KAFKA_VTYPE_END,
178
+ :topic, RD_KAFKA_VTYPE_TOPIC,
179
+ :rkt, RD_KAFKA_VTYPE_RKT,
180
+ :partition, RD_KAFKA_VTYPE_PARTITION,
181
+ :value, RD_KAFKA_VTYPE_VALUE,
182
+ :key, RD_KAFKA_VTYPE_KEY,
183
+ :opaque, RD_KAFKA_VTYPE_OPAQUE,
184
+ :msgflags, RD_KAFKA_VTYPE_MSGFLAGS,
185
+ :timestamp, RD_KAFKA_VTYPE_TIMESTAMP,
186
+ :header, RD_KAFKA_VTYPE_HEADER,
187
+ :headers, RD_KAFKA_VTYPE_HEADERS,
188
+ ]
189
+
190
+ RD_KAFKA_ADMIN_OP_ANY = 0
191
+ RD_KAFKA_ADMIN_OP_CREATETOPICS = 1
192
+ RD_KAFKA_ADMIN_OP_DELETETOPICS = 2
193
+ RD_KAFKA_ADMIN_OP_CREATEPARTITIONS = 3
194
+ RD_KAFKA_ADMIN_OP_ALTERCONFIGS = 4
195
+ RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS = 5
196
+
197
+ enum :admin_operation, [
198
+ :any, RD_KAFKA_ADMIN_OP_ANY,
199
+ :create_topics, RD_KAFKA_ADMIN_OP_CREATETOPICS,
200
+ :delete_topics, RD_KAFKA_ADMIN_OP_DELETETOPICS,
201
+ :create_partitions, RD_KAFKA_ADMIN_OP_CREATEPARTITIONS,
202
+ :alter_configs, RD_KAFKA_ADMIN_OP_ALTERCONFIGS,
203
+ :describe_configs, RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS,
204
+ ]
205
+
206
+ RD_KAFKA_RESOURCE_UNKNOWN = 0
207
+ RD_KAFKA_RESOURCE_ANY = 1
208
+ RD_KAFKA_RESOURCE_TOPIC = 2
209
+ RD_KAFKA_RESOURCE_GROUP = 3
210
+ RD_KAFKA_RESOURCE_BROKER = 4
211
+
212
+ enum :resource_type, [
213
+ :unknown, RD_KAFKA_RESOURCE_UNKNOWN,
214
+ :any, RD_KAFKA_RESOURCE_ANY,
215
+ :topic, RD_KAFKA_RESOURCE_TOPIC,
216
+ :group, RD_KAFKA_RESOURCE_GROUP,
217
+ :broker, RD_KAFKA_RESOURCE_BROKER,
218
+ ]
219
+
220
+ RD_KAFKA_CONFIG_SOURCE_UNKNOWN_CONFIG = 0
221
+ RD_KAFKA_CONFIG_SOURCE_DYNAMIC_TOPIC_CONFIG = 1
222
+ RD_KAFKA_CONFIG_SOURCE_DYNAMIC_BROKER_CONFIG = 2
223
+ RD_KAFKA_CONFIG_SOURCE_DYNAMIC_DEFAULT_BROKER_CONFIG = 3
224
+ RD_KAFKA_CONFIG_SOURCE_STATIC_BROKER_CONFIG = 4
225
+ RD_KAFKA_CONFIG_SOURCE_DEFAULT_CONFIG = 5
226
+
227
+ enum :config_source, [
228
+ :unknown_config, RD_KAFKA_CONFIG_SOURCE_UNKNOWN_CONFIG,
229
+ :dynamic_topic_config, RD_KAFKA_CONFIG_SOURCE_DYNAMIC_TOPIC_CONFIG,
230
+ :dynamic_broker_config, RD_KAFKA_CONFIG_SOURCE_DYNAMIC_BROKER_CONFIG,
231
+ :dynamic_default_broker_config, RD_KAFKA_CONFIG_SOURCE_DYNAMIC_DEFAULT_BROKER_CONFIG,
232
+ :static_broker_config, RD_KAFKA_CONFIG_SOURCE_STATIC_BROKER_CONFIG,
233
+ :default_config, RD_KAFKA_CONFIG_SOURCE_DEFAULT_CONFIG,
234
+ ]
235
+
236
+ typedef :int, :timeout_ms
237
+ typedef :int32, :partition
238
+ typedef :int32, :broker_id
239
+ typedef :int64, :offset
240
+ typedef :string, :topic
241
+ typedef :pointer, :opaque
242
+
243
+ # Load types after enums and constants so they're able to reference them.
244
+ require "kafka/ffi/admin"
245
+ require "kafka/ffi/error"
246
+ require "kafka/ffi/event"
247
+ require "kafka/ffi/queue"
248
+ require "kafka/ffi/topic"
249
+ require "kafka/ffi/opaque"
250
+ require "kafka/ffi/client"
251
+ require "kafka/ffi/config"
252
+ require "kafka/ffi/message"
253
+ require "kafka/ffi/metadata"
254
+ require "kafka/ffi/group_list"
255
+ require "kafka/ffi/group_info"
256
+ require "kafka/ffi/group_member_info"
257
+ require "kafka/ffi/topic_config"
258
+ require "kafka/ffi/opaque_pointer"
259
+ require "kafka/ffi/topic_partition"
260
+ require "kafka/ffi/topic_partition_list"
261
+ require "kafka/ffi/topic_metadata"
262
+ require "kafka/ffi/broker_metadata"
263
+ require "kafka/ffi/partition_metadata"
264
+
265
+ # Errors
266
+ attach_function :rd_kafka_err2str, [:error_code], :string
267
+ attach_function :rd_kafka_err2name, [:error_code], :string
268
+ attach_function :rd_kafka_last_error, [], :error_code
269
+
270
+ # Version
271
+ attach_function :rd_kafka_version, [], :int
272
+ attach_function :rd_kafka_version_str, [], :string
273
+
274
+ # Client
275
+ attach_function :rd_kafka_new, [:kafka_type, Config, :pointer, :int], Client, blocking: true
276
+ attach_function :rd_kafka_type, [Client], :kafka_type
277
+ attach_function :rd_kafka_name, [Client], :string
278
+ attach_function :rd_kafka_memberid, [Client], :pointer
279
+ attach_function :rd_kafka_clusterid, [Client], :pointer
280
+ attach_function :rd_kafka_controllerid, [Client, :timeout_ms], :broker_id, blocking: true
281
+ attach_function :rd_kafka_default_topic_conf_dup, [Client], TopicConfig
282
+ attach_function :rd_kafka_conf, [Client], Config
283
+ attach_function :rd_kafka_poll, [Client, :timeout_ms], :int, blocking: true
284
+ attach_function :rd_kafka_outq_len, [Client], :int
285
+ attach_function :rd_kafka_brokers_add, [Client, :string], :int
286
+
287
+ # @note This function MUST ONLY be called from within a librdkafka
288
+ # callback.
289
+ # @note A callback may use this to force an immediate return to the caller
290
+ # that dispatched the callback without processing any further events.
291
+ attach_function :rd_kafka_yield, [Client], :void
292
+
293
+ # @test
294
+ attach_function :rd_kafka_pause_partitions, [Client, TopicPartitionList.by_ref], :error_code
295
+ # @test
296
+ attach_function :rd_kafka_resume_partitions, [Client, TopicPartitionList.by_ref], :error_code
297
+ # @test
298
+ attach_function :rd_kafka_query_watermark_offsets, [Client, :topic, :partition, :pointer, :pointer, :timeout_ms], :error_code, blocking: true
299
+ # @test
300
+ attach_function :rd_kafka_get_watermark_offsets, [Client, :topic, :partition, :pointer, :pointer], :error_code
301
+ # @test
302
+ attach_function :rd_kafka_offsets_for_times, [Client, TopicPartitionList.by_ref, :timeout_ms], :error_code, blocking: true
303
+
304
+ attach_function :rd_kafka_mem_free, [Client, :pointer], :void
305
+ attach_function :rd_kafka_destroy, [Client], :void, blocking: true
306
+
307
+ # Config
308
+ #
309
+ # NOTE: The following deprecated functions have not been implemented
310
+ # rd_kafka_conf_set_dr_cb
311
+ # rd_kafka_conf_set_default_topic_conf
312
+
313
+ attach_function :rd_kafka_conf_new, [], Config
314
+ attach_function :rd_kafka_conf_set, [Config, :string, :string, :pointer, :size_t], :config_result
315
+ attach_function :rd_kafka_conf_get, [Config, :string, :pointer, :pointer], :config_result
316
+
317
+ # @todo?
318
+ # attach_function :rd_kafka_conf_set_opaque, [Config, :pointer], :void
319
+ # attach_function :rd_kafka_opaque, [Client], :pointer
320
+
321
+ attach_function :rd_kafka_conf_dup, [Config], Config
322
+ attach_function :rd_kafka_conf_dup_filter, [Config, :size_t, :pointer], Config
323
+
324
+ # @param event_type is a bitmask of RD_KAFKA_EVENT_* constants.
325
+ attach_function :rd_kafka_conf_set_events, [Config, :event_type], :void
326
+
327
+ callback :background_event_cb, [Client, Event, :opaque], :void
328
+ attach_function :rd_kafka_conf_set_background_event_cb, [Config, :background_event_cb], :void
329
+
330
+ callback :dr_msg_cb, [Client, Message.by_ref, :pointer], :void
331
+ attach_function :rd_kafka_conf_set_dr_msg_cb, [Config, :dr_msg_cb], :void
332
+
333
+ callback :consume_cb, [Message.by_ref, :pointer], :void
334
+ attach_function :rd_kafka_conf_set_consume_cb, [Config, :consume_cb], :void
335
+
336
+ callback :rebalance_cb, [Client, :error_code, TopicPartitionList.by_ref, :pointer], :void
337
+ attach_function :rd_kafka_conf_set_rebalance_cb, [Config, :rebalance_cb], :void
338
+
339
+ callback :offset_commit_cb, [Client, :error_code, TopicPartitionList.by_ref, :pointer], :void
340
+ attach_function :rd_kafka_conf_set_offset_commit_cb, [Config, :offset_commit_cb], :void
341
+
342
+ callback :error_cb, [Client, :error_code, :string, :pointer], :void
343
+ attach_function :rd_kafka_conf_set_error_cb, [Config, :error_cb], :void
344
+
345
+ callback :throttle_cb, [Client, :string, :broker_id, :int, :pointer], :void
346
+ attach_function :rd_kafka_conf_set_throttle_cb, [Config, :throttle_cb], :void
347
+
348
+ callback :log_cb, [Client, :int, :string, :string], :void
349
+ attach_function :rd_kafka_conf_set_log_cb, [Config, :log_cb], :void
350
+
351
+ callback :stats_cb, [Client, :string, :size_t, :pointer], :void
352
+ attach_function :rd_kafka_conf_set_stats_cb, [Config, :stats_cb], :void
353
+
354
+ callback :oauth_bearer_token_refresh_cb, [Client, :string, :pointer], :void
355
+ attach_function :rd_kafka_conf_set_oauthbearer_token_refresh_cb, [Config, :oauth_bearer_token_refresh_cb], :void
356
+
357
+ callback :socket_cb, [:int, :int, :int, :pointer], :int
358
+ attach_function :rd_kafka_conf_set_socket_cb, [Config, :socket_cb], :void
359
+
360
+ # @todo first :pointer is to struct sockaddr
361
+ callback :connect_cb, [:int, :pointer, :int, :string, :pointer], :int
362
+ attach_function :rd_kafka_conf_set_connect_cb, [Config, :connect_cb], :void
363
+
364
+ callback :closesocket_cb, [:int, :pointer], :int
365
+ attach_function :rd_kafka_conf_set_closesocket_cb, [Config, :closesocket_cb], :void
366
+
367
+ # @test
368
+ if !::FFI::Platform.windows?
369
+ callback :open_cb, [:string, :int, :mode_t, :pointer], :int
370
+ attach_function :rd_kafka_conf_set_open_cb, [Config, :open_cb], :void
371
+ end
372
+
373
+ # @test
374
+ callback :ssl_cert_verify_cb, [Client, :string, :broker_id, :pointer, :int, :string, :size_t, :string, :size_t, :pointer], :int
375
+ attach_function :rd_kafka_conf_set_ssl_cert_verify_cb, [Config, :ssl_cert_verify_cb], :config_result
376
+
377
+ attach_function :rd_kafka_conf_set_ssl_cert, [Config, :cert_type, :cert_enc, :buffer_in, :size_t, :pointer, :size_t], :config_result
378
+ # :rd_kafka_conf_set_opaque
379
+
380
+ # NOTE: Never call rd_kafka_conf_destroy on a Config that has been passed
381
+ # to rd_kafka_new as librdkafka takes ownership at that point.
382
+ attach_function :rd_kafka_conf_destroy, [Config], :void
383
+
384
+ # Topic Config
385
+ attach_function :rd_kafka_topic_conf_new, [], TopicConfig
386
+ attach_function :rd_kafka_topic_conf_set, [TopicConfig, :string, :string, :pointer, :size_t], :config_result
387
+ attach_function :rd_kafka_topic_conf_get, [TopicConfig, :string, :pointer, :pointer], :config_result
388
+ attach_function :rd_kafka_topic_conf_dup, [TopicConfig], TopicConfig
389
+
390
+ callback :topic_partitioner_cb, [Topic, :string, :size_t, :int32, :pointer, :pointer], :partition
391
+ attach_function :rd_kafka_topic_conf_set_partitioner_cb, [TopicConfig, :topic_partitioner_cb], :void
392
+
393
+ attach_function :rd_kafka_topic_conf_destroy, [TopicConfig], :void
394
+
395
+ # Message
396
+ attach_function :rd_kafka_message_timestamp, [Message.by_ref, :pointer], :int64
397
+ attach_function :rd_kafka_message_latency, [Message.by_ref], :int64
398
+ attach_function :rd_kafka_message_status, [Message.by_ref], :message_status
399
+ attach_function :rd_kafka_message_headers, [Message.by_ref, Message::Header], :error_code
400
+ attach_function :rd_kafka_message_detach_headers, [Message.by_ref, Message::Header], :error_code
401
+ attach_function :rd_kafka_message_set_headers, [Message.by_ref, Message::Header], :void
402
+ attach_function :rd_kafka_message_destroy, [Message.by_ref], :void
403
+
404
+ # Message::Header
405
+ attach_function :rd_kafka_headers_new, [:size_t], Message::Header
406
+ attach_function :rd_kafka_headers_copy, [Message::Header], Message::Header
407
+ attach_function :rd_kafka_header_cnt, [Message::Header], :size_t
408
+ attach_function :rd_kafka_header_add, [Message::Header, :string, :size_t, :string, :size_t], :error_code
409
+ attach_function :rd_kafka_header_remove, [Message::Header, :string], :error_code
410
+ attach_function :rd_kafka_header_get, [Message::Header, :size_t, :string, :pointer, :pointer], :error_code
411
+ attach_function :rd_kafka_header_get_all, [Message::Header, :size_t, :pointer, :pointer, :pointer], :error_code
412
+ attach_function :rd_kafka_header_get_last, [Message::Header, :string, :pointer, :pointer], :error_code
413
+ attach_function :rd_kafka_headers_destroy, [Message::Header], :void
414
+
415
+ # Consumer
416
+
417
+ ## High Level Consumer API
418
+ attach_function :rd_kafka_subscribe, [Consumer, TopicPartitionList.by_ref], :error_code
419
+ attach_function :rd_kafka_unsubscribe, [Consumer], :error_code
420
+ attach_function :rd_kafka_subscription, [Consumer, :pointer], :error_code
421
+ attach_function :rd_kafka_consumer_poll, [Consumer, :timeout_ms], Message.by_ref, blocking: true
422
+ attach_function :rd_kafka_poll_set_consumer, [Consumer], :error_code
423
+ attach_function :rd_kafka_consumer_close, [Consumer], :error_code, blocking: true
424
+
425
+ attach_function :rd_kafka_assign, [Consumer, TopicPartitionList.by_ref], :error_code
426
+ attach_function :rd_kafka_assignment, [Consumer, :pointer], :error_code
427
+ attach_function :rd_kafka_commit, [Consumer, TopicPartitionList.by_ref, :bool], :error_code, blocking: true
428
+ # @test
429
+ attach_function :rd_kafka_commit_message, [Consumer, Message.by_ref, :bool], :error_code, blocking: true
430
+
431
+ # @todo?
432
+ # attach_function :rd_kafka_commit_queue, [Consumer], TopicPartitionList.by_ref, Queue, :commit_queue_cb, :pointer], :error_code
433
+
434
+ attach_function :rd_kafka_committed, [Consumer, TopicPartitionList.by_ref, :timeout_ms], :error_code, blocking: true
435
+
436
+ # @todo
437
+ # attach_function :rd_kafka_position, [Consumer, TopicPartitionList.by_ref], :error_code
438
+
439
+ ## Legacy Simple Consumer API
440
+ attach_function :rd_kafka_consume_start, [Topic, :partition, :offset], :int
441
+ attach_function :rd_kafka_consume_start_queue, [Topic, :partition, :offset, Queue], :int
442
+ attach_function :rd_kafka_consume_stop, [Topic, :partition], :int
443
+ attach_function :rd_kafka_consume, [Topic, :partition, :timeout_ms], Message.by_ref
444
+ attach_function :rd_kafka_consume_batch, [Topic, :partition, :timeout_ms, :pointer, :size_t], :ssize_t
445
+ attach_function :rd_kafka_consume_callback, [Topic, :partition, :timeout_ms, :consume_cb, :pointer], :int
446
+
447
+ ### Simple Consumer Queue API
448
+ attach_function :rd_kafka_consume_queue, [Queue, :timeout_ms], Message.by_ref
449
+ attach_function :rd_kafka_consume_batch_queue, [Queue, :timeout_ms, :pointer, :size_t], :ssize_t
450
+ attach_function :rd_kafka_consume_callback_queue, [Queue, :timeout_ms, :consume_cb, :pointer], :int
451
+
452
+ ### Simple Consumer Topic + Partition API
453
+ attach_function :rd_kafka_offset_store, [Topic, :partition, :offset], :error_code
454
+ attach_function :rd_kafka_offsets_store, [Client, TopicPartitionList.by_ref], :error_code
455
+
456
+ # Producer
457
+ attach_function :rd_kafka_produce, [Topic, :partition, :int, :pointer, :size_t, :string, :size_t, :pointer], :int
458
+ attach_function :rd_kafka_producev, [Producer, :varargs], :error_code
459
+ attach_function :rd_kafka_produce_batch, [Topic, :partition, :int, Message.by_ref, :int], :int
460
+ attach_function :rd_kafka_flush, [Producer, :timeout_ms], :error_code, blocking: true
461
+ attach_function :rd_kafka_purge, [Producer, :int], :error_code, blocking: true
462
+
463
+ # Metadata
464
+ attach_function :rd_kafka_metadata, [Client, :bool, Topic, :pointer, :timeout_ms], :error_code
465
+ attach_function :rd_kafka_metadata_destroy, [Metadata.by_ref], :void
466
+
467
+ # Group List
468
+ attach_function :rd_kafka_list_groups, [Client, :string, :pointer, :timeout_ms], :error_code
469
+ attach_function :rd_kafka_group_list_destroy, [GroupList.by_ref], :void
470
+
471
+ # Queue
472
+ attach_function :rd_kafka_queue_new, [Client], Queue
473
+ attach_function :rd_kafka_queue_poll, [Queue, :timeout_ms], Event
474
+ attach_function :rd_kafka_queue_get_main, [Client], Queue
475
+ attach_function :rd_kafka_queue_get_consumer, [Consumer], Queue
476
+ attach_function :rd_kafka_queue_get_partition, [Consumer, :topic, :partition], Queue
477
+ attach_function :rd_kafka_queue_get_background, [Client], Queue
478
+ attach_function :rd_kafka_queue_forward, [Queue, Queue], :void
479
+ attach_function :rd_kafka_set_log_queue, [Client, Queue], :error_code
480
+ attach_function :rd_kafka_queue_length, [Queue], :size_t
481
+ # :rd_kafka_queue_io_event_enable
482
+ # :rd_kafka_queue_cb_event_enable
483
+ attach_function :rd_kafka_queue_destroy, [Queue], :void
484
+
485
+ # Event
486
+ attach_function :rd_kafka_event_type, [Event], :event_type
487
+ attach_function :rd_kafka_event_name, [Event], :string
488
+
489
+ attach_function :rd_kafka_event_message_next, [Event], Message.by_ref
490
+ attach_function :rd_kafka_event_message_array, [Event, :pointer, :size_t], :size_t
491
+ attach_function :rd_kafka_event_message_count, [Event], :size_t
492
+ attach_function :rd_kafka_event_config_string, [Event], :string
493
+ attach_function :rd_kafka_event_error, [Event], :error_code
494
+ attach_function :rd_kafka_event_error_string, [Event], :string
495
+ attach_function :rd_kafka_event_error_is_fatal, [Event], :bool
496
+ # :rd_kafka_event_opaque
497
+ attach_function :rd_kafka_event_log, [Event, :pointer, :pointer, :pointer], :int
498
+ attach_function :rd_kafka_event_stats, [Event], :string
499
+ attach_function :rd_kafka_event_topic_partition_list, [Event], TopicPartitionList.by_ref
500
+ attach_function :rd_kafka_event_topic_partition, [Event], TopicPartition.by_ref
501
+ attach_function :rd_kafka_event_destroy, [Event], :void
502
+
503
+ # Event casting
504
+ # Each of these functions will type check the Event to see if it is the
505
+ # desired type, returning nil if it is not.
506
+ attach_function :rd_kafka_event_CreateTopics_result, [Event], Event
507
+ attach_function :rd_kafka_event_DeleteTopics_result, [Event], Event
508
+ attach_function :rd_kafka_event_CreatePartitions_result, [Event], Event
509
+ attach_function :rd_kafka_event_AlterConfigs_result, [Event], Event
510
+ attach_function :rd_kafka_event_DescribeConfigs_result, [Event], Event
511
+
512
+ # Topics
513
+ attach_function :rd_kafka_topic_new, [Client, :topic, TopicConfig], Topic
514
+ attach_function :rd_kafka_topic_name, [Topic], :topic
515
+ attach_function :rd_kafka_seek, [Topic, :partition, :offset, :timeout_ms], :error_code, blocking: true
516
+
517
+ # @note May only be called inside a topic_partitioner_cb
518
+ attach_function :rd_kafka_topic_partition_available, [Topic, :partition], :bool
519
+
520
+ # @todo?
521
+ # attach_function :rd_kafka_topic_opaque, [Topic], :pointer
522
+ # @todo
523
+ attach_function :rd_kafka_topic_destroy, [Topic], :void
524
+
525
+ # Topic Partition
526
+
527
+ # NOTE: Must never by called for elements in a TopicPartitionList. Mostly
528
+ # here for completeness since it likely never makes sense to call.
529
+ attach_function :rd_kafka_topic_partition_destroy, [TopicPartition.by_ref], :void
530
+
531
+ # Topic Partition List
532
+ attach_function :rd_kafka_topic_partition_list_new, [:int32], TopicPartitionList.by_ref
533
+ attach_function :rd_kafka_topic_partition_list_add, [TopicPartitionList.by_ref, :topic, :partition], TopicPartition.by_ref
534
+ attach_function :rd_kafka_topic_partition_list_add_range, [TopicPartitionList.by_ref, :topic, :int32, :int32], :void
535
+ attach_function :rd_kafka_topic_partition_list_del, [TopicPartitionList.by_ref, :topic, :partition], :int
536
+ attach_function :rd_kafka_topic_partition_list_del_by_idx, [TopicPartitionList.by_ref, :int], :int
537
+ attach_function :rd_kafka_topic_partition_list_copy, [TopicPartitionList.by_ref], TopicPartitionList.by_ref
538
+ attach_function :rd_kafka_topic_partition_list_set_offset, [TopicPartitionList.by_ref, :topic, :partition, :offset], :error_code
539
+ attach_function :rd_kafka_topic_partition_list_find, [TopicPartitionList.by_ref, :topic, :partition], TopicPartition.by_ref
540
+
541
+ callback :topic_list_cmp_func, [TopicPartition.by_ref, TopicPartition.by_ref, :pointer], :int
542
+ attach_function :rd_kafka_topic_partition_list_sort, [TopicPartitionList.by_ref, :topic_list_cmp_func, :pointer ], :void
543
+
544
+ attach_function :rd_kafka_topic_partition_list_destroy, [TopicPartitionList.by_ref], :void
545
+
546
+ # Admin Commands
547
+
548
+ ## AdminOptions
549
+ attach_function :rd_kafka_AdminOptions_new, [Client, :admin_operation], Admin::AdminOptions
550
+ attach_function :rd_kafka_AdminOptions_set_request_timeout, [Admin::AdminOptions, :timeout_ms, :pointer, :size_t], :error_code
551
+ attach_function :rd_kafka_AdminOptions_set_operation_timeout, [Admin::AdminOptions, :timeout_ms, :pointer, :size_t], :error_code
552
+ attach_function :rd_kafka_AdminOptions_set_validate_only, [Admin::AdminOptions, :bool, :pointer, :size_t], :error_code
553
+ attach_function :rd_kafka_AdminOptions_set_broker, [Admin::AdminOptions, :broker_id, :pointer, :size_t], :error_code
554
+ # :rd_kafka_AdminOptions_set_opaque
555
+ attach_function :rd_kafka_AdminOptions_destroy, [Admin::AdminOptions], :void
556
+
557
+ ## DescribeConfigs
558
+ attach_function :rd_kafka_DescribeConfigs, [Client, :pointer, :size_t, Admin::AdminOptions, Queue], :void
559
+ attach_function :rd_kafka_DescribeConfigs_result_resources, [Event, :pointer], :pointer
560
+
561
+ ## AlterConfigs
562
+ attach_function :rd_kafka_AlterConfigs, [Client, :pointer, :size_t, Admin::AdminOptions, Queue], :void
563
+ attach_function :rd_kafka_AlterConfigs_result_resources, [Event, :pointer], :pointer
564
+
565
+ ## Resource Type (enum)
566
+ attach_function :rd_kafka_ResourceType_name, [:resource_type], :string
567
+
568
+ ## ConfigResource
569
+ attach_function :rd_kafka_ConfigResource_new, [:resource_type, :string], Admin::ConfigResource
570
+ attach_function :rd_kafka_ConfigResource_set_config, [Admin::ConfigResource, :string, :string], :error_code
571
+ attach_function :rd_kafka_ConfigResource_configs, [Admin::ConfigResource, :pointer], :pointer
572
+ attach_function :rd_kafka_ConfigResource_type, [Admin::ConfigResource], :resource_type
573
+ attach_function :rd_kafka_ConfigResource_name, [Admin::ConfigResource], :string
574
+ attach_function :rd_kafka_ConfigResource_error, [Admin::ConfigResource], :error_code
575
+ attach_function :rd_kafka_ConfigResource_error_string, [Admin::ConfigResource], :string
576
+ attach_function :rd_kafka_ConfigResource_destroy, [Admin::ConfigResource], :void
577
+ attach_function :rd_kafka_ConfigResource_destroy_array, [:pointer, :size_t], :void
578
+
579
+ ## ConfigEntry
580
+ attach_function :rd_kafka_ConfigEntry_name, [Admin::ConfigEntry], :string
581
+ attach_function :rd_kafka_ConfigEntry_value, [Admin::ConfigEntry], :string
582
+ attach_function :rd_kafka_ConfigEntry_source, [Admin::ConfigEntry], :config_source
583
+ attach_function :rd_kafka_ConfigEntry_is_read_only, [Admin::ConfigEntry], :int
584
+ attach_function :rd_kafka_ConfigEntry_is_default, [Admin::ConfigEntry], :int
585
+ attach_function :rd_kafka_ConfigEntry_is_sensitive, [Admin::ConfigEntry], :int
586
+ attach_function :rd_kafka_ConfigEntry_is_synonym, [Admin::ConfigEntry], :int
587
+ attach_function :rd_kafka_ConfigEntry_synonyms, [Admin::ConfigEntry, :pointer], :pointer
588
+
589
+ ## ConfigSource
590
+ attach_function :rd_kafka_ConfigSource_name, [:config_source], :string
591
+
592
+ ## Create Topics / NewTopic
593
+ attach_function :rd_kafka_CreateTopics, [Client, :pointer, :size_t, Admin::AdminOptions, Queue], :void, blocking: true
594
+ attach_function :rd_kafka_CreateTopics_result_topics, [Event, :pointer], :pointer
595
+ attach_function :rd_kafka_NewTopic_new, [:topic, :int, :int, :pointer, :size_t], Admin::NewTopic
596
+ attach_function :rd_kafka_NewTopic_set_replica_assignment, [Admin::NewTopic, :partition, :pointer, :size_t, :pointer, :size_t], :error_code
597
+ attach_function :rd_kafka_NewTopic_set_config, [Admin::NewTopic, :string, :string], :error_code
598
+ attach_function :rd_kafka_NewTopic_destroy, [Admin::NewTopic], :void
599
+ attach_function :rd_kafka_NewTopic_destroy_array, [:pointer, :size_t], :void
600
+
601
+ # TopicResult
602
+ attach_function :rd_kafka_topic_result_error, [Admin::TopicResult], :error_code
603
+ attach_function :rd_kafka_topic_result_error_string, [Admin::TopicResult], :string
604
+ attach_function :rd_kafka_topic_result_name, [Admin::TopicResult], :topic
605
+
606
+ # DeleteTopics / DeleteTopic
607
+ attach_function :rd_kafka_DeleteTopics, [Client, :pointer, :size_t, Admin::AdminOptions, Queue], :void
608
+ attach_function :rd_kafka_DeleteTopics_result_topics, [Event, :pointer], :pointer
609
+ attach_function :rd_kafka_DeleteTopic_new, [:topic], Admin::DeleteTopic
610
+ attach_function :rd_kafka_DeleteTopic_destroy, [Admin::DeleteTopic], :void
611
+ attach_function :rd_kafka_DeleteTopic_destroy_array, [:pointer, :size_t], :void
612
+
613
+ # CreatePartitions / NewPartitions
614
+ attach_function :rd_kafka_CreatePartitions, [Client, :pointer, :size_t, Admin::AdminOptions, Queue], :void
615
+ attach_function :rd_kafka_CreatePartitions_result_topics, [Event, :pointer], :pointer
616
+
617
+ attach_function :rd_kafka_NewPartitions_new, [:topic, :size_t, :pointer, :size_t], Admin::NewPartitions
618
+
619
+ attach_function :rd_kafka_NewPartitions_set_replica_assignment, [Admin::NewPartitions, :partition, :pointer, :size_t, :pointer, :size_t], :error_code
620
+
621
+ attach_function :rd_kafka_NewPartitions_destroy, [Admin::NewPartitions], :void
622
+ attach_function :rd_kafka_NewPartitions_destroy_array, [:pointer, :size_t], :void
623
+ end
624
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kafka
4
+ # @private
5
+ #
6
+ # Niceties around a Thread to call Client#poll at a regular interval. This is
7
+ # required in the Producer and optional in the Consumer when
8
+ # poll_set_consumer is not used.
9
+ class Poller < Thread
10
+ def initialize(client)
11
+ @client = client
12
+ @run = true
13
+
14
+ self.abort_on_exception = true
15
+
16
+ super do
17
+ while @run
18
+ @client.poll
19
+ end
20
+ end
21
+ end
22
+
23
+ def stop
24
+ @run = false
25
+ join
26
+ end
27
+ end
28
+ end