signalcontext

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 4 Imported by: 31

README

Signal Context

⚠️ 🛑 Do not use this library on Go 1.16+! Go 1.16 introduced a native standard library for signal.NotifyContext, which you should use instead.


Signal Context (signalcontext) is a Go library for creating Go context's that cancel on signals. This is very useful for client-side applications that want to cancel operations on user interrupts (e.g. CTRL+C).

Features

  • Small - A very tiny API and less than 50 lines of code.

  • Independent - No external dependencies besides the Go standard library, meaning it won't bloat your project.

  • Flexible - Use native Go contexts and extend/wrap as needed.

Usage

Here is an example for gracefully stopping an HTTP server when the user presses CTRL+C in their terminal:

package main

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

	"github.com/sethvargo/go-signalcontext"
)

func main() {
	ctx, cancel := signalcontext.OnInterrupt()
	defer cancel()

	s := &http.Server{
		Addr: ":8080",
	}
	go func() {
		if err := s.ListenAndServe(); err != nil {
			log.Fatal(err)
		}
	}()

	// Wait for CTRL+C
	<-ctx.Done()

	// Stop the server
	shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := s.Shutdown(shutdownCtx); err != nil {
		log.Fatal(err)
	}
}

You can also use custom signals:

ctx, cancel := signalcontext.On(syscall.SIGUSR1)
defer cancel()

And also wrap an existing context:

ctx1, cancel1 := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel1()

ctx2, cancel2 := signalcontext.Wrap(ctx1, syscall.SIGUSR1)
defer cancel2()

Documentation

Overview

Package signalcontext creates context.Contexts that cancel on os.Signals.

ctx, cancel := signalcontext.OnInterrupt()
defer cancel()

Deprecated: Use signal.NotifyContext instead.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func On deprecated

func On(signals ...os.Signal) (context.Context, context.CancelFunc)

On creates a new context that cancels on the given signals.

Deprecated: Use signal.NotifyContext instead.

func OnInterrupt deprecated

func OnInterrupt() (context.Context, context.CancelFunc)

OnInterrupt creates a new context that cancels on SIGINT or SIGTERM.

Deprecated: Use signal.NotifyContext instead.

Example
ctx, cancel := signalcontext.OnInterrupt()
defer cancel()

s := &http.Server{
	Addr: ":8080",
}
go func() {
	if err := s.ListenAndServe(); err != nil {
		log.Fatal(err)
	}
}()

// Wait for CTRL+C
<-ctx.Done()

// Stop the server
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(shutdownCtx); err != nil {
	log.Fatal(err)
}
Output:

func Wrap deprecated

func Wrap(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc)

Wrap creates a new context that cancels on the given signals. It wraps the provided context.

Deprecated: Use signal.NotifyContext instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL