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

[question] High CPU load on dial #603

Closed
l4zygreed opened this issue Jun 25, 2020 · 5 comments
Closed

[question] High CPU load on dial #603

l4zygreed opened this issue Jun 25, 2020 · 5 comments
Labels

Comments

@l4zygreed
Copy link

While testing my code for websocket recreation to server, I noticed that when I dial to the server which is offline

client, _, err = websocket.DefaultDialer.Dial(url, nil)

the Dial function cause CPU ~100% load until connection or timeout.

Can I somehow decrease CPU load when dial?

Versions

Go version: 1.14.4

package version: v1.4.1

2,6 GHz 6-Core Intel Core i7

@AllenX2018
Copy link

Hello~ Can you provide detailed codes and error information?

@l4zygreed
Copy link
Author

l4zygreed commented Jul 4, 2020

server restarts sometimes so I need to reconnect to it
I use code kinda like this

ws.Dialer = websocket.Dialer{
	Proxy:   http.ProxyFromEnvironment,
	HandshakeTimeout: 5 * time.Second,
}
for err != nil || ws.Client == nil {
	ws.Client, _, err = ws.Dialer.Dial(url, nil)
	if err != nil {
		log.Println("Server not available:", err)
	}
	time.Sleep(10 * time.Second)
}

error is

2020/07/04 17:58:44 Server not available: dial tcp *ip here*:9090: i/o timeout
2020/07/04 17:58:59 Server not available: dial tcp *ip here*:9090: i/o timeout

when server in shutdown Dialer.Dial() try to connect to server and loads CPU to 99-100% until timeout.
But I need to have several processes with websockets that should be reconnected to different servers in future.
I want to reduce CPU load when websockets trying connect to dead server.

Is there a way to lower CPU load or a better solution for waiting the server come alive?

@ghost
Copy link

ghost commented Aug 18, 2020

Is other code concurrently accessing ws.Client? Run the application with the race detector and fix any reported errors.

@IngCr3at1on
Copy link

It's very possible that the issue here is related to the infinite loop in time.Sleep; there is a known issue that both time.Sleep and time.Ticker can result in high CPU usage, golang/go#27707

If you check the comments this has been at least partially addressed for time.Ticker but not time.Sleep. So I'd recommend the following:

  • ensure you are using a current go version (1.14 or later)
  • replace sleep usage with a ticker
  • run race detector as suggested by @TaylorRex and address anything it presents.
  • reply here if issue persists

@ghost
Copy link

ghost commented Sep 30, 2020

@l4zygreed Did you run the application with the race detector and fix and reported problems?

Is the goroutine that calls ws.Dialer.Dial the only application goroutine? If not, is there evidence that ws.Dialer.Dial is using the CPU time and not some other goroutine?

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

No branches or pull requests

3 participants