[서버] nagle 알고리즘

2015. 9. 13. 22:44프로그래밍/서버

728x90
728x90

정의

IP 네트워크에서 데이터는 몇 곂의 헤더로 캡슐화되어 목적지로 전해진다.

이 헤더들의 용량도 제법 크고, 적은 데이터를 보내게 되면 배보다 배꼽이 커진다!!


보낼 수 있는 데이터를 바로 패킷으로 만들지 않고 가능한 모아서 더 큰 패킷으로

만들어 한번에 보내면 효율적으로 전송할 수 있다.


네이글 알고리즘은 이것을 실제로 구현한 네트워크 전송 알고리즘이다.


동작원리

네이글 알고리즘 수도코드
1
2
3
4
5
6
7
8
9
10
11
12
#define MSS "maximum segment size"
if there is new data to send
  if the window size >= MSS and available data is >= MSS
    send complete MSS segment now
  else
    if there is unconfirmed data still in the pipe
      enqueue data in the buffer until an acknowledge is received
    else
      send data immediately
    end if
  end if
end if
cs


경우 1. 상대방이 받을 수 있는 사이즈(window size)와 내가 보낼 데이터가 MSS보다 크다면 문제 없이 바로 전송.

경우 2. 더 이상 SEND 처리할 데이터가 없다면 현재 있는 것 그대로 보낸다.

경우 3. 작은 패킷이 여러개 보내질 때 nagle off 상태 일 때는 ACK를 기다리지 않고 바로 전송한다.

nagle on일때는 ACK가 올떄까지 전송하지 않고 ACK가 오는 시점에 버퍼의 데이터를 패킷으로 만들어 전송한다.


ACK를 기다리는 지연방식으로 네이글을 작은 패킷을 한번에 보내 대여폭 면에서 효율을 얻을수 있다.

하지만 이런 지연 방식은 결론적으로 전송속도가 느려지는 결과가 생길 수 있다.


C++ Scoket에서 Nagle 옵션

1
2
3
4
5
6
7
int setsockopt(
  _In_       SOCKET s,
  _In_       int    level,
  _In_       int    optname,
  _In_ const char   *optval,
  _In_       int    optlen
);
cs


optname에 TCP_NODELAY를 써주면 된다.


TCP_NODELAY

The TCP_NODELAY option is specific to TCP/IP service providers. The Nagle algorithm is disabled if the TCP_NODELAY option is enabled (and vice versa). The process involves buffering send data when there is unacknowledged data already in flight or buffering send data until a full-size packet can be sent. It is highly recommended that TCP/IP service providers enable the Nagle Algorithm by default, and for the vast majority of application protocols the Nagle Algorithm can deliver significant performance enhancements. However, for some applications this algorithm can impede performance, and TCP_NODELAY can be used to turn it off. These are applications where many small messages are sent, and the time delays between the messages are maintained. Application writers should not set TCP_NODELAY unless the impact of doing so is well-understood and desired because setting TCP_NODELAY can have a significant negative impact on network and application performance.

네이글 알고리즘은 기본적인 소캣 통신에선 사용되게 세팅되어 있다.

MSDN : https://msdn.microsoft.com/en-us/library/windows/desktop/ms740476(v=vs.85).aspx

728x90
반응형

'프로그래밍 > 서버' 카테고리의 다른 글

[TCP/IP] 네트워크 스택 이해하기  (0) 2016.01.04
[참고] cgcii 홈페이지. 열심히 읽어보자!  (0) 2015.10.26
[서버] 홀펀칭  (4) 2015.09.13
[서버] UDP 패킷 손실에 대하여  (0) 2015.09.12
[서버] TTL  (0) 2015.09.12
TCP Max Packet  (0) 2015.09.11
리눅스 vim cheat sheet  (2) 2015.09.05