From af6e65a8bbd67e4b444da779b8bdcf9ed7bc30ab Mon Sep 17 00:00:00 2001 From: Justin Henck Date: Tue, 10 May 2016 08:17:34 -0400 Subject: [PATCH] Support 0 as a "no timeout" setting (#46) * Support 0 as a "no timeout" setting If you are working behind a load balancer the load balancer might handle this and send an EOF. In that case, you want to have no read and write timeouts at all, so we should support the 0 setting as "unlimited" just like the Connection itself. * Change to empty time struct * Update the flag description for timeout --- cmd/tusd/main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/tusd/main.go b/cmd/tusd/main.go index 6ab7e1a..604cc61 100644 --- a/cmd/tusd/main.go +++ b/cmd/tusd/main.go @@ -55,7 +55,7 @@ func init() { flag.StringVar(&dir, "dir", "./data", "Directory to store uploads in") flag.Int64Var(&storeSize, "store-size", 0, "Size of space allowed for storage") flag.StringVar(&basepath, "base-path", "/files/", "Basepath of the HTTP server") - flag.Int64Var(&timeout, "timeout", 30*1000, "Read timeout for connections in milliseconds") + flag.Int64Var(&timeout, "timeout", 30*1000, "Read timeout for connections in milliseconds. A zero value means that reads will not timeout") flag.StringVar(&s3Bucket, "s3-bucket", "", "Use AWS S3 with this bucket as storage backend (requires the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables to be set)") flag.StringVar(&hooksDir, "hooks-dir", "", "") flag.BoolVar(&version, "version", false, "Print tusd version information") @@ -255,18 +255,32 @@ type Conn struct { } func (c *Conn) Read(b []byte) (int, error) { - err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) + var err error + if c.ReadTimeout > 0 { + err = c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) + } else { + err = c.Conn.SetReadDeadline(time.Time{}) + } + if err != nil { return 0, err } + return c.Conn.Read(b) } func (c *Conn) Write(b []byte) (int, error) { - err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout)) + var err error + if c.WriteTimeout > 0 { + err = c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout)) + } else { + err = c.Conn.SetWriteDeadline(time.Time{}) + } + if err != nil { return 0, err } + return c.Conn.Write(b) }