Class: Semian::ThreadSafe::SlidingWindow

Inherits:
Simple::SlidingWindow show all
Defined in:
lib/semian/simple_sliding_window.rb

Instance Attribute Summary

Attributes inherited from Simple::SlidingWindow

#max_size

Instance Method Summary collapse

Methods inherited from Simple::SlidingWindow

#clear

Constructor Details

#initializeSlidingWindow

Returns a new instance of SlidingWindow.



47
48
49
50
# File 'lib/semian/simple_sliding_window.rb', line 47

def initialize(**)
  super
  @lock = Mutex.new
end

Instance Method Details

#pushObject



63
64
65
# File 'lib/semian/simple_sliding_window.rb', line 63

def push(*)
  @lock.synchronize { super }
end

#reject!Object

#size, #last, and #clear are not wrapped in a mutex. For the first two, the worst-case is a thread-switch at a timing where they’d receive an out-of-date value–which could happen with a mutex as well.

As for clear, it’s an all or nothing operation. Doesn’t matter if we have the lock or not.



59
60
61
# File 'lib/semian/simple_sliding_window.rb', line 59

def reject!(*)
  @lock.synchronize { super }
end