Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect on lost network #15

Open
dtonon opened this issue Nov 14, 2022 · 4 comments
Open

Reconnect on lost network #15

dtonon opened this issue Nov 14, 2022 · 4 comments

Comments

@dtonon
Copy link

dtonon commented Nov 14, 2022

Hi, which is the correct way to manage a reconnect when the connection is lost?
I tried this code:

def relay_connect(relay_host)

  ws = WebSocket::Client::Simple.connect(relay_host)

  ws.on :open do |event|
    # ....
  end

  ws.on :message do |msg|
    # ....
  end

  ws.on :error do |event|
    
    puts "Error, reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

  ws.on :close do |event|
    # p [:close, event.code, event.reason]
    puts "Reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

end

relay_connect(relay_host)

loop do
  STDIN.gets.strip
end

But it does not work.
It doesn't give any output when I block the network, while the CPU spikes up at 100%.
Any help :)

@unasuke
Copy link
Collaborator

unasuke commented Nov 16, 2022

while the CPU spikes up at 100%

@dtonon Which line did the CPU spiked? Could you recognize it? I tried your script on my local machine, it works and did not spike a CPU.

@dtonon
Copy link
Author

dtonon commented Nov 16, 2022

@unasuke sorry, my fault, the previus code works because I simplified it too much.
The loop with the STDIN.gets is inside the method, and now that I note the matter it obviously creates a recursive escalation of loops, stressing the CPU.

def relay_connect(relay_host)

  ws = WebSocket::Client::Simple.connect(relay_host)

  ws.on :open do |event|
    # ....
  end

  ws.on :message do |msg|
    # ....
  end

  ws.on :error do |event|
    
    puts "Error, reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

  ws.on :close do |event|
    # p [:close, event.code, event.reason]
    puts "Reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

  loop do
    ws.send STDIN.gets.strip
  end

end

relay_connect(relay_host)

Related: when I block the network (applying a firewall rule) I cannot see any "reconnecting" output, perhaps it is somehow buffered?

@dtonon
Copy link
Author

dtonon commented Nov 20, 2022

@unasuke even removing the ricorsive connection the hang problem remains.
I suspect that the culprit is the firewall (Little Snitch on macOS) that I use to simulate the blocked connection. As soon I deny the connection the CPU spikes at 100% and even if I remove the blocking rule the process stay stucked.
Instead if I disable the network at the system level, the CPU usage is normal and as soon I reactivate it the buffered messages are sent.
In both cases no close/error events are been generated, so the client cannot really understand that is offline.

Tested code:

require 'websocket-client-simple'

relay_host = "ws://127.0.0.1"

def relay_connect(relay_host)

  ws = WebSocket::Client::Simple.connect(relay_host)

  ws.on :open do |event|
    puts "---------- Open -----------------\n#{event.inspect}\n\n"
  end

  ws.on :message do |event|
    puts "---------- Event -----------------\n#{event.inspect}\n\n"
  end

  ws.on :error do |event|
    puts "---------- Error -----------------\n#{event.inspect}\n\n"
  end

  ws.on :close do |event|
    puts "---------- Close -----------------\n#{event.inspect}\n\n"
  end

  loop do
    ws.send STDIN.gets.strip
  end

end

relay_connect(relay_host)

@unasuke
Copy link
Collaborator

unasuke commented Nov 22, 2022

(Little Snitch on macOS)

It's great infomation! I tried that scripts on Ubuntu (WSL), but there is no problem.
So I guess macOS does other behavior. I will try this on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants