Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [#226](https://github.com/slack-ruby/slack-ruby-client/pull/226), [#232](https://github.com/slack-ruby/slack-ruby-client/pull/232), [#236](https://github.com/slack-ruby/slack-ruby-client/pull/236), [#234](https://github.com/slack-ruby/slack-ruby-client/pull/234): Added periodic ping that reconnects on failure - [@RodneyU215](https://github.com/RodneyU215), [@dblock](https://github.com/dblock), [@ioquatix](https://github.com/ioquatix).
* [#242](https://github.com/slack-ruby/slack-ruby-client/pull/242): Added `thread_ts` option to `chat_postEphemeral` - [@dblock](https://github.com/dblock).
* [#242](https://github.com/slack-ruby/slack-ruby-client/pull/242): Added `apps_uninstall` to Web API - [@dblock](https://github.com/dblock).
* [#244](https://github.com/slack-ruby/slack-ruby-client/pull/244): Implementing #restart for the celluloid socket class - [@RodneyU215](https://github.com/RodneyU215).
* Your contribution here.

### 0.13.1 (2018/9/30)
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def run_loop
def run_ping!
time_since_last_message = @socket.time_since_last_message
return if time_since_last_message < websocket_ping
raise Slack::RealTime::Client::ClientNotStartedError if time_since_last_message > (websocket_ping * 2)
raise Slack::RealTime::Client::ClientNotStartedError if !@socket.connected? || time_since_last_message > (websocket_ping * 2)

ping
rescue Slack::RealTime::Client::ClientNotStartedError
Expand Down
37 changes: 23 additions & 14 deletions lib/slack/real_time/concurrency/celluloid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ def run_loop
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE => e
logger.debug("#{self.class}##{__method__}") { e }
driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'server closed connection')) unless @closing
ensure
begin
current_actor.terminate if current_actor.alive?
rescue StandardError
nil
end
end

def disconnect!
super
@ping_timer.cancel if @ping_timer
end

def close
Expand All @@ -60,7 +59,7 @@ def read

def handle_read(buffer)
logger.debug("#{self.class}##{__method__}") { buffer }
driver.parse buffer
driver.parse buffer if driver
end

def write(data)
Expand All @@ -71,23 +70,33 @@ def write(data)
def start_async(client)
@client = client
Actor.new(future.run_client_loop)
Actor.new(future.run_ping_loop)
end

def run_client_loop
if @client.run_ping?
current_actor.every @client.websocket_ping do
@client.run_ping!
end
end

@client.run_loop
rescue StandardError => e
logger.debug("#{self.class}##{__method__}") { e }
raise e
end

def run_ping_loop
return unless @client.run_ping?

@ping_timer = every @client.websocket_ping do
@client.run_ping!
end
end

def restart_async(client, new_url)
@last_message_at = current_time
@url = new_url
@client = client
Actor.new(future.run_client_loop)
end

def connected?
!@connected.nil?
!@connected.nil? && !@driver.nil?
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/real_time/concurrency/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def restart_async(client, new_url)

def disconnect!
super
EventMachine.stop if @thread
EventMachine.stop_event_loop if EventMachine.reactor_running?
@thread = nil
end

Expand Down
19 changes: 18 additions & 1 deletion spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,29 @@ def stop_server
@reply_to = nil
client.on :pong do |data|
@reply_to = data.reply_to
queue.push nil
client.stop!
end
start_server
wait_for_server
queue.pop_with_timeout(5)
expect(@reply_to).to be 1
end
it 'rebuilds the websocket connection when dropped' do
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super happy with this test, but it's been the most consistent way I've been able to validate that the reconnect works as expected for each of the 3 supported concurrency libraries.

@reply_to = nil
client.on :pong do |data|
@reply_to = data.reply_to
if @reply_to == 1
client.instance_variable_get(:@socket).close
else
expect(@reply_to).to be 2
queue.push nil
client.stop!
end
end
start_server
queue.pop_with_timeout(10)
queue.pop_with_timeout(10)
end
end

context 'with websocket_ping not set' do
Expand Down