forked from bittersweet/notifilter-receive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more events at the same time
Doing persisting to ES and checking the DB for notifications and processing them took a lot of time, and the previous setup meant that a lot of events got dropped. By using anotehr channel to push tasks onto, and starting 4 goroutines that will take work off there to process, it's been able to handle everything I throw at it. The buffered channel is key here.
- Loading branch information
1 parent
6b6982a
commit 767bba6
Showing
5 changed files
with
76 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'socket' | ||
require 'json' | ||
|
||
class Stats | ||
def initialize | ||
@batch_size = 20 | ||
@backlog = [] | ||
end | ||
|
||
def socket | ||
Thread.current[:statsd_socket] ||= UDPSocket.new | ||
end | ||
|
||
def track(message) | ||
@backlog << message | ||
# uncomment to enable buffering messages per 20 | ||
# if @backlog.size >= @batch_size | ||
# flush | ||
# end | ||
flush | ||
end | ||
|
||
def flush | ||
@backlog.each do |item| | ||
message = item.to_json | ||
# puts message | ||
puts socket.send(message, 0, "127.0.0.1", 8000) | ||
end | ||
@backlog.clear | ||
end | ||
end | ||
|
||
socket = UDPSocket.new | ||
jobs = [] | ||
25.times do | ||
jobs << Thread.new do | ||
s = Stats.new | ||
100.times do |i| | ||
data = { user_id: rand(10), created_at: Time.now } | ||
s.track({'identifier' => 'signup', 'data' => data}) | ||
end | ||
end | ||
end | ||
|
||
jobs.map(&:join) |