<commit_before>package signals

import (
	"os"
	"os/signal"
	"syscall"
)

var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
var onlyOneSignalHandler = make(chan struct{})

// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
// which is closed on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() (stopCh <-chan struct{}) {
	close(onlyOneSignalHandler) // panics when called twice

	stop := make(chan struct{})
	c := make(chan os.Signal, 2)
	signal.Notify(c, shutdownSignals...)
	go func() {
		<-c
		close(stop)
		<-c
		os.Exit(1) // second signal. Exit directly.
	}()

	return stop<commit_msg>refactor(signals): replace stop channel with signal context<commit_after>package signals

import (
	"context"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

var (
	shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
	signalCtx       context.Context
	cancel          context.CancelFunc
	once            sync.Once
)

// Context returns a Context registered to close on SIGTERM and SIGINT.
// If a second signal is caught, the program is terminated with exit code 1.
func Context() context.Context {
	once.Do(func() {
		c := make(chan os.Signal, 2)
		signal.Notify(c, shutdownSignals...)
		signalCtx, cancel = context.WithCancel(context.Background())
		go func() {
			<-c
			cancel()

			select {
			case <-signalCtx.Done():
			case <-c:
				os.Exit(1) // second signal. Exit directly.
			}
		}()
	})

	return signalCtx
{'fraction_non_alphanumeric': 0.12190352020860495, 'fraction_numerical': 0.003911342894393742, 'mean_word_length': 3.4752186588921283, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5907780', 'n_tokens_mistral': 540, 'n_tokens_neox': 490, 'n_words': 155}