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

时间:2016-04-21 22:44:58

标签: http go server

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?

2 个答案:

答案 0 :(得分:2)

在服务器上设置合理的超时,例如:

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

答案 1 :(得分:1)

因为我只想在写入停止时放弃连接(并且快速,以便我可以记录到目前为止收到的数据并允许客户端恢复),ReadTimeout对我来说不是正确的解决方案。 / p>

我在this gist找到了答案。您需要在连接上设置读取。

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
}