Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #35 from dgrijalva/fix_32

Fix for #32: Cycle TCP_CORK/TCP_NOPUSH flag between messages
  • Loading branch information...
commit 75d1bb46767f45ffc245f4dd8d0c5eb4e0af7240 2 parents b64b23d + 42ba4fe
Scott White smw1218 authored
Showing with 24 additions and 0 deletions.
  1. +1 −0  server.go
  2. +19 −0 server_notwindows.go
  3. +4 −0 server_windows.go
1  server.go
View
@@ -249,6 +249,7 @@ func (srv *Server) handler(c net.Conn) {
// write response
if srv.sendfile {
res.Write(c)
+ srv.cycleNonBlock(c)
} else {
wbuf := bufio.NewWriter(c)
res.Write(wbuf)
19 server_notwindows.go
View
@@ -9,6 +9,7 @@ import (
// only valid on non-windows
func (srv *Server) setupNonBlockingListener(err error, l *net.TCPListener) error {
+ // FIXME: File() returns a copied pointer. we're leaking it. probably doesn't matter
if srv.listenerFile, err = l.File(); err != nil {
return err
}
@@ -23,3 +24,21 @@ func (srv *Server) setupNonBlockingListener(err error, l *net.TCPListener) error
}
return nil
}
+
+func (srv *Server) cycleNonBlock(c net.Conn) {
+ if srv.sendfile {
+ if tcpC, ok := c.(*net.TCPConn); ok {
+ if f, err := tcpC.File(); err == nil {
+ // f is a copy. must be closed
+ defer f.Close()
+ fd := int(f.Fd())
+ // Disable TCP_CORK/TCP_NOPUSH
+ syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, srv.sockOpt, 0)
+ // For TCP_NOPUSH, we need to force flush
+ c.Write([]byte{})
+ // Re-enable TCP_CORK/TCP_NOPUSH
+ syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, srv.sockOpt, 1)
+ }
+ }
+ }
+}
4 server_windows.go
View
@@ -9,3 +9,7 @@ import (
func (srv *Server) setupNonBlockingListener(err error, l *net.TCPListener) error {
return nil
}
+
+func (srv *Server) cycleNonBlock(c *net.Conn) {
+ // nuthin
+}
Please sign in to comment.
Something went wrong with that request. Please try again.