OctopusDeploy / Halibut

| Public | A secure communication stack for .NET using JSON-RPC over SSL.
Other
12 stars 44 forks source link

Improve linux performance when listening on many ports #600

Closed LukeButters closed 5 months ago

LukeButters commented 5 months ago

Background

While trying to get 1000s of listening simulated tentacles working, I was running into issues in which Octopus was unable to communicate with the Tentacle. Eventually I discovered that the CPU usage of Tentacle Simulator was high despite it not doing anything. Turns out it was spending all its time in SpinWait.

This was done because TcpListener.Stop hangs if Accept is in progress https://github.com/dotnet/runtime/issues/24513 . Which is claimed to be fixed, although I see an absolute disaster of reverts so it is not clear if it is fixed.

Working on the assumption TcpListener.Stop still hangs if Accept is in progress, the new optional path is to:

Note that the new behaviour must be opted in by setting:

UseAsyncListener = true;

on HalibutTimeoutsAndLimits.

Results

Before

CPU usage was half my cores when listening on 1000 sockets.

After

CPU usage is near nothing.

Tentacle Simulator can actually be used with 1000s of listening tentacles.

How to review this PR

Quality :heavy_check_mark:

Pre-requisites