Class: Datadog::Core::Buffer::ThreadSafe

Inherits:
Random
  • Object
show all
Defined in:
lib/datadog/core/buffer/thread_safe.rb

Overview

Buffer that stores objects, has a maximum size, and can be safely used concurrently on any environment.

This implementation uses a Mutex around public methods, incurring overhead in order to ensure thread-safety.

This is implementation is recommended for non-CRuby environments. If using CRuby, CRuby is a faster implementation with minimal compromise.

Instance Method Summary collapse

Methods inherited from Random

#closed?

Constructor Details

#initialize(max_size) ⇒ ThreadSafe

Returns a new instance of ThreadSafe.



15
16
17
18
19
# File 'lib/datadog/core/buffer/thread_safe.rb', line 15

def initialize(max_size)
  super

  @mutex = Mutex.new
end

Instance Method Details

#closeObject



46
47
48
# File 'lib/datadog/core/buffer/thread_safe.rb', line 46

def close
  synchronize { super }
end

#concat(items) ⇒ Object



27
28
29
# File 'lib/datadog/core/buffer/thread_safe.rb', line 27

def concat(items)
  synchronize { super }
end

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


37
38
39
# File 'lib/datadog/core/buffer/thread_safe.rb', line 37

def empty?
  synchronize { super }
end

#lengthObject

Return the current number of stored items.



32
33
34
# File 'lib/datadog/core/buffer/thread_safe.rb', line 32

def length
  synchronize { super }
end

#popObject

Stored items are returned and the local buffer is reset.



42
43
44
# File 'lib/datadog/core/buffer/thread_safe.rb', line 42

def pop
  synchronize { super }
end

#push(item) ⇒ Object

Add a new “item“ in the local queue. This method doesn’t block the execution even if the buffer is full. In that case, a random item is discarded.



23
24
25
# File 'lib/datadog/core/buffer/thread_safe.rb', line 23

def push(item)
  synchronize { super }
end

#synchronize(&block) ⇒ Object



50
51
52
# File 'lib/datadog/core/buffer/thread_safe.rb', line 50

def synchronize(&block)
  @mutex.synchronize(&block)
end