JustinHK
JustinHK

Reputation: 768

What is the proper way to detect a dropped connection in Go?

I am working with a Go HTTP server implementation that reads an upload from a mobile client. However, I'm experiencing problems where because of a long keep-alive, the server will hang reading the request buffer for quite a long time if the mobile client goes offline (as often happens).

What is the proper way to detect a dropped connection and close the input buffer?

Upvotes: 1

Views: 1873

Answers (2)

JustinHK
JustinHK

Reputation: 768

Because I wanted to drop the connection only when writes stop (and quickly, so that I can record the data received so far and allow the client to resume), the ReadTimeout isn't the right solution for me.

I found the answer in this gist. You need to set a read on the connection itself.

package nettimeout

import (
    "net"
    "time"
)

// Listener wraps a net.Listener, and gives a place to store the timeout
// parameters. On Accept, it will wrap the net.Conn with our own Conn for us.
type Listener struct {
    net.Listener
    ReadTimeout  time.Duration
    WriteTimeout time.Duration
}

func (l *Listener) Accept() (net.Conn, error) {
    c, err := l.Listener.Accept()
    if err != nil {
        return nil, err
    }
    tc := &Conn{
        Conn:         c,
        ReadTimeout:  l.ReadTimeout,
        WriteTimeout: l.WriteTimeout,
    }
    return tc, nil
}

// Conn wraps a net.Conn, and sets a deadline for every read
// and write operation.
type Conn struct {
    net.Conn
    ReadTimeout  time.Duration
    WriteTimeout time.Duration
}

func (c *Conn) Read(b []byte) (int, error) {
    err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
    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))
    if err != nil {
        return 0, err
    }
    return c.Conn.Write(b)
}

func NewListener(addr string, readTimeout, writeTimeout time.Duration) (net.Listener, error) {
    l, err := net.Listen("tcp", addr)
    if err != nil {
        return nil, err
    }

    tl := &Listener{
        Listener:     l,
        ReadTimeout:  readTimeout,
        WriteTimeout: writeTimeout,
    }
    return tl, nil
}

Upvotes: 1

OneOfOne
OneOfOne

Reputation: 99234

Set a reasonable timeout on the server, for example:

srv := &http.Server{
    Addr: ":443",
    ReadTimeout: time.Minute * 2,
    WriteTimeout: time.Minute * 2,
}
log.Fatal(srv.ListenAndServeTLS(certFile, keyFile))

Upvotes: 2

Related Questions