Class: Stoplight::Infrastructure::Memory::DataStore::SlidingWindow Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stoplight/infrastructure/memory/data_store/sliding_window.rb,
sig/_private/stoplight/infrastructure/memory/data_store/sliding_window.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

Not thread-safe; synchronization must be handled externally

Hash-based sliding window for O(1) amortized operations.

Maintains a running sum and stores per-second counts in a Hash. Ruby's Hash preserves insertion order (FIFO), allowing efficient removal of expired buckets from the front via Hash#shift, with their counts subtracted from the running sum.

Performance: O(1) amortized for both reads and writes Memory: Bounded to the number of buckets

Instance Method Summary collapse

Constructor Details

#initialize(clock:) ⇒ SlidingWindow

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SlidingWindow.

Parameters:



20
21
22
23
24
# File 'lib/stoplight/infrastructure/memory/data_store/sliding_window.rb', line 20

def initialize(clock:)
  @buckets = Hash.new { |buckets, bucket| buckets[bucket] = 0 }
  @running_sum = 0
  @clock = clock
end

Instance Method Details

#incrementvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Increment the count at a given timestamp



27
28
29
30
# File 'lib/stoplight/infrastructure/memory/data_store/sliding_window.rb', line 27

def increment
  buckets[current_bucket] += 1
  self.running_sum += 1
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


37
38
39
# File 'lib/stoplight/infrastructure/memory/data_store/sliding_window.rb', line 37

def inspect
  "#<#{self.class.name} #{buckets}>"
end

#sum_in_window(window_start) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • window_start (Time)

Returns:

  • (Integer)


32
33
34
35
# File 'lib/stoplight/infrastructure/memory/data_store/sliding_window.rb', line 32

def sum_in_window(window_start)
  slide_window!(window_start)
  self.running_sum
end