Skip to content

Commit f772997

Browse files
committed
Don't use exceptions for internal flow control.
1 parent f800262 commit f772997

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

lib/slack/real_time/client.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,29 @@ def run_loop
103103
end
104104
end
105105

106-
def run_ping!
106+
# Ensure the server is running, and ping the remote server if no other messages were sent.
107+
def keep_alive
107108
time_since_last_message = @socket.time_since_last_message
108-
return if time_since_last_message < websocket_ping
109-
raise Slack::RealTime::Client::ClientNotStartedError if !@socket.connected? || time_since_last_message > (websocket_ping * 2)
110109

111-
ping
112-
rescue Slack::RealTime::Client::ClientNotStartedError
113-
restart_async
110+
# If the server responded within the specified time, we are okay:
111+
return true if time_since_last_message < websocket_ping
112+
113+
# If the client is not connected or the server has not responded for a while:
114+
if !@socket.connected? || time_since_last_message > (websocket_ping * 2)
115+
return false
116+
end
117+
118+
# Kick off the next ping message:
119+
self.ping
120+
121+
return true
122+
end
123+
124+
# Check if the remote server is responsive, and if not, restart the connection.
125+
def run_ping!
126+
unless self.keep_alive
127+
restart_async
128+
end
114129
end
115130

116131
def run_ping?

0 commit comments

Comments
 (0)