Refactor connection channel for delivering notifications

This commit is contained in:
Omar Roth 2019-06-03 13:12:06 -05:00
parent 84b2583973
commit d892ba6aa5
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2
2 changed files with 117 additions and 104 deletions

View file

@ -186,10 +186,21 @@ spawn do
end
end
notification_channels = [] of Channel(PQ::Notification)
PG.connect_listen(PG_URL, "notifications") do |event|
notification_channels.each do |channel|
channel.send(event)
connection_channel = Channel({Bool, Channel(PQ::Notification)}).new
spawn do
connections = [] of Channel(PQ::Notification)
PG.connect_listen(PG_URL, "notifications") { |event| connections.each { |connection| connection.send(event) } }
loop do
action, connection = connection_channel.receive
case action
when true
connections << connection
when false
connections.delete(connection)
end
end
end
@ -4469,15 +4480,7 @@ get "/api/v1/auth/notifications" do |env|
topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)
topics ||= [] of String
notification_channel = Channel(PQ::Notification).new
notification_channels << notification_channel
begin
create_notification_stream(env, proxies, config, Kemal.config, decrypt_function, topics, notification_channel)
rescue ex
ensure
notification_channels.delete(notification_channel)
end
create_notification_stream(env, proxies, config, Kemal.config, decrypt_function, topics, connection_channel)
end
post "/api/v1/auth/notifications" do |env|
@ -4486,15 +4489,7 @@ post "/api/v1/auth/notifications" do |env|
topics = env.params.body["topics"]?.try &.split(",").uniq.first(1000)
topics ||= [] of String
notification_channel = Channel(PQ::Notification).new
notification_channels << notification_channel
begin
create_notification_stream(env, proxies, config, Kemal.config, decrypt_function, topics, notification_channel)
rescue ex
ensure
notification_channels.delete(notification_channel)
end
create_notification_stream(env, proxies, config, Kemal.config, decrypt_function, topics, connection_channel)
end
get "/api/v1/auth/preferences" do |env|