]> Cypherpunks repositories - gostls13.git/commitdiff
net: TCPConn.SetNoDelay, back by popular demand
authorRuss Cox <rsc@golang.org>
Mon, 26 Jul 2010 23:19:39 +0000 (16:19 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 26 Jul 2010 23:19:39 +0000 (16:19 -0700)
R=r
CC=golang-dev
https://golang.org/cl/1880047

src/pkg/net/sock.go
src/pkg/net/tcpsock.go

index fbdb695839ed6f82d3166a876afb827537aa449a..d04d4dd7c2afc6ec3fdb296ef512c0b1562492d9 100644 (file)
@@ -129,6 +129,12 @@ func setKeepAlive(fd *netFD, keepalive bool) os.Error {
        return setsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive))
 }
 
+func setNoDelay(fd *netFD, noDelay bool) os.Error {
+       fd.incref()
+       defer fd.decref()
+       return setsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(noDelay))
+}
+
 func setLinger(fd *netFD, sec int) os.Error {
        var l syscall.Linger
        if sec >= 0 {
index d40035291da6f0ab0f2552d2944d44a8ea38859e..7a60cd2e7d3db671fdd5651b28c51a5c267a2847 100644 (file)
@@ -73,7 +73,7 @@ type TCPConn struct {
 
 func newTCPConn(fd *netFD) *TCPConn {
        c := &TCPConn{fd}
-       setsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1)
+       c.SetNoDelay(true)
        return c
 }
 
@@ -192,6 +192,17 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error {
        return setKeepAlive(c.fd, keepalive)
 }
 
+// SetNoDelay controls whether the operating system should delay
+// packet transmission in hopes of sending fewer packets
+// (Nagle's algorithm).  The default is true (no delay), meaning
+// that data is sent as soon as possible after a Write.
+func (c *TCPConn) SetNoDelay(noDelay bool) os.Error {
+       if !c.ok() {
+               return os.EINVAL
+       }
+       return setNoDelay(c.fd, noDelay)
+}
+
 // DialTCP is like Dial but can only connect to TCP networks
 // and returns a TCPConn structure.
 func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) {