Skip to content

Commit a3fe309

Browse files
authored
Improved webhooks performance (#55)
1 parent 2c7ceaa commit a3fe309

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

lib/webhooks/sender.rb

+3-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Webhooks
44
# The class sends events in multi-threaded mode
55
class Sender
6-
MAX_THREADS = 8
6+
BATCH_SIZE = 8
77
URL = Settings.webhooks_url
88

99
def initialize(events)
@@ -14,9 +14,9 @@ def initialize(events)
1414
def send_events
1515
return unless URL
1616

17-
sort_events.each_value do |events|
17+
events.compact.each_slice(BATCH_SIZE) do |events_batch|
1818
thread = Thread.new do
19-
events.each do |event|
19+
events_batch.each do |event|
2020
Client.new(URL).send_payload(event)
2121
end
2222
end
@@ -30,20 +30,5 @@ def send_events
3030
private
3131

3232
attr_reader :events, :threads
33-
34-
def sort_events
35-
i = 1
36-
result = {}
37-
38-
events.compact.each do |event|
39-
arr = result[i]
40-
arr = [] if arr.nil?
41-
arr << event
42-
result[i] = arr
43-
i >= MAX_THREADS ? i = 1 : i += 1
44-
end
45-
46-
result
47-
end
4833
end
4934
end

0 commit comments

Comments
 (0)