in

A resource termination library to smoothly clean up resources on term signals


graceful is a resource termination library to smoothly clean up resources on term signals.

package main

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

	"graceful"
)

func simpleHandler(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "hello worldn")

	// Long-running go routine
	graceful.Go(func() {
		deadline := time.NewTimer(time.Minute)
		for {
			select {
			case <-deadline.C:
				return
			default:
			}
		}
	})
}

func main() {
	http.HandleFunc("/hello", simpleHandler)
	svr := &http.Server{
		Addr:    ":8080",
		Handler: http.HandlerFunc(simpleHandler),
	}
	go func() {
		err := svr.ListenAndServe()
		if err != nil {
			panic(err)
		}
	}()

	// Example go routine tracking.
	graceful.Go(func() {
		deadline := time.NewTimer(time.Second * 5)
		for {
			select {
			case <-deadline.C:
				return
			default:
			}
		}
	})

	// Specify graceful order.
	graceful.In(httpShutdown(svr), graceful.GoRoutineTerminator())

	// Wait for signals from os.
	graceful.Wait()
}

func httpShutdown(svr *http.Server) graceful.TerminatorFunc {
	return func() {
		if err := svr.Shutdown(context.Background()); err != nil {
			fmt.Println(err)
		}
	}
}

GitHub

https://github.com/sharonjl/graceful




Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Like ThreeJS but for Python and based on wgpu

Disco Balma Lyrics — Asees Kaur | Mellow D