Class: Greenfinch::BufferedConsumer
- Inherits:
-
Object
- Object
- Greenfinch::BufferedConsumer
- Defined in:
- lib/greenfinch-ruby/consumer.rb
Overview
BufferedConsumer buffers messages in memory, and sends messages as a batch. This can improve performance, but calls to #send! may still block if the buffer is full. If you use this consumer, you should call #flush when your application exits or the messages remaining in the buffer will not be sent.
To use a BufferedConsumer directly with a Greenfinch::Tracker, instantiate your Tracker like this
buffered_consumer = Greenfinch::BufferedConsumer.new
begin
buffered_tracker = Greenfinch::Tracker.new(YOUR_GREENFINCH_TOKEN) do |type, message|
buffered_consumer.send!(type, message)
end
# Do some tracking here
...
ensure
buffered_consumer.flush
end
Constant Summary collapse
- MAX_LENGTH =
50
Instance Method Summary collapse
-
#flush ⇒ Object
Pushes all remaining messages in the buffer to Greenfinch.
-
#initialize(events_endpoint = nil, update_endpoint = nil, import_endpoint = nil, max_buffer_length = MAX_LENGTH, &block) ⇒ BufferedConsumer
constructor
Create a Greenfinch::BufferedConsumer.
-
#send(type, message) ⇒ Object
This method was deprecated in release 2.0.0, please use send! instead.
-
#send!(type, message) ⇒ Object
Stores a message for Greenfinch in memory.
Constructor Details
#initialize(events_endpoint = nil, update_endpoint = nil, import_endpoint = nil, max_buffer_length = MAX_LENGTH, &block) ⇒ BufferedConsumer
Create a Greenfinch::BufferedConsumer. If you provide endpoint arguments, they will be used instead of the default Greenfinch endpoints. This can be useful for proxying, debugging, or if you prefer not to use SSL for your events.
You can also change the preferred buffer size before the consumer automatically sends its buffered events. The Greenfinch endpoints have a limit of 50 events per HTTP request, but you can lower the limit if your individual events are very large.
By default, BufferedConsumer will use a standard Greenfinch consumer to send the events once the buffer is full (or on calls to #flush), but you can override this behavior by passing a block to the constructor, in the same way you might pass a block to the Greenfinch::Tracker constructor. If a block is passed to the constructor, the *_endpoint constructor arguments are ignored.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/greenfinch-ruby/consumer.rb', line 199 def initialize(events_endpoint=nil, update_endpoint=nil, import_endpoint=nil, max_buffer_length=MAX_LENGTH, &block) @max_length = [max_buffer_length, MAX_LENGTH].min @buffers = { :event => [], :profile_update => [], } if block @sink = block else consumer = Consumer.new(events_endpoint, update_endpoint, import_endpoint) @sink = consumer.method(:send!) end end |
Instance Method Details
#flush ⇒ Object
Pushes all remaining messages in the buffer to Greenfinch. You should call #flush before your application exits or messages may not be sent.
240 241 242 |
# File 'lib/greenfinch-ruby/consumer.rb', line 240 def flush @buffers.keys.each { |k| flush_type(k) } end |
#send(type, message) ⇒ Object
This method was deprecated in release 2.0.0, please use send! instead
232 233 234 235 |
# File 'lib/greenfinch-ruby/consumer.rb', line 232 def send(type, ) warn '[DEPRECATION] send has been deprecated as of release 2.0.0, please use send! instead' send!(type, ) end |
#send!(type, message) ⇒ Object
Stores a message for Greenfinch in memory. When the buffer hits a maximum length, the consumer will flush automatically. Flushes are synchronous when they occur.
Currently, only :event and :profile_update messages are buffered, :import messages will be send immediately on call.
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/greenfinch-ruby/consumer.rb', line 220 def send!(type, ) type = type.to_sym if @buffers.has_key? type @buffers[type] << flush_type(type) if @buffers[type].length >= @max_length else @sink.call(type, ) end end |