Documentation ¶
Overview ¶
Package nserv (nice server) provides a variation of the standard http.Server implementation enhanced with graceful exit and throttling.
Throttling enables you to limit number of simultaneous connections the server accepts. This limit can be changed even after the server had been started. Graceful exit means you can signal the server to stop and at that point the server stops accepting new connections. Active connections run their natural course and only after all connections are closed the server shuts down.
Usage:
import "gopkg.in/kornel661/nserv.v1"
or
go get gopkg.in/kornel661/nserv.v1
Replace v1 by the version you need.
For up-to-date changelog and features list see [README] (https://github.com/kornel661/nserv/blob/master/README.md).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( DefaultReadTimeout = 60 * time.Second // default ReadTimeout set by the ListenAndServe methods DefaultWriteTimeout = 60 * time.Second // default WriteTimeout set by the ListenAndServe methods DefaultMaxConns = 1000 // default MaxConns set by the ListenAndServe methods )
Some default values set by the ListenAndServe method family. Feel free to modify these variables, e.g.,
nserv.DefaultMaxConns = 100 srv.ListenAndServe()
Or, alternatively (arguably a better solution if you're writing a 'serious' server), use srv.Listen directly or reimplement ListenAndServe.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct { http.Server // standard net.Server functionality InitialMaxConns int // initial limit on simultaneous connections // contains filtered or unexported fields }
Server with graceful exit and throttling.
Server is an extension of http.Server from the standard library (its API is a superset of that of http.Server).
Example ¶
// initialize database, etc. defer func() { // do clean-up (close DB connections, etc.) }() // set-up server: srv := &nserv.Server{} srv.Addr = "localhost:12345" http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) }) // catch signals: signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt, os.Kill) go func() { <-signals log.Println("Caught signal. Shutting down gracefully.") srv.Stop() }() // start serving: log.Printf("Serving at http://%s\n", srv.Addr) if err := srv.ListenAndServe(); err != nil { log.Println(err) }
Output:
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":http" is used.
If either of ReadTimeout, WriteTimeout or MaxConns is 0, it's going to be set to a 'sane' default value, see the corresponding Default... variable.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming TLS connections.
Filenames containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.
If srv.Addr is blank, ":https" is used.
If either of ReadTimeout, WriteTimeout or MaxConns is 0, it's going to be set to a 'sane' default value, see the corresponding Default... variable.
func (*Server) MaxConns ¶
MaxConns sets new throttling limit (max number of simultaneous connections), returns number of free slots for incoming connections. For n < 0 doesn't change the limit. See limitnet.ThrottledListener for more detailed description.
Won't return until srv.Serve is called.
func (*Server) Serve ¶
Serve accepts incoming connections on the Listener listn (wrapped with ThrottledListener from the gopkg.in/kornel661/limitnet.v1 package), creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them. Don't close listn. Rather use srv.Stop() method to exit gracefully. Serve returns on unrecoverable errors and when the server is explicitly stopped by srv.Stop(). By the time Serve returns the listener listn is closed.
type TCPKeepAliveListener ¶
type TCPKeepAliveListener struct {
*net.TCPListener
}
TCPKeepAliveListener sets TCP keep-alive timeouts on accepted connections. It's used by ListenAndServe and ListenAndServeTLS so dead TCP connections (e.g. closing laptop mid-download) eventually go away.