How to create a Rails / Ruby method similar to javascript / debounce throttle function

In our application, we show the callback route for the remote external service. When we receive a callback, we publish an update for client subscribers using Eventsource on the client / browser side and frantically on the server side. Sometimes, however, we get bombarded with callback requests from this external service, which leads to the fact that we publish a huge number of updates for the client. Is there a way on the Rails side similar to the javascript debounce function that will wait for the set time between callbacks received to post a message?

We already use sidekiq + threads, so we open offers using these tools.

+4
source share
1 answer

There is a Sidekiq-debounce gem.

Another approach (without such a gem) is to use Rails.cacheto run your execution only once in x time

delay = 1.minute
Rails.cache.fetch('unique-identifier-of-the-job', expires_in: delay) do
 YourActiveJobHere.set(wait: delay).perform_later('your-action')
end
+2
source

All Articles