Efficient debouncing mechanism for Ruby events. Use it for rate limiting, deduplication, or other scenarios where you want to wait for a certain amount of time before processing a given event.
Add this line to your application's Gemfile:
gem 'debounced'And then execute:
$ bundle installOr install it yourself as:
$ gem install debounced# config/initializers/debounced.rb
Debounced.configure do |config|
config.socket_descriptor = '/tmp/my_app.debounceEvents'
config.wait_timeout = 3 # idle timeout in seconds for a given activity descriptor
endStart the debounce server with:
$ bundle exec rake debounced:serverIn your Ruby application code:
require 'debounced'
# Start a background thread to receive notification that events are ready to be handled after debounce wait is complete
proxy = Debounced::ServiceProxy.new
proxy.listen
# Define your event class; create a helper method that will produce a Debounced::Callback object, which
# is used to notify the server that the event is ready to be handled
class MyEvent
attr_reader :test_id
def initialize(test_id:)
@test_id = test_id
end
def publish
# put logic here to publish the event after debouncing
puts "Publishing event: #{inspect}"
end
def debounce_callback
Debounced::Callback.new(
class_name: self.class.name,
params: { test_id: },
method_name: 'publish',
method_params: []
)
end
end
event = MyEvent.new({ test_id: "Hello World" })
# request the server to debounce the event, ignoring it if another event with the
# same descriptor arrives before the timeout
proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
# 2 seconds later
proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
# 4 seconds later
proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
# 5 seconds later the event is published!
# > Publishing event: #<MyEvent:0x00007f9b1b8b3b40 @test_id="Hello World">On SIGTERM the server stops debouncing: pending timers still fire on schedule, requests that arrive afterwards are published immediately, and once nothing is pending the server closes its connections and exits. Keep debounce timeouts shorter than your platform's shutdown grace period (30 seconds on Heroku).
So that a process receives the callbacks still pending when it shuts down, stop the proxy with a timeout before exiting:
at_exit { proxy.stop(timeout: 10) }It waits until the server closes the connection or the timeout passes, and returns at once when not connected. SIGINT stops the server immediately, which is convenient in development.
- The debounce server listens on a Unix socket and runs one lightweight timer per activity descriptor
- When you call
debounce_activity, the proxy sends the descriptor, timeout and callback to the server - Each new request for the same descriptor cancels its timer and starts a new one, keeping the latest callback
- When a timer expires, the server sends the callback back to the proxy, which invokes it
Several processes can connect to one server, for example every Puma worker of an application. Each process's descriptors are debounced separately, even when two processes use the same descriptor: each gets its own callback, and a process's pending callbacks are dropped when it disconnects. When no server is reachable, the proxy invokes callbacks immediately.
Measured with bin/benchmark, which drives a server process over its socket. Medians of three runs on an Apple M4 Max,
Ruby 4.0.4, compared with the Node.js server from debounced 2.1.0 on Node.js 24.18.
| Measure | Ruby server (3.0) | Node.js server (2.1) |
|---|---|---|
| Throughput: 100,000 requests across 10,000 descriptors | 224,000 requests/s | 263,000 requests/s |
| Callback lateness after its timeout, median | 0.25 ms | 0.26 ms |
| Callback lateness, 99th percentile | 1.96 ms | 1.95 ms |
| Memory, idle | 27 MB | 48 MB |
| Memory, 100,000 pending timers | 156 MB | 174 MB |
To measure another server, pass its command: bin/benchmark node path/to/server.mjs.
The gem is available as open source under the terms of the MIT License.