Skip to content

About

Debounce Ruby callbacks across processes with a lightweight Ruby timer server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Debounced

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.

Installation

Add this line to your application's Gemfile:

gem 'debounced'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install debounced

Usage

Configuration

# 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
end

Starting the server

Start the debounce server with:

$ bundle exec rake debounced:server

In 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">

Shutting down

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.

How It Works

  1. The debounce server listens on a Unix socket and runs one lightweight timer per activity descriptor
  2. When you call debounce_activity, the proxy sends the descriptor, timeout and callback to the server
  3. Each new request for the same descriptor cancels its timer and starts a new one, keeping the latest callback
  4. 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.

Performance

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.

License

The gem is available as open source under the terms of the MIT License.

About

Debounce Ruby callbacks across processes with a lightweight Ruby timer server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages