Giter Club home page Giter Club logo

go-signalcontext's Introduction

Signal Context

GoDoc GitHub Actions

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()

go-signalcontext's People

Contributors

sethvargo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.