naia-lib / naia

a cross-platform (including Wasm!) networking library built in Rust. Intended to make multiplayer game development dead-simple & lightning-fast
Apache License 2.0
878 stars 58 forks source link

Server uses ~100% of CPU #85

Closed connorcarpenter closed 2 years ago

connorcarpenter commented 2 years ago

This is a flaw of the polling strategy used by the Server:

So currently in the sync code, if there are no messages found in the shared message channel, will just loop back and check again, again, again, until there is a message. This constant checking uses near 100% of CPU.

How does one fix this? One solution would be to remove the Sync code ... unfortunately, this would break the Bevy adapter. The other solution might be to constantly measure time between new incoming messages, and then at the average measured duration. This makes the Server potentially less responsive, but it will poll less frequently.

I need to learn what strategies have been used in the past to overcome this - it's a common, old problem for Servers. If anyone has any ideas or any learning resources .. please let me know!