Method: Stud::Buffer#buffer_initialize
- Defined in:
- lib/stud/buffer.rb
#buffer_initialize(options = {}) ⇒ Object
Initialize the buffer.
Call directly from your constructor if you wish to set some non-default options. Otherwise buffer_initialize will be called automatically during the first buffer_receive call.
Options:
- :max_items, Max number of items to buffer before flushing. Default 50.
- :max_interval, Max number of seconds to wait between flushes. Default 5.
- :logger, A logger to write log messages to. No default. Optional.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/stud/buffer.rb', line 80 def buffer_initialize(={}) if ! self.class.method_defined?(:flush) raise ArgumentError, "Any class including Stud::Buffer must define a flush() method." end @buffer_config = { :max_items => [:max_items] || 50, :max_interval => [:max_interval] || 5, :logger => [:logger] || nil, :has_on_flush_error => self.class.method_defined?(:on_flush_error), :has_on_full_buffer_receive => self.class.method_defined?(:on_full_buffer_receive) } @buffer_state = { # items accepted from including class :pending_items => {}, :pending_count => 0, # guard access to pending_items & pending_count :pending_mutex => Mutex.new, # items which are currently being flushed :outgoing_items => {}, :outgoing_count => 0, # ensure only 1 flush is operating at once :flush_mutex => Mutex.new, # data for timed flushes :last_flush => Time.now, :timer => Thread.new do loop do sleep(@buffer_config[:max_interval]) buffer_flush(:force => true) end end } # events we've accumulated buffer_clear_pending end |