NatureWhistle.EventGuard (nature_whistle v0.3.0)

Copy Markdown

Rate-limit and sliding-window helpers for NatureWhistle.

The event handler delegates all traffic-shaping decisions to this module so the logic is easy to test in isolation. Two independent filters are exposed:

  • rate_limit limits the number of alert dispatches over a time window
  • sliding_window counts breach density across fixed-size buckets

Both helpers read and write to the :nature_whistle_rate_limit ETS table.

Summary

Functions

Returns true when the alert is still under its rate-limit allowance.

Returns true when the alert has already reached its sliding-window cap.

Records a rate-limit timestamp for the given alert.

Increments the sliding-window bucket for the alert at the current timestamp.

Functions

allow_rate_limit?(alert, now)

Returns true when the alert is still under its rate-limit allowance.

The alert configuration may include a rate_limit keyword list with:

  • :window_ms - the lookback window used for counting recent events
  • :max_events - the number of events allowed within that window

When no rate limit is configured, the function always returns true.

allow_sliding_window?(alert, now)

Returns true when the alert has already reached its sliding-window cap.

The sliding-window gate uses coarse buckets of @sub_bucket_ms milliseconds. This keeps the ETS state compact while still approximating the configured density window closely enough for alerting purposes.

record_rate_limit(map, now)

Records a rate-limit timestamp for the given alert.

The timestamps are stored newest-first so the cleanup pass can trim old data efficiently while still keeping the implementation straightforward.

record_sliding_window_event(alert, now)

Increments the sliding-window bucket for the alert at the current timestamp.

Buckets are stored in ETS as { {:sliding_window, alert_id}, bucket_start_ms } entries whose counters are incremented atomically.