GithubHelp home page GithubHelp logo

flga / httpctx Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 7 KB

httpctx provides a context aware implementation of ListenAndServe, ListenAndServeTLS, Serve and ServeTLS, useful for integration with golang.org/x/sync/errgroup.

License: The Unlicense

Go 100.00%

httpctx's Introduction

httpctx

Go Reference Go Report Card

A context aware implementation of ListenAndServe, ListenAndServeTLS, Serve and ServeTLS, useful for integration with golang.org/x/sync/errgroup.

Usage:

package main

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

	"github.com/flga/httpctx"
	"golang.org/x/sync/errgroup"
)

func main() {
	g, ctx := errgroup.WithContext(context.Background())

	// setup a failure in ~5s
	g.Go(func() error {
		time.Sleep(5 * time.Second)
		log.Println("something went wrong!")
		return errors.New("boom")
	})

	// the actual server
	g.Go(func() error {
		srv := http.Server{
			Addr: ":0",
		}

		log.Println("listening")
		return httpctx.ListenAndServe(
			ctx, // when this ctx expires, the server starts the shutdown process
			&srv,
			// wait for at most 30s when shutting down
			httpctx.WithShutdownTimeout(30*time.Second),
			// called just before the shutdown process starts, with the configured timeout
			httpctx.BeforeShutdown(func(timeout time.Duration) {
				log.Printf("shutting down, waiting for at most %.0fs", timeout.Seconds())
			}),
			// called after the shutdown process ends, with the error it returned (if any)
			httpctx.AfterShutdown(func(err error) {
				if err != nil {
					// probably a context.DeadlineExceeded
					log.Printf("unclean shutdown: %s", err)
					return
				}
				log.Println("shutdown complete")
			}),
		)
	})

	if err := g.Wait(); err != nil {
		panic(err)
	}
}

httpctx's People

Contributors

flga avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.