<commit_before><commit_msg>golang graceful shutdown of web server<commit_after>// https://www.rudderstack.com/blog/implementing-graceful-shutdown-in-go/

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"golang.org/x/sync/errgroup"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("started handler")
	time.Sleep(10*time.Second)
	fmt.Println("completed handler")
	w.Write([]byte("hello world"))
}

func main() {

	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		c := make(chan os.Signal, 1) // we need to reserve to buffer size 1, so the notifier are not blocked
		signal.Notify(c, os.Interrupt, syscall.SIGTERM)

		<-c
		cancel()
	}()

	// db, err := repo.SetupPostgresDB(ctx, getConfig("DB_DSN", "root@tcp(127.0.0.1:3306)/service"))
	// if err != nil {
	// 	panic(err)
	// }

	mux := http.NewServeMux()
	// assign a route/todo to a handler myHandler
	mux.HandleFunc("/hello", myHandler)

	httpServer := &http.Server{
		Addr:    ":8000",
		Handler: mux,
	}

	g, gCtx := errgroup.WithContext(ctx)
	g.Go(func() error {
		return httpServer.ListenAndServe()
	})
	g.Go(func() error {
		<-gCtx.Done()
		fmt.Println("gracefully shutdown........")
		time.Sleep(time.Second)
		return httpServer.Shutdown(context.Background())
	})

	if err := g.Wait(); err != nil {
		fmt.Printf("exit reason: %s \n", err)
	}
}


{'fraction_non_alphanumeric': 0.17862266857962697, 'fraction_numerical': 0.01291248206599713, 'mean_word_length': 3.7448979591836733, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081286', 'n_tokens_mistral': 572, 'n_tokens_neox': 527, 'n_words': 111}