Implement Server read and write timeouts
This commit is contained in:
parent
42c95f8c8d
commit
7d470c5fb1
13
client.go
13
client.go
@ -65,13 +65,6 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
|||||||
return c.do(req, nil)
|
return c.do(req, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) deadline() time.Time {
|
|
||||||
if c.Timeout > 0 {
|
|
||||||
return time.Now().Add(c.Timeout)
|
|
||||||
}
|
|
||||||
return time.Time{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) do(req *Request, via []*Request) (*Response, error) {
|
func (c *Client) do(req *Request, via []*Request) (*Response, error) {
|
||||||
// Connect to the host
|
// Connect to the host
|
||||||
config := &tls.Config{
|
config := &tls.Config{
|
||||||
@ -89,10 +82,8 @@ func (c *Client) do(req *Request, via []*Request) (*Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Set connection deadline
|
// Set connection deadline
|
||||||
if deadline := c.deadline(); !deadline.IsZero() {
|
if d := c.Timeout; d != 0 {
|
||||||
if err := conn.SetDeadline(deadline); err != nil {
|
conn.SetDeadline(time.Now().Add(d))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the request
|
// Write the request
|
||||||
|
@ -12,6 +12,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var server gemini.Server
|
var server gemini.Server
|
||||||
|
server.ReadTimeout = 1 * time.Minute
|
||||||
|
server.WriteTimeout = 2 * time.Minute
|
||||||
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
|
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
14
server.go
14
server.go
@ -21,6 +21,13 @@ type Server struct {
|
|||||||
// Certificates contains the certificates used by the server.
|
// Certificates contains the certificates used by the server.
|
||||||
Certificates CertificateStore
|
Certificates CertificateStore
|
||||||
|
|
||||||
|
// ReadTimeout is the maximum duration for reading a request.
|
||||||
|
ReadTimeout time.Duration
|
||||||
|
|
||||||
|
// WriteTimeout is the maximum duration before timing out
|
||||||
|
// writes of the response.
|
||||||
|
WriteTimeout time.Duration
|
||||||
|
|
||||||
// CreateCertificate, if not nil, will be called to create a new certificate
|
// CreateCertificate, if not nil, will be called to create a new certificate
|
||||||
// if the current one is expired or missing.
|
// if the current one is expired or missing.
|
||||||
CreateCertificate func(hostname string) (tls.Certificate, error)
|
CreateCertificate func(hostname string) (tls.Certificate, error)
|
||||||
@ -159,6 +166,13 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) {
|
|||||||
|
|
||||||
// respond responds to a connection.
|
// respond responds to a connection.
|
||||||
func (s *Server) respond(conn net.Conn) {
|
func (s *Server) respond(conn net.Conn) {
|
||||||
|
if d := s.ReadTimeout; d != 0 {
|
||||||
|
conn.SetReadDeadline(time.Now().Add(d))
|
||||||
|
}
|
||||||
|
if d := s.WriteTimeout; d != 0 {
|
||||||
|
conn.SetWriteDeadline(time.Now().Add(d))
|
||||||
|
}
|
||||||
|
|
||||||
r := bufio.NewReader(conn)
|
r := bufio.NewReader(conn)
|
||||||
w := newResponseWriter(conn)
|
w := newResponseWriter(conn)
|
||||||
// Read requested URL
|
// Read requested URL
|
||||||
|
Loading…
Reference in New Issue
Block a user