When should I use TCP_NODELAY and when TCP_CORK?

前端 未结 4 1416
余生分开走
余生分开走 2020-11-30 18:07

I understood that both of them disable Nagle\'s algorithm.

When should/ shouldn\'t I use each one of them?

4条回答
  •  [愿得一人]
    2020-11-30 18:41

    It's an optimisation, so like any optimisation:

    1. Do not use it
    2. Wait until performance becomes a problem, then having determined that socket latency is definitely the cause of it, and testing proves that this will definitely fix it, AND this is the easiest way of fixing it, do it.

    Basically the aim is to avoid having to send out several frames where a single frame can be used, with sendfile() and its friends.

    So for example, in a web server, you send the headers followed by the file contents, the headers will be assembled in-memory, the file will then be sent directly by the kernel. TCP_CORK allows you to have the headers and the beginning of the file sent in a single frame, even with TCP_NODELAY, which would otherwise cause the first chunk to be sent out immediately.

提交回复
热议问题