Class: Shipeasy::SDK::See::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/shipeasy/sdk/see.rb

Overview

Per-process spam guard: identical events within 30s collapse to one send; a hard cap bounds total sends. Thread-safe. The worker dedupes by fingerprint anyway — this only bounds network chatter from a hot loop.

Instance Method Summary collapse

Constructor Details

#initialize(max_per_process: SEE_MAX_PER_PROCESS, dedup_window_ms: SEE_DEDUP_WINDOW_MS) ⇒ Limiter

Returns a new instance of Limiter.



156
157
158
159
160
161
162
# File 'lib/shipeasy/sdk/see.rb', line 156

def initialize(max_per_process: SEE_MAX_PER_PROCESS, dedup_window_ms: SEE_DEDUP_WINDOW_MS)
  @max    = max_per_process
  @window = dedup_window_ms
  @last   = {}
  @sent   = 0
  @mutex  = Mutex.new
end

Instance Method Details

#should_send?(ev) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/shipeasy/sdk/see.rb', line 164

def should_send?(ev)
  @mutex.synchronize do
    return false if @sent >= @max

    key = [
      ev["kind"],
      ev["error_type"],
      ev["message"].to_s[0, 200],
      See.top_stack_line(ev["stack"]),
    ].join("|")
    now = (Time.now.to_f * 1000).to_i
    prev = @last[key]
    return false if prev && (now - prev) < @window

    @last[key] = now
    @sent += 1
    true
  end
end