graceful

package module
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 9 Imported by: 19

README

Deprecated; this package is no longer maintained.

graceful

Build Status Go Report Card GoDoc License MIT

Installation

go get -u github.com/TV4/graceful

Usage

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/TV4/graceful"
)

type server struct{}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	time.Sleep(2 * time.Second)
	w.Write([]byte("Hello!"))
}

func main() {
	addr := ":2017"

	log.Printf("Listening on http://0.0.0.0%s\n", addr)

	graceful.ListenAndServe(&http.Server{
		Addr:    addr,
		Handler: &server{},
	})
}
$ go run main.go
2017/06/19 16:35:28 Listening on http://0.0.0.0:2017
^C
You can also use graceful.LogListenAndServe
package main

import (
	"net/http"
	"time"

	"github.com/TV4/graceful"
)

type server struct{}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	time.Sleep(2 * time.Second)
	w.Write([]byte("Hello!"))
}

func main() {
	graceful.LogListenAndServe(&http.Server{
		Addr:    ":2017",
		Handler: &server{},
	})
}
$ go run main.go
Listening on http://0.0.0.0:2017
^C
Server shutdown with timeout: 15s
Finished all in-flight HTTP requests
Shutdown finished 14s before deadline
And optionally your handler can implement the Shutdowner interface
package main

import (
	"context"
	"fmt"
	"net/http"

	graceful "github.com/TV4/graceful"
)

func main() {
	graceful.LogListenAndServe(&http.Server{
		Addr:    ":8080",
		Handler: &server{},
	})
}

type server struct{}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello world!")
}

func (s *server) Shutdown(ctx context.Context) error {
	// Here you can do anything that you need to do
	// after the *http.Server has stopped accepting
	// new connections and finished its Shutdown

	// The ctx is the same as the context used to
	// perform *https.Server.Shutdown and thus
	// shares the timeout (15 seconds by default)

	fmt.Println("Finished *server.Shutdown")

	return nil
}
$ go run main.go
Listening on http://0.0.0.0:8080
^C
Server shutdown with timeout: 15s
Finished all in-flight HTTP requests
Shutting down handler with timeout: 15s
Finished *server.Shutdown
Shutdown finished 15s before deadline

License (MIT)

Copyright (c) 2017-2018 TV4

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Deprecated: This package is no longer maintained.

Package graceful simplifies graceful shutdown of HTTP servers (Go 1.8+).

Installation

Just go get the package:

go get -u github.com/TV4/graceful

Usage

A small usage example

package main

import (
    "context"
    "log"
    "net/http"
    "os"
    "time"

    "github.com/TV4/graceful"
)

type server struct {
    logger *log.Logger
}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    time.Sleep(5 * time.Second)
    w.Write([]byte("Hello!"))
}

func (s *server) Shutdown(ctx context.Context) error {
    time.Sleep(2 * time.Second)
    s.logger.Println("Shutdown finished")
    return nil
}

func main() {
    graceful.LogListenAndServe(setup(":2017"))
}

func setup(addr string) (*http.Server, *log.Logger) {
    s := &server{logger: log.New(os.Stdout, "", 0)}
    return &http.Server{Addr: addr, Handler: s}, s.logger
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ListeningFormat       = "Listening on http://%s\n"
	ShutdownFormat        = "\nServer shutdown with timeout: %s\n"
	ErrorFormat           = "Error: %v\n"
	FinishedFormat        = "Shutdown finished %ds before deadline\n"
	FinishedHTTP          = "Finished all in-flight HTTP requests\n"
	HandlerShutdownFormat = "Shutting down handler with timeout: %ds\n"
)

Format strings used by the logger

View Source
var Timeout = 15 * time.Second

Timeout for context used in call to *http.Server.Shutdown

Functions

func ListenAndServe

func ListenAndServe(s Server)

ListenAndServe starts the server in a goroutine and then calls Shutdown

func ListenAndServeTLS added in v0.3.0

func ListenAndServeTLS(s TLSServer, certFile, keyFile string)

ListenAndServeTLS starts the server in a goroutine and then calls Shutdown

func LogListenAndServe

func LogListenAndServe(s Server, loggers ...Logger)

LogListenAndServe logs using the logger and then calls ListenAndServe

func Shutdown

func Shutdown(s Shutdowner)

Shutdown blocks until os.Interrupt or syscall.SIGTERM received, then running *http.Server.Shutdown with a context having a timeout

Types

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Fatal(...interface{})
}

Logger is implemented by *log.Logger

type Server

type Server interface {
	ListenAndServe() error
	Shutdowner
}

Server is implemented by *http.Server

type Shutdowner

type Shutdowner interface {
	Shutdown(ctx context.Context) error
}

Shutdowner is implemented by *http.Server, and optionally by *http.Server.Handler

type TLSServer added in v0.3.0

type TLSServer interface {
	ListenAndServeTLS(string, string) error
	Shutdowner
}

TLSServer is implemented by *http.Server

Jump to

Keyboard shortcuts

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