Handle more than one request at a time
This commit is contained in:
parent
6297d1e3ca
commit
758e056952
32
server.go
32
server.go
|
@ -3,9 +3,11 @@ package gemini
|
|||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Server is a Gemini server.
|
||||
|
@ -33,13 +35,38 @@ func (s *Server) ListenAndServe() error {
|
|||
}
|
||||
|
||||
// Serve listens for requests on the provided listener.
|
||||
func (s *Server) Serve(ln net.Listener) error {
|
||||
func (s *Server) Serve(l net.Listener) error {
|
||||
var tempDelay time.Duration // how long to sleep on accept failure
|
||||
|
||||
for {
|
||||
rw, err := ln.Accept()
|
||||
rw, err := l.Accept()
|
||||
if err != nil {
|
||||
// If this is a temporary error, sleep
|
||||
if ne, ok := err.(net.Error); ok && ne.Temporary() {
|
||||
if tempDelay == 0 {
|
||||
tempDelay = 5 * time.Millisecond
|
||||
} else {
|
||||
tempDelay *= 2
|
||||
}
|
||||
if max := 1 * time.Second; tempDelay > max {
|
||||
tempDelay = max
|
||||
}
|
||||
log.Printf("gemini: Accept error: %v; retrying in %v", err, tempDelay)
|
||||
time.Sleep(tempDelay)
|
||||
continue
|
||||
}
|
||||
|
||||
// Otherwise, return the error
|
||||
return err
|
||||
}
|
||||
|
||||
tempDelay = 0
|
||||
go s.respond(rw)
|
||||
}
|
||||
}
|
||||
|
||||
// respond responds to a connection.
|
||||
func (s *Server) respond(rw net.Conn) {
|
||||
var resp *Response
|
||||
|
||||
if rawurl, err := readLine(rw); err != nil {
|
||||
|
@ -69,7 +96,6 @@ func (s *Server) Serve(ln net.Listener) error {
|
|||
|
||||
resp.Write(rw)
|
||||
rw.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// RequestInfo contains information about a request.
|
||||
|
|
Loading…
Reference in New Issue
Block a user