Socket “Flush” by temporarily enabling NoDelay

前端 未结 4 1243
萌比男神i
萌比男神i 2020-12-17 02:14

Background

I have an implementation of an HTTP server in C#. Using ab I discovered a weird performance issue. Each request took 5 ms with Keep-Alive Off but 40 ms w

4条回答
  •  Happy的楠姐
    2020-12-17 02:25

    I tested this with Wireshark. Unfortunately,

    socket.NoDelay = true;
    socket.NoDelay = false;
    

    has no effect. Similarly,

    socket.NoDelay = true;
    socket.Send(new byte[0]);
    socket.NoDelay = false;
    

    also has no effect. From observed behaviour, it appears that the NoDelay property only affects the next call to Send with a non-empty buffer. In other words, you have to send some actual data before NoDelay will have any effect.

    Therefore, I conclude that there is no way to explicitly flush the socket if you don’t want to send any extra data.

    However, since you are writing an HTTP server, you may be able to use a few tricks:

    • For requests that are served using Transfer-Encoding: chunked, you can send the end-of-stream marker (the "\r\n0\r\n\r\n") with NoDelay = true.
    • If you are serving a file from the local filesystem, you will know when the file ends, so you could set NoDelay = true just before sending the last chunk.
    • For requests that are served using Content-Encoding: gzip, you can set NoDelay = true just before closing the gzip stream; the gzip stream will send some last bits before actually finishing and closing.

    I’m certainly going to add the above to my HTTP server now :)

提交回复
热议问题