Browse Source

Use new net timeout API.

Andy Balholm 7 years ago
parent
commit
aed3c63345
2 changed files with 9 additions and 6 deletions
  1. 4 2
      response_test.go
  2. 5 4
      server.go

+ 4 - 2
response_test.go

@@ -11,6 +11,8 @@ import (
 	"testing"
 )
 
+const serverAddr = "localhost:11344"
+
 // REQMOD example 2 from RFC 3507, adjusted for order of headers, etc.
 func TestREQMOD2(t *testing.T) {
 	request :=
@@ -49,9 +51,9 @@ func TestREQMOD2(t *testing.T) {
 			"\r\n"
 
 	HandleFunc("/server", HandleREQMOD2)
-	go ListenAndServe(":11344", nil)
+	go ListenAndServe(serverAddr, nil)
 
-	conn, err := net.Dial("tcp", "localhost:11344")
+	conn, err := net.Dial("tcp", serverAddr)
 	if err != nil {
 		t.Fatalf("could not connect to ICAP server on localhost")
 	}

+ 5 - 4
server.go

@@ -14,6 +14,7 @@ import (
 	"net"
 	"net/http"
 	"runtime/debug"
+	"time"
 )
 
 // Objects implementing the Handler interface can be registered
@@ -115,8 +116,8 @@ func (c *conn) serve() {
 type Server struct {
 	Addr         string  // TCP address to listen on, ":1344" if empty
 	Handler      Handler // handler to invoke
-	ReadTimeout  int64   // the net.Conn.SetReadTimeout value for new connections
-	WriteTimeout int64   // the net.Conn.SetWriteTimeout value for new connections
+	ReadTimeout  time.Duration
+	WriteTimeout time.Duration
 }
 
 // ListenAndServe listens on the TCP network address srv.Addr and then
@@ -154,10 +155,10 @@ func (srv *Server) Serve(l net.Listener) error {
 			return e
 		}
 		if srv.ReadTimeout != 0 {
-			rw.SetReadTimeout(srv.ReadTimeout)
+			rw.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
 		}
 		if srv.WriteTimeout != 0 {
-			rw.SetWriteTimeout(srv.WriteTimeout)
+			rw.SetWriteDeadline(time.Now().Add(srv.WriteTimeout))
 		}
 		c, err := newConn(rw, handler)
 		if err != nil {