Class: Trifle::Stats::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/trifle/stats/buffer.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

DEFAULT_DURATION =
1
DEFAULT_SIZE =
256

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver:, duration: DEFAULT_DURATION, size: DEFAULT_SIZE, aggregate: true, async: true) ⇒ Buffer

rubocop:disable Metrics/MethodLength



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/trifle/stats/buffer.rb', line 235

def initialize(driver:, duration: DEFAULT_DURATION, size: DEFAULT_SIZE, aggregate: true, async: true) # rubocop:disable Metrics/MethodLength
  @driver = driver
  @duration = duration.to_f
  @size = size.to_i.positive? ? size.to_i : 1
  @async = async
  @queue = BufferQueue.new(aggregate: aggregate)
  @mutex = Mutex.new
  @stopped = false
  @flush_pending = false
  @pending_condition = ConditionVariable.new
  @worker = start_worker if async && @duration.positive?
  self.class.register(self)
end

Class Method Details

.flush_allObject



222
223
224
# File 'lib/trifle/stats/buffer.rb', line 222

def flush_all
  BufferRegistry.flush_all
end

.pending_flushes?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/trifle/stats/buffer.rb', line 230

def pending_flushes?
  BufferRegistry.pending?
end

.register(buffer) ⇒ Object



214
215
216
# File 'lib/trifle/stats/buffer.rb', line 214

def register(buffer)
  BufferRegistry.register(buffer)
end

.run_pending!Object



226
227
228
# File 'lib/trifle/stats/buffer.rb', line 226

def run_pending!
  BufferRegistry.run_pending!
end

.unregister(buffer) ⇒ Object



218
219
220
# File 'lib/trifle/stats/buffer.rb', line 218

def unregister(buffer)
  BufferRegistry.unregister(buffer)
end

Instance Method Details

#flush!Object



257
258
259
260
261
262
# File 'lib/trifle/stats/buffer.rb', line 257

def flush!
  actions = drain_actions(reset_pending: true)
  return if actions.nil?

  process(actions)
end

#flush_from_registry!Object



274
275
276
277
# File 'lib/trifle/stats/buffer.rb', line 274

def flush_from_registry!
  actions = drain_pending_actions
  process(actions) if actions
end

#inc(keys:, values:, tracking_key: nil) ⇒ Object



249
250
251
# File 'lib/trifle/stats/buffer.rb', line 249

def inc(keys:, values:, tracking_key: nil)
  enqueue(:inc, keys: keys, values: values, tracking_key: tracking_key)
end

#set(keys:, values:, tracking_key: nil) ⇒ Object



253
254
255
# File 'lib/trifle/stats/buffer.rb', line 253

def set(keys:, values:, tracking_key: nil)
  enqueue(:set, keys: keys, values: values, tracking_key: tracking_key)
end

#shutdown!Object



264
265
266
267
268
269
270
271
272
# File 'lib/trifle/stats/buffer.rb', line 264

def shutdown!
  return if @shutdown

  @shutdown = true
  stop_worker
  BufferRegistry.cancel_pending(self)
  flush!
  self.class.unregister(self)
end