debounced 2.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9fa1cd41e5d203718e5f338068db77acd8b07f15369c705e096f9a2108b7614
4
- data.tar.gz: 544067be17e4efad251d656374b5d41a08c2c3ad627fc7f0ac75dbdf83be0c05
3
+ metadata.gz: daa35f9312614c9334ddcb04cd3b569fe070e952b544ceee739b2400a49eefca
4
+ data.tar.gz: 50c4d30cc47754c0370f984fad5f74626199ab43394160d1c538f1b651db0dfe
5
5
  SHA512:
6
- metadata.gz: 7965f45c4e5055cf0c0e98123c6e88b5e7f1d9ef2ac6e0ecc1a3f09e51364b6feb2d5c3f07dc294d54a6f15af717cf6b6ccb87eae090466583f07f8ff6d162cd
7
- data.tar.gz: 7165afce55d82ff97a1747181bd219e63a94ac79f23d0ec3b3ffbc04b4c6ce31f83be4b1a3372dd20484bae19db00f4546e7e4bc02f162efaf5dc560c4569fae
6
+ metadata.gz: 1373f6bf67a372eab97c5f932452cb5f244fd015aece067800c1146f1121686c681e3ab7318c050e75cd4a5b28df911dade2965fd3a1a57c9cb9b4a3a5da66f0
7
+ data.tar.gz: ade42af6dc1f0ec81473cdd19ed33e6a3eba213273ebf7dc7478b2018f1b9db31b4e124e7ab81af00cd2374009d4151ef0a5897e4da041bee6982fa739cf3e1c
data/CHANGELOG.md CHANGED
@@ -5,34 +5,50 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [Unreleased]
8
+ ## [4.0.0] - 2026-12-02
9
9
 
10
- ### Fixed
10
+ ### Added
11
+
12
+ - `debounced-server` executable, which starts the server without booting the host application; `--config FILE` loads
13
+ a Ruby file that calls `Debounced.configure`.
14
+ - The previous `debounced:server` rake task booted the whole host application (every gem in its bundle, plus its app
15
+ code) only to read the socket path, so the server used as much memory as the app. The executable loads only the gem
16
+ and the config file, so the server uses about as much memory as the gem alone.
11
17
 
12
- - Each process's descriptors are debounced separately. When two processes debounced the same descriptor, the later
13
- request cancelled the earlier process's callback; now each process gets its own callback, and a process's pending
14
- callbacks are cancelled when it disconnects
18
+ ### Changed
15
19
 
16
- ## [2.1.0] - 2026-09-26
20
+ - The default logger writes to stdout instead of `debounced_proxy.log`
17
21
 
18
- ### Security
22
+ ### Removed
19
23
 
20
- - Callbacks are only dispatched to public methods defined by the application, never to core Ruby methods
21
- - The server creates its socket owner-only, and the proxy refuses a socket owned by another user
24
+ - The `debounced:server` rake task and the Railtie that loaded it; start the server with `debounced-server` instead
25
+
26
+ ## [3.0.0] - 2026-11-12
22
27
 
23
28
  ### Changed
24
29
 
25
- - Requires Node.js 22 or later; Node.js 18 and 20 are past end of life
30
+ - The debounce server is now written in Ruby (`Debounced::Server`, on the `async` gem); Node.js is no longer required
31
+ - Start it with `bundle exec rake debounced:server`, as before
32
+ - Requires Ruby 3.3 or later
33
+ - The server serves any number of connected processes and debounces each process's descriptors separately, publishing each callback to the process that requested it
34
+ - On SIGTERM the server fires its pending timers on schedule, publishes new requests immediately, and exits once
35
+ nothing is pending; SIGINT still exits immediately
36
+ - `ServiceProxy#stop(timeout:)` first waits up to `timeout` seconds for the server to close the connection, so a
37
+ process can receive the callbacks still pending when it shuts down
38
+ - `Debounced.configuration.error_handler` — an optional callable, invoked with the error and the `Debounced::Callback`
39
+ when a callback raises, so a host app can report callback failures to an error tracker without the gem depending on one
26
40
 
27
41
  ### Fixed
28
42
 
29
- - A second server no longer takes over the socket of a running one, and only removes a socket it created
30
- - The server exits non-zero when it cannot listen
31
- - Multi-byte characters split across socket reads are no longer corrupted
32
- - The server serves any number of connected processes and publishes each callback to the process that requested it
33
- - The listener no longer adds an extra idle timeout before delivering a callback
43
+ - The gem now depends on and requires `concurrent-ruby` directly, rather than relying on `semantic_logger` to pull it in
44
+ transitively (`ServiceProxy` uses `Concurrent::AtomicBoolean`)
45
+ - Callbacks are only dispatched to public methods defined by the application, never to core Ruby methods
46
+ - The socket is created owner-only, and the proxy refuses a socket owned by another user
47
+ - A second server no longer takes over the socket of a running one
34
48
  - Requests larger than the socket buffer are no longer truncated
35
49
  - A failed send falls back to invoking the callback instead of raising
50
+ - Callbacks are no longer delayed by an extra idle timeout in the listener
51
+ - Multi-byte characters split across socket reads are no longer corrupted
36
52
  - `ServiceProxy#stop` works before `listen`
37
53
 
38
54
  ## [1.0.7](https://github.com/Flytedesk/debounced/compare/v1.0.6...v1.0.7) (2026-04-15)
data/README.md CHANGED
@@ -23,36 +23,42 @@ Or install it yourself as:
23
23
  $ gem install debounced
24
24
  ```
25
25
 
26
- ## Dependencies
26
+ ## Usage
27
27
 
28
- This gem requires Node.js to be installed on your system, as it uses a Node.js server to handle the debouncing logic. You'll need:
28
+ Debounced runs as two processes that talk over a Unix socket:
29
29
 
30
- - Node.js >= 22.0.0
30
+ - **The server** keeps one timer per activity. It is a standalone executable that loads only the gem and a
31
+ config file.
32
+ - **Your application** sends activity to the server through `Debounced::ServiceProxy`, and runs the callbacks the
33
+ server sends back when timers expire.
31
34
 
32
- ## Usage
35
+ Both load the same config file, so they agree on the socket.
33
36
 
34
37
  ### Configuration
35
38
 
36
39
  ```ruby
37
- # config/initializers/debounced.rb
40
+ # config/debounced.rb
38
41
  Debounced.configure do |config|
39
42
  config.socket_descriptor = '/tmp/my_app.debounceEvents'
40
43
  config.wait_timeout = 3 # idle timeout in seconds for a given activity descriptor
41
44
  end
42
45
  ```
43
46
 
44
- ### Starting the server
47
+ By default the gem logs to stdout through `semantic_logger`. Set `config.logger` to use another logger.
45
48
 
46
- Start the nodeJS debounce server with:
49
+ ### Starting the server
47
50
 
48
51
  ```bash
49
- $ bundle exec debounced:server
52
+ $ bundle exec debounced-server --config config/debounced.rb
50
53
  ```
51
54
 
52
- In your Ruby application code:
55
+ ### Debouncing activity in your application
56
+
57
+ Load the same config file, then create a proxy:
53
58
 
54
59
  ```ruby
55
60
  require 'debounced'
61
+ require_relative 'config/debounced'
56
62
 
57
63
  # Start a background thread to receive notification that events are ready to be handled after debounce wait is complete
58
64
  proxy = Debounced::ServiceProxy.new
@@ -95,13 +101,65 @@ proxy.debounce_activity("my-event-123", 5, event.debounce_callback)
95
101
  # > Publishing event: #<MyEvent:0x00007f9b1b8b3b40 @test_id="Hello World">
96
102
  ```
97
103
 
104
+ ### In a Rails application
105
+
106
+ Load the config file from an initializer. The initializer is also the place for settings that only apply to your
107
+ application: Rails' logger, and an `error_handler` for callbacks that raise (callbacks run in your application, never in
108
+ the server).
109
+
110
+ ```ruby
111
+ # config/initializers/debounced.rb
112
+ require Rails.root.join('config/debounced')
113
+
114
+ Debounced.configure do |config|
115
+ config.logger = Rails.logger
116
+ config.error_handler = ->(error, callback) { Rails.error.report(error, context: { callback: callback.as_json }) }
117
+ end
118
+ ```
119
+
120
+ ### Shutting down
121
+
122
+ On SIGTERM the server stops debouncing: pending timers still fire on schedule, requests that arrive afterwards are
123
+ published immediately, and once nothing is pending the server closes its connections and exits. Keep debounce
124
+ timeouts shorter than your platform's shutdown grace period (30 seconds on Heroku).
125
+
126
+ So that a process receives the callbacks still pending when it shuts down, stop the proxy with a timeout before
127
+ exiting:
128
+
129
+ ```ruby
130
+ at_exit { proxy.stop(timeout: 10) }
131
+ ```
132
+
133
+ It waits until the server closes the connection or the timeout passes, and returns at once when not connected.
134
+ SIGINT stops the server immediately, which is convenient in development.
135
+
98
136
  ## How It Works
99
137
 
100
- 1. The gem creates a Unix socket for communication between Ruby and Node.js
101
- 2. When you call `debounce_activity`, it sends the event to the Node.js server
102
- 3. The Node.js server restarts a timer every time an event with a given activity_descriptor is received
103
- 4. When the timeout expires, it sends the event back to Ruby to be published
138
+ 1. The debounce server listens on a Unix socket and runs one lightweight timer per activity descriptor
139
+ 2. When you call `debounce_activity`, the proxy sends the descriptor, timeout and callback to the server
140
+ 3. Each new request for the same descriptor cancels its timer and starts a new one, keeping the latest callback
141
+ 4. When a timer expires, the server sends the callback back to the proxy, which invokes it
142
+
143
+ Several processes can connect to one server, for example every Puma worker of an application. Each process's
144
+ descriptors are debounced separately, even when two processes use the same descriptor: each gets its own callback,
145
+ and a process's pending callbacks are dropped when it disconnects.
146
+ When no server is reachable, the proxy invokes callbacks immediately.
147
+
148
+ ## Performance
149
+
150
+ Measured with `bin/benchmark`, which drives a server process over its socket. Medians of three runs on an Apple M4 Max,
151
+ Ruby 4.0.4, compared with the Node.js server from `debounced` 2.1.0 on Node.js 24.18.
152
+
153
+ | Measure | Ruby server (3.0) | Node.js server (2.1) |
154
+ |---|---|---|
155
+ | Throughput: 100,000 requests across 10,000 descriptors | 224,000 requests/s | 263,000 requests/s |
156
+ | Callback lateness after its timeout, median | 0.25 ms | 0.26 ms |
157
+ | Callback lateness, 99th percentile | 1.96 ms | 1.95 ms |
158
+ | Memory, idle | 27 MB | 48 MB |
159
+ | Memory, 100,000 pending timers | 156 MB | 174 MB |
160
+
161
+ To measure another server, pass its command: `bin/benchmark node path/to/server.mjs`.
104
162
 
105
163
  ## License
106
164
 
107
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
165
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require 'debounced'
6
+ require 'debounced/server'
7
+
8
+ # Write each log line as it happens; Ruby buffers stdout when it isn't a terminal (e.g. on Heroku)
9
+ $stdout.sync = true
10
+
11
+ config_file = nil
12
+ OptionParser.new do |opts|
13
+ opts.banner = 'Usage: debounced-server [--config FILE]'
14
+ opts.on('--config FILE', 'Ruby file that calls Debounced.configure') { |file| config_file = file }
15
+ end.parse!
16
+
17
+ load File.expand_path(config_file) if config_file
18
+
19
+ Debounced::Server.new(Debounced.configuration.socket_descriptor).listen
@@ -78,6 +78,7 @@ module Debounced
78
78
  rescue StandardError => e
79
79
  Debounced.configuration.logger.warn("Unable to invoke callback #{as_json}: #{e.message}")
80
80
  Debounced.configuration.logger.warn(e.backtrace.join("\n"))
81
+ Debounced.configuration.error_handler&.call(e, self)
81
82
  end
82
83
 
83
84
  private
@@ -0,0 +1,162 @@
1
+ require 'async'
2
+ require 'async/barrier'
3
+ require 'async/notification'
4
+ require 'async/queue'
5
+ require 'io/event'
6
+ require 'json'
7
+ require 'socket'
8
+
9
+ module Debounced
10
+ class Server
11
+ def initialize(socket_descriptor)
12
+ @socket_descriptor = socket_descriptor
13
+ @timers = {}
14
+ @writers = Async::Barrier.new
15
+ @queue = IO::Event::Timers.new
16
+ @timer_scheduled = Async::Notification.new
17
+ end
18
+
19
+ def listen
20
+ remove_stale_socket_file
21
+ Sync do |task|
22
+ @root = task
23
+ server = bind_owner_only
24
+ logger.info("#{self.class.name} listening on #{@socket_descriptor}")
25
+ task.async { run_timers }
26
+ task.async { drain_on_sigterm }
27
+ loop do
28
+ connection = server.accept
29
+ task.async { serve(connection) }
30
+ end
31
+ ensure
32
+ File.delete(@socket_descriptor) if server
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def bind_owner_only
39
+ previous_umask = File.umask(0o177)
40
+ UNIXServer.new(@socket_descriptor)
41
+ ensure
42
+ File.umask(previous_umask)
43
+ end
44
+
45
+ def serve(connection)
46
+ logger.info('Client connected')
47
+ outbox = Async::Queue.new
48
+ @timers[outbox] = {}
49
+ @writers.async { write_messages(connection, outbox) }
50
+ while (line = connection.gets(ServiceProxy::DELIMITER, chomp: true))
51
+ handle(line, outbox)
52
+ end
53
+ rescue IOError, SystemCallError => e
54
+ logger.warn("Client connection error: #{e.message}")
55
+ ensure
56
+ logger.info('Client disconnected')
57
+ @timers.delete(outbox)&.each_value(&:cancel!)
58
+ outbox.close
59
+ connection.close
60
+ stop_if_drained
61
+ end
62
+
63
+ def write_messages(connection, outbox)
64
+ outbox.each { |message| send_message(connection, message) }
65
+ rescue IOError, SystemCallError => e
66
+ logger.warn("Unable to write to client: #{e.message}")
67
+ end
68
+
69
+ def handle(line, outbox)
70
+ message = JSON.parse(line)
71
+ case message['type']
72
+ when 'debounceEvent' then debounce(message['data'], outbox)
73
+ when 'reset' then reset
74
+ else logger.warn("Unknown message: #{line}")
75
+ end
76
+ rescue JSON::ParserError => e
77
+ logger.warn("Unable to parse message: #{e.message}")
78
+ end
79
+
80
+ def debounce(data, outbox)
81
+ descriptor = data['descriptor']
82
+ timers = @timers.fetch(outbox)
83
+ timers.delete(descriptor)&.cancel!
84
+ if @draining
85
+ publish(descriptor, data['callback'], outbox)
86
+ return stop_if_drained
87
+ end
88
+
89
+ logger.debug { "Debouncing #{descriptor}" }
90
+ timers[descriptor] = @queue.after(data['timeout']) do
91
+ timers.delete(descriptor)
92
+ publish(descriptor, data['callback'], outbox)
93
+ stop_if_drained
94
+ end
95
+ @timer_scheduled.signal
96
+ end
97
+
98
+ def drain_on_sigterm
99
+ signals, signal_writer = IO.pipe
100
+ Signal.trap('TERM') { signal_writer.write_nonblock('.', exception: false) }
101
+ signals.read(1)
102
+ logger.info('Received SIGTERM; firing pending timers, then exiting')
103
+ @draining = true
104
+ stop_if_drained
105
+ end
106
+
107
+ def stop_if_drained
108
+ finish_draining if @draining && @timers.each_value.all?(&:empty?)
109
+ end
110
+
111
+ def finish_draining
112
+ @draining = false
113
+ @timers.each_key(&:close)
114
+ @writers.wait
115
+ @root.stop
116
+ end
117
+
118
+ def run_timers
119
+ loop do
120
+ wait_for_next_deadline
121
+ @queue.fire
122
+ end
123
+ end
124
+
125
+ def wait_for_next_deadline
126
+ interval = @queue.wait_interval
127
+ if interval.nil?
128
+ @timer_scheduled.wait
129
+ elsif interval.positive?
130
+ Async::Task.current.with_timeout(interval) { @timer_scheduled.wait }
131
+ end
132
+ rescue Async::TimeoutError
133
+ nil
134
+ end
135
+
136
+ def publish(descriptor, callback, outbox)
137
+ logger.debug { "Debounce period expired for #{descriptor}" }
138
+ outbox.push(type: 'publishEvent', callback:)
139
+ end
140
+
141
+ def reset
142
+ @timers.each_value { |timers| timers.each_value(&:cancel!).clear }
143
+ end
144
+
145
+ def send_message(connection, message)
146
+ connection.write(JSON.generate(message), ServiceProxy::DELIMITER)
147
+ end
148
+
149
+ def remove_stale_socket_file
150
+ return unless File.exist?(@socket_descriptor)
151
+
152
+ UNIXSocket.new(@socket_descriptor).close
153
+ raise SocketConflictError, "Another server is listening on #{@socket_descriptor}"
154
+ rescue Errno::ECONNREFUSED
155
+ File.delete(@socket_descriptor)
156
+ end
157
+
158
+ def logger
159
+ Debounced.configuration.logger
160
+ end
161
+ end
162
+ end
@@ -1,3 +1,4 @@
1
+ require 'concurrent/atomic/atomic_boolean'
1
2
  require 'socket'
2
3
  require 'json'
3
4
 
@@ -49,7 +50,7 @@ module Debounced
49
50
  end
50
51
  end
51
52
  rescue IOError, SystemCallError, NoServerError => e
52
- logger.warn("Unable to send #{activity_descriptor} to #{server_name} (#{e.message}); skipping debounce step.")
53
+ logger.warn("Unable to send #{activity_descriptor} (#{e.message}); skipping debounce step.")
53
54
  close
54
55
  callback.call
55
56
  end
@@ -85,12 +86,25 @@ module Debounced
85
86
  @listening = false
86
87
  end
87
88
 
88
- def stop
89
+ ###
90
+ # Stop listening. With a timeout, first wait up to that many seconds for the server to close the connection, which it
91
+ # does once it has drained after SIGTERM, so the callbacks still pending arrive before the process exits.
92
+ def stop(timeout: 0)
93
+ deadline = monotonic_now + timeout
94
+ sleep 0.05 while connected? && monotonic_now < deadline
89
95
  @abort_signal&.make_true
90
96
  end
91
97
 
92
98
  private
93
99
 
100
+ def connected?
101
+ listening && @mutex.synchronize { !@socket.nil? }
102
+ end
103
+
104
+ def monotonic_now
105
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
106
+ end
107
+
94
108
  def close
95
109
  @mutex.synchronize do
96
110
  return unless @socket
@@ -1,3 +1,3 @@
1
1
  module Debounced
2
- VERSION = "2.1.1"
2
+ VERSION = "4.0.0"
3
3
  end
data/lib/debounced.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'debounced/version'
2
- require 'debounced/railtie' if defined?(Rails)
3
2
  require 'debounced/no_server_error'
4
3
  require 'debounced/socket_conflict_error'
5
4
  require 'debounced/service_proxy'
@@ -21,7 +20,7 @@ module Debounced
21
20
  end
22
21
 
23
22
  class Configuration
24
- attr_accessor :socket_descriptor, :wait_timeout, :enable_trace_logging
23
+ attr_accessor :socket_descriptor, :wait_timeout, :enable_trace_logging, :error_handler
25
24
 
26
25
  def initialize
27
26
  @socket_descriptor = ENV['DEBOUNCED_SOCKET'] || '/tmp/app.debounceEvents'
@@ -32,7 +31,7 @@ module Debounced
32
31
  def logger
33
32
  return @logger if defined? @logger
34
33
 
35
- SemanticLogger.add_appender(file_name: 'debounced_proxy.log', formatter: :color)
34
+ SemanticLogger.add_appender(io: $stdout)
36
35
  SemanticLogger.default_level = ENV.fetch('LOG_LEVEL', 'info')
37
36
  @logger = SemanticLogger['ServiceProxy']
38
37
  end
metadata CHANGED
@@ -1,116 +1,150 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debounced
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Passero
8
- bindir: bin
8
+ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
- name: json
13
+ name: async
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '2.19'
18
+ version: '2.46'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '2.19'
25
+ version: '2.46'
26
26
  - !ruby/object:Gem::Dependency
27
- name: semantic_logger
27
+ name: concurrent-ruby
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '4.17'
32
+ version: '1.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '4.17'
39
+ version: '1.0'
40
40
  - !ruby/object:Gem::Dependency
41
- name: logger
41
+ name: io-event
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.21'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.21'
54
+ - !ruby/object:Gem::Dependency
55
+ name: json
42
56
  requirement: !ruby/object:Gem::Requirement
43
57
  requirements:
44
58
  - - ">="
45
59
  - !ruby/object:Gem::Version
46
- version: '0'
60
+ version: '2.19'
61
+ - - "<"
62
+ - !ruby/object:Gem::Version
63
+ version: '4.0'
47
64
  type: :runtime
48
65
  prerelease: false
49
66
  version_requirements: !ruby/object:Gem::Requirement
50
67
  requirements:
51
68
  - - ">="
52
69
  - !ruby/object:Gem::Version
53
- version: '0'
70
+ version: '2.19'
71
+ - - "<"
72
+ - !ruby/object:Gem::Version
73
+ version: '4.0'
54
74
  - !ruby/object:Gem::Dependency
55
- name: rspec
75
+ name: semantic_logger
56
76
  requirement: !ruby/object:Gem::Requirement
57
77
  requirements:
58
- - - "~>"
78
+ - - ">="
59
79
  - !ruby/object:Gem::Version
60
- version: '3.0'
61
- type: :development
80
+ version: '4.17'
81
+ - - "<"
82
+ - !ruby/object:Gem::Version
83
+ version: '6.0'
84
+ type: :runtime
62
85
  prerelease: false
63
86
  version_requirements: !ruby/object:Gem::Requirement
64
87
  requirements:
65
- - - "~>"
88
+ - - ">="
66
89
  - !ruby/object:Gem::Version
67
- version: '3.0'
90
+ version: '4.17'
91
+ - - "<"
92
+ - !ruby/object:Gem::Version
93
+ version: '6.0'
68
94
  - !ruby/object:Gem::Dependency
69
- name: debug
95
+ name: logger
70
96
  requirement: !ruby/object:Gem::Requirement
71
97
  requirements:
72
- - - "~>"
98
+ - - ">="
73
99
  - !ruby/object:Gem::Version
74
- version: '1.0'
100
+ version: '0'
101
+ type: :runtime
102
+ prerelease: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
75
105
  - - ">="
76
106
  - !ruby/object:Gem::Version
77
- version: 1.0.0
107
+ version: '0'
108
+ - !ruby/object:Gem::Dependency
109
+ name: rspec
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: '3.0'
78
115
  type: :development
79
116
  prerelease: false
80
117
  version_requirements: !ruby/object:Gem::Requirement
81
118
  requirements:
82
119
  - - "~>"
83
120
  - !ruby/object:Gem::Version
84
- version: '1.0'
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: 1.0.0
88
- description: Leverage JavaScript micro-event loop to debounce events in Ruby applications
121
+ version: '3.0'
122
+ description: Debounce Ruby callbacks across processes through a lightweight Ruby timer
123
+ server
89
124
  email:
90
125
  - gary@flytedesk.com
91
- executables: []
126
+ executables:
127
+ - debounced-server
92
128
  extensions: []
93
129
  extra_rdoc_files: []
94
130
  files:
95
131
  - CHANGELOG.md
96
132
  - README.md
133
+ - exe/debounced-server
97
134
  - lib/debounced.rb
98
135
  - lib/debounced/callback.rb
99
136
  - lib/debounced/callbackable.rb
100
- - lib/debounced/javascript/server.mjs
101
- - lib/debounced/javascript/service.mjs
102
137
  - lib/debounced/no_server_error.rb
103
- - lib/debounced/railtie.rb
138
+ - lib/debounced/server.rb
104
139
  - lib/debounced/service_proxy.rb
105
140
  - lib/debounced/socket_conflict_error.rb
106
141
  - lib/debounced/version.rb
107
- - lib/tasks/debounced.rake
108
- homepage: https://github.com/flytedesk/debounced
142
+ homepage: https://github.com/Flytedesk/debounced-rb
109
143
  licenses:
110
144
  - MIT
111
145
  metadata:
112
- source_code_uri: https://github.com/flytedesk/debounced
113
- changelog_uri: https://github.com/flytedesk/debounced/blob/main/CHANGELOG.md
146
+ source_code_uri: https://github.com/Flytedesk/debounced-rb
147
+ changelog_uri: https://github.com/Flytedesk/debounced-rb/blob/main/CHANGELOG.md
114
148
  rdoc_options: []
115
149
  require_paths:
116
150
  - lib
@@ -118,14 +152,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
152
  requirements:
119
153
  - - ">="
120
154
  - !ruby/object:Gem::Version
121
- version: 3.1.0
155
+ version: 3.3.0
122
156
  required_rubygems_version: !ruby/object:Gem::Requirement
123
157
  requirements:
124
158
  - - ">="
125
159
  - !ruby/object:Gem::Version
126
160
  version: '0'
127
161
  requirements: []
128
- rubygems_version: 4.0.10
162
+ rubygems_version: 4.0.20
129
163
  specification_version: 4
130
164
  summary: Efficient event debouncing in Ruby
131
165
  test_files: []
@@ -1,9 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import DebounceEventService from './service.mjs'
4
- let socketDescriptor = '/tmp/app.debounceEvents';
5
- if (process.argv.length > 2) {
6
- socketDescriptor = process.argv[2];
7
- }
8
-
9
- new DebounceEventService(socketDescriptor).listen();
@@ -1,167 +0,0 @@
1
- import net from 'net';
2
- import fs from 'fs';
3
-
4
- function log(message, ...args) {
5
- const timestamp = new Date().toISOString();
6
- console.log(`${timestamp} - ${message}`, ...args);
7
- }
8
-
9
- export default class DebounceService {
10
- constructor(socketDescriptor) {
11
- this._socketDescriptor = socketDescriptor;
12
- this._timers = new Map();
13
- this.publishEvent = this.publishEvent.bind(this);
14
- this.debounceEvent = this.debounceEvent.bind(this);
15
- this.reset = this.reset.bind(this);
16
- this.listen = this.listen.bind(this);
17
- this.handleError = this.handleError.bind(this);
18
- this.sendMessage = this.sendMessage.bind(this);
19
- this.onClientConnected = this.onClientConnected.bind(this);
20
- this.handleMessage = this.handleMessage.bind(this);
21
- this.configureServer();
22
- }
23
-
24
- onConnectionError(err) {
25
- log('DebounceService client connection error');
26
- this.handleError(err);
27
- }
28
-
29
- onClientDisconnected(socket) {
30
- log('DebounceService client disconnected');
31
- this._timers.get(socket).forEach(timerID => clearTimeout(timerID));
32
- this._timers.delete(socket);
33
- }
34
-
35
- handleMessage(message, socket) {
36
- try {
37
- const object = JSON.parse(message);
38
- if (object.type === 'debounceEvent') {
39
- this.debounceEvent(object.data, socket);
40
- } else if (object.type === 'reset') {
41
- this.reset();
42
- } else {
43
- log('DebounceService unknown message', message);
44
- }
45
- } catch (e) {
46
- log('unable to parse message', e);
47
- }
48
- }
49
-
50
- onClientConnected(socket) {
51
- log('DebounceService client connected');
52
- let connectionBuffer = '';
53
- this._timers.set(socket, new Map());
54
- socket.on('close', () => this.onClientDisconnected(socket));
55
- socket.on('error', this.onConnectionError);
56
- socket.setEncoding('utf8');
57
- socket.on('data', data => {
58
- log('DebounceService data received', data);
59
- connectionBuffer += data;
60
- const messages = connectionBuffer.split('\f');
61
- connectionBuffer = messages.pop();
62
- messages.forEach(message => this.handleMessage(message, socket));
63
- })
64
- }
65
-
66
- configureServer() {
67
- this.server = net.createServer(this.onClientConnected)
68
- this.server.on('error', (err) => {
69
- this.handleError(err);
70
- process.exit(1);
71
- });
72
- }
73
-
74
- cleanupDescriptor() {
75
- if (fs.existsSync(this._socketDescriptor)) {
76
- log('DebounceEventService removing socket file ', this._socketDescriptor);
77
- fs.unlinkSync(this._socketDescriptor);
78
- }
79
- }
80
-
81
- listen() {
82
- if (!fs.existsSync(this._socketDescriptor)) {
83
- this.bind();
84
- return;
85
- }
86
-
87
- const probe = net.connect(this._socketDescriptor);
88
- probe.on('connect', () => {
89
- probe.destroy();
90
- log('DebounceEventService another server is listening on', this._socketDescriptor);
91
- process.exit(1);
92
- });
93
- probe.on('error', (err) => {
94
- if (err.code !== 'ECONNREFUSED') {
95
- this.handleError(err);
96
- process.exit(1);
97
- }
98
- log('DebounceEventService removing stale socket file ', this._socketDescriptor);
99
- fs.unlinkSync(this._socketDescriptor);
100
- this.bind();
101
- });
102
- }
103
-
104
- bind() {
105
- const previousUmask = process.umask(0o177);
106
- this.server.listen(this._socketDescriptor, () => {
107
- log('DebounceService listening on', this._socketDescriptor);
108
- this.cleanupOnExit();
109
- });
110
- process.umask(previousUmask);
111
- }
112
-
113
- cleanupOnExit() {
114
- process.on('exit', (code) => {
115
- log(`Process exiting with code: ${code}`);
116
- this.server.close();
117
- this.cleanupDescriptor();
118
- });
119
-
120
- ['SIGTERM', 'SIGINT'].forEach((signal) => {
121
- process.on(signal, () => {
122
- log(`Process received ${signal}`);
123
- process.exit(0);
124
- });
125
- });
126
- }
127
-
128
- sendMessage(socket, message) {
129
- try {
130
- socket.write(message);
131
- socket.write("\f");
132
- } catch (err) {
133
- this.handleError(err);
134
- }
135
- }
136
-
137
- publishEvent(descriptor, callback, socket) {
138
- log(`Debounce period expired for ${descriptor} - sending to client`);
139
- const message = JSON.stringify({
140
- type: 'publishEvent',
141
- callback: callback
142
- });
143
- this.sendMessage(socket, message);
144
- }
145
-
146
- debounceEvent({ descriptor, timeout, callback }, socket) {
147
- const timers = this._timers.get(socket);
148
- clearTimeout(timers.get(descriptor));
149
-
150
- log("Debouncing", descriptor);
151
- timers.set(descriptor, setTimeout(() => {
152
- timers.delete(descriptor);
153
- this.publishEvent(descriptor, callback, socket);
154
- }, timeout * 1000));
155
- }
156
-
157
- reset() {
158
- this._timers.forEach(timers => {
159
- timers.forEach(timerID => clearTimeout(timerID));
160
- timers.clear();
161
- });
162
- }
163
-
164
- handleError(err) {
165
- log('\n\n######\nERROR: ', err);
166
- }
167
- }
@@ -1,9 +0,0 @@
1
- require "rails/railtie"
2
-
3
- module Debounced
4
- class Railtie < Rails::Railtie
5
- rake_tasks do
6
- load File.expand_path("../../tasks/debounced.rake", __FILE__)
7
- end
8
- end
9
- end
@@ -1,16 +0,0 @@
1
- namespace :debounced do
2
- desc 'Start the debounce server'
3
- task :server => :environment do
4
- require 'debounced'
5
-
6
- # Configuration is available because environment is loaded
7
- socket_descriptor = Debounced.configuration.socket_descriptor
8
-
9
- # Find the gem's JavaScript files
10
- gem_lib_path = Pathname.new(Gem::Specification.find_by_name('debounced').gem_dir).join('lib')
11
- js_path = gem_lib_path.join('debounced', 'javascript', 'server.mjs')
12
-
13
- # Exec replaces the current process with the node process
14
- exec("node #{js_path} #{socket_descriptor}")
15
- end
16
- end