Class: Rdkafka::Consumer
- Inherits:
-
Object
- Object
- Rdkafka::Consumer
- Includes:
- Enumerable, Helpers::OAuth, Helpers::Time
- Defined in:
- lib/rdkafka/consumer.rb,
lib/rdkafka/consumer/headers.rb,
lib/rdkafka/consumer/message.rb,
lib/rdkafka/consumer/partition.rb,
lib/rdkafka/consumer/topic_partition_list.rb
Overview
A consumer of Kafka messages. It uses the high-level consumer approach where the Kafka brokers automatically assign partitions and load balance partitions over consumers that have the same ‘:“group.id”` set in their configuration.
To create a consumer set up a Config and call consumer on that. It is mandatory to set ‘:“group.id”` in the configuration.
Consumer implements ‘Enumerable`, so you can use `each` to consume messages, or for example `each_slice` to consume batches of messages.
Defined Under Namespace
Modules: Headers Classes: Message, Partition, TopicPartitionList
Instance Method Summary collapse
-
#assign(list) ⇒ Object
Atomic assignment of partitions to consume.
-
#assignment ⇒ TopicPartitionList
Returns the current partition assignment.
-
#assignment_lost? ⇒ Boolean
True if our current assignment has been lost involuntarily.
-
#close ⇒ nil
Close this consumer.
-
#closed? ⇒ Boolean
Whether this consumer has closed.
-
#cluster_id ⇒ String?
Returns the ClusterId as reported in broker metadata.
-
#commit(list = nil, async = false) ⇒ nil
Manually commit the current offsets of this consumer.
-
#committed(list = nil, timeout_ms = Defaults::CONSUMER_COMMITTED_TIMEOUT_MS) ⇒ TopicPartitionList
Return the current committed offset per partition for this consumer group.
-
#consumer_group_metadata_pointer ⇒ Object
Returns pointer to the consumer group metadata.
-
#each(timeout_ms: Defaults::CONSUMER_POLL_TIMEOUT_MS) {|message| ... } ⇒ nil
Poll for new messages and yield for each received one.
-
#each_batch(max_items: 100, bytes_threshold: Float::INFINITY, timeout_ms: 250, yield_on_error: false, &block) ⇒ Object
deprecated
Deprecated.
This method has been removed due to data consistency concerns
-
#enable_background_queue_io_events(fd, payload = "\x01") ⇒ nil
Enable IO event notifications for background events.
-
#enable_queue_io_events(fd, payload = "\x01") ⇒ nil
Enable IO event notifications for fiber scheduler integration When the consumer queue has messages, librdkafka will write to your FD.
-
#events_poll(timeout_ms = Defaults::CONSUMER_EVENTS_POLL_TIMEOUT_MS) ⇒ Object
Polls the main rdkafka queue (not the consumer one).
-
#events_poll_nb(timeout_ms = 0) ⇒ Integer
Polls the main rdkafka queue without releasing the GVL (Global VM Lock).
-
#events_poll_nb_each {|count| ... } ⇒ nil
Polls for events in a non-blocking loop, yielding the count after each iteration.
-
#initialize(native_kafka) ⇒ Consumer
constructor
A new instance of Consumer.
-
#lag(topic_partition_list, watermark_timeout_ms = Defaults::CONSUMER_LAG_TIMEOUT_MS) ⇒ Hash{String => Hash{Integer => Integer}}
Calculate the consumer lag per partition for the provided topic partition list.
-
#member_id ⇒ String?
Returns this client’s broker-assigned group member id.
-
#name ⇒ String
Consumer name.
-
#offsets_for_times(list, timeout_ms = Defaults::CONSUMER_OFFSETS_FOR_TIMES_TIMEOUT_MS) ⇒ TopicPartitionList
Lookup offset for the given partitions by timestamp.
-
#pause(list) ⇒ nil
Pause producing or consumption for the provided list of partitions.
-
#poll(timeout_ms) ⇒ Message?
Poll for the next message on one of the subscribed topics.
-
#poll_batch(timeout_ms, max_items: 100) ⇒ Array<Message>
Poll for a batch of messages from the consumer queue in a single FFI call.
-
#poll_batch_nb(timeout_ms = 0, max_items: 100) ⇒ Array<Message>
Poll for a batch of messages without releasing the GVL (Global VM Lock).
-
#poll_nb(timeout_ms = 0) ⇒ Message?
Poll for the next message without releasing the GVL (Global VM Lock).
-
#poll_nb_each {|message| ... } ⇒ nil
Polls for messages in a non-blocking loop, yielding each message to the caller.
-
#position(list = nil) ⇒ TopicPartitionList
Return the current positions (offsets) for topics and partitions.
-
#query_watermark_offsets(topic, partition, timeout_ms = Defaults::CONSUMER_QUERY_WATERMARK_TIMEOUT_MS) ⇒ Integer
Query broker for low (oldest/beginning) and high (newest/end) offsets for a partition.
-
#resume(list) ⇒ nil
Resumes producing consumption for the provided list of partitions.
-
#seek(message) ⇒ nil
Seek to a particular message.
-
#seek_by(topic, partition, offset) ⇒ nil
Seek to a particular message by providing the topic, partition and offset.
-
#start ⇒ Object
Starts the native Kafka polling thread and kicks off the init polling.
-
#store_offset(message) ⇒ nil
Store offset of a message to be used in the next commit of this consumer.
-
#subscribe(*topics) ⇒ nil
Subscribes to one or more topics letting Kafka handle partition assignments.
-
#subscription ⇒ TopicPartitionList
Returns the current subscription to topics and partitions.
-
#unsubscribe ⇒ nil
Unsubscribe from all subscribed topics.
Methods included from Helpers::OAuth
#oauthbearer_set_token, #oauthbearer_set_token_failure
Methods included from Helpers::Time
#monotonic_now, #monotonic_now_ms
Constructor Details
#initialize(native_kafka) ⇒ Consumer
Returns a new instance of Consumer.
20 21 22 23 24 25 |
# File 'lib/rdkafka/consumer.rb', line 20 def initialize(native_kafka) @native_kafka = native_kafka # Makes sure, that native kafka gets closed before it gets GCed by Ruby ObjectSpace.define_finalizer(self, native_kafka.finalizer) end |
Instance Method Details
#assign(list) ⇒ Object
Atomic assignment of partitions to consume
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rdkafka/consumer.rb', line 325 def assign(list) closed_consumer_check(__method__) unless list.is_a?(TopicPartitionList) raise TypeError.new("list has to be a TopicPartitionList") end tpl = list.to_native_tpl begin response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_assign(inner, tpl) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response, "Error assigning '#{list.to_h}'") end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) end end |
#assignment ⇒ TopicPartitionList
Returns the current partition assignment.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/rdkafka/consumer.rb', line 350 def assignment closed_consumer_check(__method__) ptr = FFI::MemoryPointer.new(:pointer) response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_assignment(inner, ptr) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end tpl = ptr.read_pointer if !tpl.null? begin Rdkafka::Consumer::TopicPartitionList.from_native_tpl(tpl) ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy tpl end end ensure ptr&.free end |
#assignment_lost? ⇒ Boolean
Returns true if our current assignment has been lost involuntarily.
375 376 377 378 379 380 381 |
# File 'lib/rdkafka/consumer.rb', line 375 def assignment_lost? closed_consumer_check(__method__) @native_kafka.with_inner do |inner| !Rdkafka::Bindings.rd_kafka_assignment_lost(inner).zero? end end |
#close ⇒ nil
Close this consumer
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rdkafka/consumer.rb', line 180 def close return if closed? ObjectSpace.undefine_finalizer(self) @native_kafka.synchronize do |inner| Rdkafka::Bindings.rd_kafka_consumer_close(inner) if @consumer_queue Rdkafka::Bindings.rd_kafka_queue_destroy(@consumer_queue) @consumer_queue = nil end end @native_kafka.close end |
#closed? ⇒ Boolean
Whether this consumer has closed
197 198 199 |
# File 'lib/rdkafka/consumer.rb', line 197 def closed? @native_kafka.closed? end |
#cluster_id ⇒ String?
Returns the ClusterId as reported in broker metadata.
513 514 515 516 517 518 |
# File 'lib/rdkafka/consumer.rb', line 513 def cluster_id closed_consumer_check(__method__) @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_clusterid(inner) end end |
#commit(list = nil, async = false) ⇒ nil
Manually commit the current offsets of this consumer.
To use this set ‘enable.auto.commit`to `false` to disable automatic triggering of commits.
If ‘enable.auto.offset.store` is set to `true` the offset of the last consumed message for every partition is used. If set to `false` you can use #store_offset to indicate when a message has been fully processed.
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/rdkafka/consumer.rb', line 655 def commit(list = nil, async = false) closed_consumer_check(__method__) if !list.nil? && !list.is_a?(TopicPartitionList) raise TypeError.new("list has to be nil or a TopicPartitionList") end tpl = list&.to_native_tpl begin response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_commit(inner, tpl, async) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) if tpl end end |
#committed(list = nil, timeout_ms = Defaults::CONSUMER_COMMITTED_TIMEOUT_MS) ⇒ TopicPartitionList
Return the current committed offset per partition for this consumer group. The offset field of each requested partition will either be set to stored offset or to -1001 in case there was no stored offset for that partition.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/rdkafka/consumer.rb', line 392 def committed(list = nil, timeout_ms = Defaults::CONSUMER_COMMITTED_TIMEOUT_MS) closed_consumer_check(__method__) if list.nil? list = assignment elsif !list.is_a?(TopicPartitionList) raise TypeError.new("list has to be nil or a TopicPartitionList") end tpl = list.to_native_tpl begin response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_committed(inner, tpl, timeout_ms) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end TopicPartitionList.from_native_tpl(tpl) ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) end end |
#consumer_group_metadata_pointer ⇒ Object
This pointer needs to be removed with ‘#rd_kafka_consumer_group_metadata_destroy`
Returns pointer to the consumer group metadata. It is used only in the context of exactly-once-semantics in transactions, this is why it is never remapped to Ruby
This API is not usable by itself from Ruby
969 970 971 972 973 974 975 |
# File 'lib/rdkafka/consumer.rb', line 969 def closed_consumer_check(__method__) @native_kafka.with_inner do |inner| Bindings.(inner) end end |
#each(timeout_ms: Defaults::CONSUMER_POLL_TIMEOUT_MS) {|message| ... } ⇒ nil
Poll for new messages and yield for each received one. Iteration will end when the consumer is closed.
If ‘enable.partition.eof` is turned on in the config this will raise an error when an eof is reached, so you probably want to disable that when using this method of iteration.
923 924 925 926 927 928 929 930 931 932 933 934 |
# File 'lib/rdkafka/consumer.rb', line 923 def each(timeout_ms: Defaults::CONSUMER_POLL_TIMEOUT_MS) loop do = poll(timeout_ms) if yield() elsif closed? break else next end end end |
#each_batch(max_items: 100, bytes_threshold: Float::INFINITY, timeout_ms: 250, yield_on_error: false, &block) ⇒ Object
This method has been removed due to data consistency concerns
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'lib/rdkafka/consumer.rb', line 943 def each_batch(max_items: 100, bytes_threshold: Float::INFINITY, timeout_ms: 250, yield_on_error: false, &block) raise NotImplementedError, <<~ERROR `each_batch` has been removed due to data consistency concerns. This method was removed because it did not properly handle partition reassignments, which could lead to processing messages from partitions that were no longer owned by this consumer, resulting in duplicate message processing and data inconsistencies. Recommended alternatives: 1. Implement your own batching logic using rebalance callbacks to properly handle partition revocations and ensure message processing correctness. 2. Use a high-level batching library that supports proper partition reassignment handling out of the box (such as the Karafka framework). ERROR end |
#enable_background_queue_io_events(fd, payload = "\x01") ⇒ nil
Enable IO event notifications for background events
77 78 79 |
# File 'lib/rdkafka/consumer.rb', line 77 def enable_background_queue_io_events(fd, payload = "\x01") @native_kafka.enable_background_queue_io_events(fd, payload) end |
#enable_queue_io_events(fd, payload = "\x01") ⇒ nil
Enable IO event notifications for fiber scheduler integration When the consumer queue has messages, librdkafka will write to your FD
68 69 70 |
# File 'lib/rdkafka/consumer.rb', line 68 def enable_queue_io_events(fd, payload = "\x01") @native_kafka.enable_main_queue_io_events(fd, payload) end |
#events_poll(timeout_ms = Defaults::CONSUMER_EVENTS_POLL_TIMEOUT_MS) ⇒ Object
This method technically should be called ‘#poll` and the current `#poll` should be called `#consumer_poll` though we keep the current naming convention to make it backward compatible.
Polls the main rdkafka queue (not the consumer one). Do NOT use it if ‘consumer_poll_set`
was set to `true`.
Events will cause application-provided callbacks to be called.
Events (in the context of the consumer):
- error callbacks
- stats callbacks
- any other callbacks supported by librdkafka that are not part of the consumer_poll, that
would have a callback configured and activated.
This method needs to be called at regular intervals to serve any queued callbacks waiting to be called. When in use, does NOT replace ‘#poll` but needs to run complementary with it.
767 768 769 770 771 |
# File 'lib/rdkafka/consumer.rb', line 767 def events_poll(timeout_ms = Defaults::CONSUMER_EVENTS_POLL_TIMEOUT_MS) @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_poll(inner, timeout_ms) end end |
#events_poll_nb(timeout_ms = 0) ⇒ Integer
Polls the main rdkafka queue without releasing the GVL (Global VM Lock).
This is more efficient than regular events_poll for non-blocking poll(0) calls, particularly useful in fiber scheduler contexts where GVL release/reacquire overhead is wasteful since we don’t expect to wait.
783 784 785 786 787 |
# File 'lib/rdkafka/consumer.rb', line 783 def events_poll_nb(timeout_ms = 0) @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_poll_nb(inner, timeout_ms) end end |
#events_poll_nb_each {|count| ... } ⇒ nil
This method holds the inner lock until the queue is empty or ‘:stop` is returned. Other consumer operations will wait until this method returns.
This method is thread-safe as it uses @native_kafka.with_inner synchronization
Do NOT use this if ‘consumer_poll_set` was set to `true`
Polls for events in a non-blocking loop, yielding the count after each iteration.
This method processes events (stats, errors, etc.) in a single GVL/mutex session, which is more efficient than repeated individual polls. It uses non-blocking polls internally (no GVL release between polls).
Yields the count of events processed after each poll iteration, allowing the caller to implement timeout or other termination logic by returning ‘:stop`.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rdkafka/consumer.rb', line 109 def events_poll_nb_each closed_consumer_check(__method__) @native_kafka.with_inner do |inner| loop do count = Rdkafka::Bindings.rd_kafka_poll_nb(inner, 0) break if count.zero? break if yield(count) == :stop end end end |
#lag(topic_partition_list, watermark_timeout_ms = Defaults::CONSUMER_LAG_TIMEOUT_MS) ⇒ Hash{String => Hash{Integer => Integer}}
Calculate the consumer lag per partition for the provided topic partition list. You can get a suitable list by calling #committed or #position (TODO). It is also possible to create one yourself, in this case you have to provide a list that already contains all the partitions you need the lag for.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/rdkafka/consumer.rb', line 489 def lag(topic_partition_list, watermark_timeout_ms = Defaults::CONSUMER_LAG_TIMEOUT_MS) out = {} topic_partition_list.to_h.each do |topic, partitions| # Query high watermarks for this topic's partitions # and compare to the offset in the list. topic_out = {} partitions.each do |p| next if p.offset.nil? _low, high = query_watermark_offsets( topic, p.partition, watermark_timeout_ms ) topic_out[p.partition] = high - p.offset end out[topic] = topic_out end out end |
#member_id ⇒ String?
Returns this client’s broker-assigned group member id
This currently requires the high-level KafkaConsumer
525 526 527 528 529 530 |
# File 'lib/rdkafka/consumer.rb', line 525 def member_id closed_consumer_check(__method__) @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_memberid(inner) end end |
#name ⇒ String
Returns consumer name.
34 35 36 37 38 |
# File 'lib/rdkafka/consumer.rb', line 34 def name @name ||= @native_kafka.with_inner do |inner| ::Rdkafka::Bindings.rd_kafka_name(inner) end end |
#offsets_for_times(list, timeout_ms = Defaults::CONSUMER_OFFSETS_FOR_TIMES_TIMEOUT_MS) ⇒ TopicPartitionList
Lookup offset for the given partitions by timestamp.
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/rdkafka/consumer.rb', line 616 def offsets_for_times(list, timeout_ms = Defaults::CONSUMER_OFFSETS_FOR_TIMES_TIMEOUT_MS) closed_consumer_check(__method__) if !list.is_a?(TopicPartitionList) raise TypeError.new("list has to be a TopicPartitionList") end tpl = list.to_native_tpl response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_offsets_for_times( inner, tpl, timeout_ms # timeout ) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end TopicPartitionList.from_native_tpl(tpl) ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) if tpl end |
#pause(list) ⇒ nil
Pause producing or consumption for the provided list of partitions
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rdkafka/consumer.rb', line 247 def pause(list) closed_consumer_check(__method__) unless list.is_a?(TopicPartitionList) raise TypeError.new("list has to be a TopicPartitionList") end tpl = list.to_native_tpl begin response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_pause_partitions(inner, tpl) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR list = TopicPartitionList.from_native_tpl(tpl) raise Rdkafka::RdkafkaTopicPartitionListError.new(response, list, "Error pausing '#{list.to_h}'") end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) end end |
#poll(timeout_ms) ⇒ Message?
Poll for the next message on one of the subscribed topics
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/rdkafka/consumer.rb', line 681 def poll(timeout_ms) closed_consumer_check(__method__) = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_consumer_poll(inner, timeout_ms) end if .null? nil else # Create struct wrapper = Rdkafka::Bindings::Message.new() # Raise error if needed if [:err] != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new([:err]) end # Create a message to pass out Rdkafka::Consumer::Message.new() end ensure # Clean up rdkafka message if there is one if && !.null? Rdkafka::Bindings.() end end |
#poll_batch(timeout_ms, max_items: 100) ⇒ Array<Message>
Poll for a batch of messages from the consumer queue in a single FFI call.
This is more efficient than calling #poll in a loop because it crosses the FFI boundary only once to fetch up to ‘max_items` messages.
The timeout controls how long to wait for the first message. Once any message is available, librdkafka fills the buffer with whatever is immediately ready and returns without further waiting.
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'lib/rdkafka/consumer.rb', line 803 def poll_batch(timeout_ms, max_items: 100) closed_consumer_check(__method__) buffer = batch_buffer(max_items) = [] count = @native_kafka.with_inner do |_inner| Rdkafka::Bindings.rd_kafka_consume_batch_queue( consumer_queue, timeout_ms, buffer, max_items ) end return if count <= 0 i = 0 begin while i < count ptr = buffer.get_pointer(i * FFI::Pointer.size) if ptr.null? i += 1 next end = Rdkafka::Bindings::Message.new(ptr) if [:err] != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new([:err]) end << Rdkafka::Consumer::Message.new() Rdkafka::Bindings.(ptr) i += 1 end ensure while i < count ptr = buffer.get_pointer(i * FFI::Pointer.size) Rdkafka::Bindings.(ptr) unless ptr.null? i += 1 end end end |
#poll_batch_nb(timeout_ms = 0, max_items: 100) ⇒ Array<Message>
Since the GVL is not released, a non-zero timeout_ms will block all Ruby threads/fibers for the duration. Use #poll_batch if you need a blocking wait.
Poll for a batch of messages without releasing the GVL (Global VM Lock).
This is more efficient than #poll_batch for non-blocking poll(0) calls, particularly useful in fiber scheduler contexts where GVL release/reacquire overhead is wasteful since we don’t expect to wait.
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'lib/rdkafka/consumer.rb', line 865 def poll_batch_nb(timeout_ms = 0, max_items: 100) closed_consumer_check(__method__) buffer = batch_buffer(max_items) = [] count = @native_kafka.with_inner do |_inner| Rdkafka::Bindings.rd_kafka_consume_batch_queue_nb( consumer_queue, timeout_ms, buffer, max_items ) end return if count <= 0 i = 0 begin while i < count ptr = buffer.get_pointer(i * FFI::Pointer.size) if ptr.null? i += 1 next end = Rdkafka::Bindings::Message.new(ptr) if [:err] != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new([:err]) end << Rdkafka::Consumer::Message.new() Rdkafka::Bindings.(ptr) i += 1 end ensure while i < count ptr = buffer.get_pointer(i * FFI::Pointer.size) Rdkafka::Bindings.(ptr) unless ptr.null? i += 1 end end end |
#poll_nb(timeout_ms = 0) ⇒ Message?
Poll for the next message without releasing the GVL (Global VM Lock).
This is more efficient than regular polling for non-blocking poll(0) calls, particularly useful in fiber scheduler contexts where GVL release/reacquire overhead is wasteful since we don’t expect to wait.
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
# File 'lib/rdkafka/consumer.rb', line 721 def poll_nb(timeout_ms = 0) closed_consumer_check(__method__) = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_consumer_poll_nb(inner, timeout_ms) end if .null? nil else # Create struct wrapper = Rdkafka::Bindings::Message.new() # Raise error if needed if [:err] != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new([:err]) end # Create a message to pass out Rdkafka::Consumer::Message.new() end ensure # Clean up rdkafka message if there is one if && !.null? Rdkafka::Bindings.() end end |
#poll_nb_each {|message| ... } ⇒ nil
This method uses ‘rd_kafka_consumer_poll` to fetch messages, unlike `events_poll_nb_each` which uses `rd_kafka_poll` for event callbacks (delivery reports, statistics, etc.). For consumers, use this method to receive messages and `events_poll_nb_each` for processing background events.
This method holds the inner lock for the duration. Other consumer operations will wait until this method returns.
Timeout/max_messages logic should be implemented by the caller
Polls for messages in a non-blocking loop, yielding each message to the caller.
This method processes messages in a single GVL/mutex session until the queue is empty or the caller returns ‘:stop`. It handles the message pointer lifecycle internally, ensuring proper cleanup via `rd_kafka_message_destroy`.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rdkafka/consumer.rb', line 154 def poll_nb_each closed_consumer_check(__method__) @native_kafka.with_inner do |inner| loop do = Rdkafka::Bindings.rd_kafka_consumer_poll_nb(inner, 0) break if .null? begin = Rdkafka::Bindings::Message.new() if [:err] != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new([:err]) end result = yield Consumer::Message.new() break if result == :stop ensure Rdkafka::Bindings.() end end end end |
#position(list = nil) ⇒ TopicPartitionList
Return the current positions (offsets) for topics and partitions. The offset field of each requested partition will be set to the offset of the last consumed message + 1, or nil in case there was no previous message.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/rdkafka/consumer.rb', line 424 def position(list = nil) if list.nil? list = assignment elsif !list.is_a?(TopicPartitionList) raise TypeError.new("list has to be nil or a TopicPartitionList") end tpl = list.to_native_tpl response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_position(inner, tpl) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end TopicPartitionList.from_native_tpl(tpl) ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) if tpl end |
#query_watermark_offsets(topic, partition, timeout_ms = Defaults::CONSUMER_QUERY_WATERMARK_TIMEOUT_MS) ⇒ Integer
Query broker for low (oldest/beginning) and high (newest/end) offsets for a partition.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/rdkafka/consumer.rb', line 453 def query_watermark_offsets(topic, partition, timeout_ms = Defaults::CONSUMER_QUERY_WATERMARK_TIMEOUT_MS) closed_consumer_check(__method__) low = FFI::MemoryPointer.new(:int64, 1) high = FFI::MemoryPointer.new(:int64, 1) response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_query_watermark_offsets( inner, topic, partition, low, high, timeout_ms ) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response, "Error querying watermark offsets for partition #{partition} of #{topic}") end [low.read_array_of_int64(1).first, high.read_array_of_int64(1).first] ensure low&.free high&.free end |
#resume(list) ⇒ nil
Resumes producing consumption for the provided list of partitions
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/rdkafka/consumer.rb', line 275 def resume(list) closed_consumer_check(__method__) unless list.is_a?(TopicPartitionList) raise TypeError.new("list has to be a TopicPartitionList") end tpl = list.to_native_tpl begin response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_resume_partitions(inner, tpl) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response, "Error resume '#{list.to_h}'") end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) end end |
#seek(message) ⇒ nil
Seek to a particular message. The next poll on the topic/partition will return the message at the given offset.
570 571 572 |
# File 'lib/rdkafka/consumer.rb', line 570 def seek() seek_by(.topic, .partition, .offset) end |
#seek_by(topic, partition, offset) ⇒ nil
Seek to a particular message by providing the topic, partition and offset. The next poll on the topic/partition will return the message at the given offset.
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/rdkafka/consumer.rb', line 583 def seek_by(topic, partition, offset) closed_consumer_check(__method__) # rd_kafka_offset_store is one of the few calls that does not support # a string as the topic, so create a native topic for it. native_topic = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_topic_new( inner, topic, nil ) end response = Rdkafka::Bindings.rd_kafka_seek( native_topic, partition, offset, Defaults::CONSUMER_SEEK_TIMEOUT_MS ) if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end ensure if native_topic && !native_topic.null? Rdkafka::Bindings.rd_kafka_topic_destroy(native_topic) end end |
#start ⇒ Object
Not needed to run unless explicit start was disabled
Starts the native Kafka polling thread and kicks off the init polling
29 30 31 |
# File 'lib/rdkafka/consumer.rb', line 29 def start @native_kafka.start end |
#store_offset(message) ⇒ nil
Store offset of a message to be used in the next commit of this consumer
When using this ‘enable.auto.offset.store` should be set to `false` in the config.
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/rdkafka/consumer.rb', line 539 def store_offset() closed_consumer_check(__method__) list = TopicPartitionList.new list.add_topic_and_partitions_with_offsets( .topic, .partition => .offset + 1 ) tpl = list.to_native_tpl response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_offsets_store( inner, tpl ) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) if tpl end |
#subscribe(*topics) ⇒ nil
Subscribes to one or more topics letting Kafka handle partition assignments.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rdkafka/consumer.rb', line 206 def subscribe(*topics) closed_consumer_check(__method__) # Create topic partition list with topics and no partition set tpl = Rdkafka::Bindings.rd_kafka_topic_partition_list_new(topics.length) topics.each do |topic| Rdkafka::Bindings.rd_kafka_topic_partition_list_add(tpl, topic, Rdkafka::Bindings::RD_KAFKA_PARTITION_UA) end # Subscribe to topic partition list and check this was successful response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_subscribe(inner, tpl) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response, "Error subscribing to '#{topics.join(", ")}'") end ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(tpl) unless tpl.nil? end |
#subscription ⇒ TopicPartitionList
Returns the current subscription to topics and partitions
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/rdkafka/consumer.rb', line 300 def subscription closed_consumer_check(__method__) ptr = FFI::MemoryPointer.new(:pointer) response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_subscription(inner, ptr) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end native = ptr.read_pointer begin Rdkafka::Consumer::TopicPartitionList.from_native_tpl(native) ensure Rdkafka::Bindings.rd_kafka_topic_partition_list_destroy(native) end end |
#unsubscribe ⇒ nil
Unsubscribe from all subscribed topics.
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/rdkafka/consumer.rb', line 231 def unsubscribe closed_consumer_check(__method__) response = @native_kafka.with_inner do |inner| Rdkafka::Bindings.rd_kafka_unsubscribe(inner) end if response != Rdkafka::Bindings::RD_KAFKA_RESP_ERR_NO_ERROR raise Rdkafka::RdkafkaError.new(response) end end |