Skip to content

Commit

Permalink
Close socket, terminate reactorm, closes #222.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Sep 5, 2018
1 parent b1b681a commit 6df313c
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions lib/slack/real_time/concurrency/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class Socket < Slack::RealTime::Socket
attr_reader :client

def start_async(client)
Thread.new do
::Async::Reactor.run do
@thread = Thread.new do
@reactor = ::Async::Reactor.new
@reactor.run do
client.run_loop
end
end
Expand All @@ -26,7 +27,11 @@ def connect!
end

def close
@driver.close
logger.warn "close: #{self}"
close_driver
close_socket
stop_reactor
terminate_thread
super
end

Expand All @@ -38,6 +43,34 @@ def run_loop

protected

def close_driver
logger.warn "close_driver: #{@driver}"
return unless @driver
@driver.close
@driver = nil
end

def close_socket
logger.warn "close_socket: #{@socket}"
return unless @socket
@socket.close
@socket = nil
end

def stop_reactor
logger.warn "stop_reactor: #{@reactor}"
return unless @reactor
@reactor.stop
@reactor = nil
end

def terminate_thread
logger.warn "terminate_thread: #{@thread}"
return unless @thread
@thread.kill
@thread = nil
end

def build_ssl_context
OpenSSL::SSL::SSLContext.new(:TLSv1_2_client).tap do |ctx|
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER)
Expand All @@ -50,8 +83,13 @@ def build_endpoint
endpoint
end

def connect_socket
build_endpoint.connect
end

def connect
@driver = Client.new(build_endpoint.connect, url)
@socket = connect_socket
@driver = Client.new(@socket, url)
end
end
end
Expand Down

0 comments on commit 6df313c

Please sign in to comment.