Deadline

package main



import (
“net/http”
“time”
“log”
)



func main() {
srv:=&http.Server{
Addr:”:8876”,
ReadTimeout:1time.Second,
WriteTimeout:10
time.Second,
}
log.Println(srv.ListenAndServe())
}




断开是由server端断开还是client端?client设置超时时间就可以。server端如果用了nginx可以用nginx,
go 的话就用net.Conn的setdeadline
文档代码



// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future I/O, not just
// the immediately following call to Read or Write.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
SetDeadline(t time.Time) error



// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error

// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error


意思就是设置读写超时时间,过了时间没有读写就抛出timeout,如果时间内读写成功,则刷新过期时间。这应该就是你想要的。



Category golang