Skip to content

penkovski/graceful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Go Report Card Test

graceful

Graceful shutdown for HTTP servers with Go 1.8+

What it is

A simple implementation wrapped in ~20 lines of code. It doesn't support TLS listeners as I don't need it currently.

Installation

go get -u github.com/penkovski/graceful

Note that you should use Go 1.8+

You can also just copy the graceful.Start or graceful.StartCtx functions in your project and not depend on this repo.

Usage

package main

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

	"github.com/penkovski/graceful"
)

func main() {
	srv := &http.Server{
		Addr:    ":8080",
		Handler: http.HandlerFunc(hello),
	}

	if err := graceful.Start(srv, 10*time.Second); err != nil {
		log.Println(err)
	}
}

func hello(w http.ResponseWriter, r *http.Request) {
	time.Sleep(5 * time.Second)
	_, _ = w.Write([]byte("hello"))
}

Usage with errgroup and Context

package main

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

	"github.com/penkovski/graceful"
	"golang.org/x/sync/errgroup"
)

func main() {
	srv := &http.Server{
		Addr:    ":8080",
		Handler: http.HandlerFunc(hello),
	}

	g, ctx := errgroup.WithContext(context.Background())
	g.Go(func() error {
		if err := graceful.StartCtx(ctx, srv, 10*time.Second); err != nil {
			log.Println("server shutdown error:", err)
			return err
		}
		return errors.New("server stopped successfully")
	})
	if err := g.Wait(); err != nil {
		log.Println("errgroup stopped:", err)
	}
}

func hello(w http.ResponseWriter, r *http.Request) {
	time.Sleep(5 * time.Second)
	_, _ = w.Write([]byte("hello"))
}

About

Graceful shutdown for HTTP servers with Go 1.8+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages