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.

Direct Known Subclasses

Tracing::ThreadSafeTraceBuffer

Instance Method Summary collapse

Methods inherited from Random

#closed?

Constructor Details

#initialize(max_size) ⇒ ThreadSafe

Returns a new instance of ThreadSafe.



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

def initialize(max_size)
  super

  @mutex = Mutex.new
end

Instance Method Details

#closeObject



48
49
50
# File 'lib/datadog/core/buffer/thread_safe.rb', line 48

def close
  synchronize { super }
end

#concat(items) ⇒ Object



29
30
31
# File 'lib/datadog/core/buffer/thread_safe.rb', line 29

def concat(items)
  synchronize { super }
end

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


39
40
41
# File 'lib/datadog/core/buffer/thread_safe.rb', line 39

def empty?
  synchronize { super }
end

#lengthObject

Return the current number of stored items.



34
35
36
# File 'lib/datadog/core/buffer/thread_safe.rb', line 34

def length
  synchronize { super }
end

#popObject

Stored items are returned and the local buffer is reset.



44
45
46
# File 'lib/datadog/core/buffer/thread_safe.rb', line 44

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.



25
26
27
# File 'lib/datadog/core/buffer/thread_safe.rb', line 25

def push(item)
  synchronize { super }
end

#synchronize(&block) ⇒ Object



52
53
54
# File 'lib/datadog/core/buffer/thread_safe.rb', line 52

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