Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Latest commit

 

History

History
40 lines (32 loc) · 902 Bytes

README.md

File metadata and controls

40 lines (32 loc) · 902 Bytes

detach Go Reference

package main

import (
	"context"
	"net/http"
	"os"
	"os/signal"

	"github.com/fxrlv/detach"
)

func main() {
	main, cancel := signal.NotifyContext(
		context.Background(), os.Interrupt,
	)
	defer cancel()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// For incoming server requests, the context is canceled when the
		// client's connection closes, the request is canceled (with HTTP/2),
		// or when the ServeHTTP method returns.
		ctx := r.Context()

		ctx = detach.WithCancel(ctx, main)
		go func() {
			// Will be cancelled by signal.
			<-ctx.Done()

			// Values are associated with http.Request context.
			server := ctx.Value(http.ServerContextKey).(*http.Server)
			server.Close()
		}()
	})

	http.ListenAndServe(":8080", nil)
}