Skip to content

Commit eb53436

Browse files
committed
Keep Thread as far from everything else as possible.
1 parent f772997 commit eb53436

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

lib/slack/real_time/concurrency/async.rb

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,28 @@ class Client < ::Async::WebSocket::Client
1313
class Socket < Slack::RealTime::Socket
1414
attr_reader :client
1515

16-
def start_async(client)
17-
@reactor = ::Async::Reactor.new
16+
def start_sync(client)
17+
start_reactor(client).wait
18+
end
1819

20+
def start_async(client)
1921
Thread.new do
20-
@reactor.run do |task|
21-
if client.run_ping?
22-
task.async do |subtask|
23-
subtask.annotate 'client keep-alive'
24-
25-
loop do
26-
subtask.sleep client.websocket_ping
27-
client.run_ping!
28-
end
29-
end
30-
end
22+
start_reactor(client)
23+
end
24+
end
25+
26+
def start_reactor(client)
27+
Async do |task|
28+
self.restart_async(client, @url)
3129

30+
if client.run_ping?
3231
task.async do |subtask|
33-
subtask.annotate 'client run-loop'
34-
client.run_loop
32+
subtask.annotate 'client keep-alive'
33+
34+
while true
35+
subtask.sleep client.websocket_ping
36+
run_ping!
37+
end
3538
end
3639
end
3740
end
@@ -40,9 +43,13 @@ def start_async(client)
4043
def restart_async(client, new_url)
4144
@url = new_url
4245
@last_message_at = current_time
43-
return unless @reactor
4446

45-
@reactor.async do
47+
if @client_task
48+
@client_task.stop
49+
end
50+
51+
@client_task = task.async do |subtask|
52+
subtask.annotate 'client run-loop'
4653
client.run_loop
4754
end
4855
end

0 commit comments

Comments
 (0)