gracefulhttp

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 6 Imported by: 0

README

gracefulhttp

GitHub go.mod Go version GitHub Actions Workflow Status Go Reference codecov Go Report Card GitHub License

This package extends the functionality of the standard http.Server in Go to include best practice configurations and graceful shutdown.

  • Smoothly stop accepting new connections while processing existing requests to completion.
  • Set a customizable timeout for graceful shutdown, with a default of 5 seconds.
  • Forcibly close ongoing connections after the timeout expires.
  • Support graceful shutdown for HTTP and HTTPS servers.
  • Apply best practice configurations inspired by Cloudflare for timeout management and TLS setup (Read more).

Installation

go get github.com/aoliveti/gracefulhttp

Usage

package main

import (
	"context"
	"errors"
	"log"
	"net/http"
	"os/signal"

	"github.com/aoliveti/gracefulhttp"
	"golang.org/x/sys/unix"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), unix.SIGINT, unix.SIGTERM)
	defer cancel()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("Hello world!"))
	})

	srv := gracefulhttp.Bind(":8080", nil)

	log.Println("starting server...")

	err := srv.ListenAndServeWithShutdown(ctx)
	if err != nil && !errors.Is(err, context.DeadlineExceeded) {
		log.Fatal(err)
	}

	log.Println("graceful shutdown completed successfully")
}

You can instantiate a GracefulServer in two different ways:

gracefulhttp.GracefulServer{
    Server: http.Server{
        Addr:    ":8080",
        Handler: nil,
    },
}

or by using the Bind() function:

func Bind(addr string, handler http.Handler) *GracefulServer

Listen and serve

To start the HTTP server with the provided address and handler, you need to use this function:

func (s *GracefulServer) ListenAndServeWithShutdown(ctx context.Context, opts ...GracefulServerOption) error

If instead you need to start a server that accepts connections over HTTPS, you must ensure to provide files containing a certificate and matching private key for the server. You should use this function:

func (s *GracefulServer) ListenAndServeTLSWithShutdown(ctx context.Context, certFile string, keyFile string, opts ...GracefulServerOption) error

It's possible to pass options to set timeouts and TLS configuration. Here's a summary table:

Option Description
WithShutdownTimer Sets the timeout for a graceful shutdown, after which all active connections will be forcibly closed
WithCloudflareTimeouts Applies timeout patches to the server, implementing best practice configurations inspired by Cloudflare
WithCloudflareTLSConfig Applies TLS configuration patches to the server, implementing best practice configurations inspired by Cloudflare
WithTLSConfig Sets the provided TLS configuration

Build and Test

Building the Project

You can build the project using the provided Makefile. Simply run:

make

Running Tests

To run tests, use the following command:

make test

Before running the tests, a certificate and key are generated using OpenSSL and placed in the certs directory. The generated certificate and key are used for testing HTTPS functions.

For detailed documentation on OpenSSL, visit https://www.openssl.org/

License

The library is released under the MIT license. See LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GracefulServer

type GracefulServer struct {
	http.Server
	// contains filtered or unexported fields
}

A GracefulServer is an extension of the http.Server that enables graceful shutdown. It allows the server to smoothly stop accepting new connections while processing existing requests to completion within a specified timeout. After the timeout, ongoing connections will be forcibly closed. The default timeout is set to 5 seconds

func Bind

func Bind(addr string, handler http.Handler) *GracefulServer

Bind returns a new GracefulServer configured with the provided address and handler.

func (*GracefulServer) ListenAndServeTLSWithShutdown

func (s *GracefulServer) ListenAndServeTLSWithShutdown(ctx context.Context, certFile string, keyFile string, opts ...GracefulServerOption) error

ListenAndServeTLSWithShutdown starts a http.Server with the provided address, handler, certificate, and key. It behaves similarly to [ListenAndServeWithShutdown] but for HTTPS connections. For additional details, refer to the documentation of [ListenAndServeWithShutdown].

func (*GracefulServer) ListenAndServeWithShutdown

func (s *GracefulServer) ListenAndServeWithShutdown(ctx context.Context, opts ...GracefulServerOption) error

ListenAndServeWithShutdown starts a http.Server with the given address and handler. It blocks until the context is canceled or an error occurs. If the context is canceled, the server will attempt a graceful shutdown. If the graceful shutdown exceeds the provided timeout, the server will be forcefully closed. The default timeout is set to 5 seconds. The context.Canceled error is intentionally ignored and thus not returned by the method. Upon timeout, the method returns only context.DeadlineExceeded error.

type GracefulServerOption

type GracefulServerOption func(s *GracefulServer)

GracefulServerOption is an option used to configure a GracefulServer instance.

func WithCloudflareTLSConfig

func WithCloudflareTLSConfig() GracefulServerOption

WithCloudflareTLSConfig applies TLS configuration patches to a http.Server, implementing best practice configurations inspired by Cloudflare: https://blog.cloudflare.com/exposing-go-on-the-internet/

func WithCloudflareTimeouts

func WithCloudflareTimeouts() GracefulServerOption

WithCloudflareTimeouts applies timeout patches to a http.Server, implementing best practice configurations inspired by Cloudflare: https://blog.cloudflare.com/exposing-go-on-the-internet/

func WithShutdownTimeout

func WithShutdownTimeout(duration time.Duration) GracefulServerOption

WithShutdownTimeout sets the timeout for a graceful shutdown, after which all active connections will be forcibly closed.

func WithTLSConfig

func WithTLSConfig(config *tls.Config) GracefulServerOption

WithTLSConfig sets the provided TLS configuration for a http.Server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL