github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xnet/no_delay.go (about)

     1  package xnet
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  type noDelayListener struct {
     8  	net.Listener
     9  }
    10  
    11  // NoDelayedListener wrap net.Listener accept only no_delay connection without Nagle algorithm,
    12  // so connection can send packet as soon as possible.
    13  func NoDelayedListener(ln net.Listener) net.Listener {
    14  	return &noDelayListener{ln}
    15  }
    16  
    17  func (n *noDelayListener) Accept() (net.Conn, error) {
    18  	conn, err := n.Listener.Accept()
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	if tcpConn, ok := conn.(*net.TCPConn); ok {
    23  		if err := tcpConn.SetNoDelay(true); err != nil {
    24  			return nil, err
    25  		}
    26  	}
    27  	return conn, nil
    28  }