• This repository has been archived on 06/Mar/2023
  • Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Graceful shutdown of Go 1.8+ servers using Server.Shutdown

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.

More Repositories

1

chronometro

Annotation triggered library that helps tracking loading times of your app
Java
45
star
2

logrus-stackdriver-formatter

Stackdriver formatter for logrus
Go
43
star
3

Embed4Play

Publish video clips from Swedish TV channels on a Wordpress blog
PHP
11
star
4

Urushiol

Test framework written in Ruby to test varnish-cache routing and caching logic
Ruby
7
star
5

gate

Limit the number of busy handlers (at the gate)
Go
6
star
6

env

Load environment variables into Go types, with fallback values.
Go
5
star
7

epg

Go client for the C More EPG Web API
Go
4
star
8

Crete-Round-Darker

Crete Round Darker is a bolder variant of Crete Round a warm slab serif providing a hint of softness to texts. Meant to be used for captioning/sub titling use. The font is licensed under the SIL Open Font License (OFL) http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL
4
star
9

go-binary-buildpack

A Heroku buildpack that runs Go binaries, based on the Ø buildpack
Shell
3
star
10

tf-modules

Terraform modules
HCL
3
star
11

polopoly-exporter

script to export content from a polopoly 9.0 system
Ruby
2
star
12

nrcontext

Using the New Relic go-agent with context.Context
Go
2
star
13

External-markup

Wordpress plugin for retriving external markup
PHP
2
star
14

graphql-advanced-query-complexity

TypeScript
2
star
15

nokogiri-xpath-issue-741

nokogiri-xpath-issue-741
Ruby
1
star
16

mms

Go clients for MMS services
Go
1
star
17

gcp-tools

A small set of command-line tools for interacting with GCP
Go
1
star
18

nids

Go version of the nid_utils gem
Go
1
star
19

features

The features package provides a simple feature flagging interface/implementation for Go applications
Go
1
star
20

vihsjltm

Ruby
1
star
21

stroombreker

Ruby
1
star