All Projects → posener → ctxutil

posener / ctxutil

Licence: Apache-2.0 License
utils for Go context

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to ctxutil

Vuex Rest Api
A utility to simplify the use of REST APIs with Vuex
Stars: ✭ 365 (+1927.78%)
Mutual labels:  utility, helper
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (+1505.56%)
Mutual labels:  utility, helper
sigctx
context with signal in golang
Stars: ✭ 19 (+5.56%)
Mutual labels:  context, signal
RaycastVisualization
This asset allows users to view raycasts as the user fires them.
Stars: ✭ 61 (+238.89%)
Mutual labels:  utility, helper
Ngx Context
Angular Context: Easy property binding for router outlet and nested component trees.
Stars: ✭ 118 (+555.56%)
Mutual labels:  utility, context
Common.utility
Various helper class
Stars: ✭ 4,101 (+22683.33%)
Mutual labels:  utility, helper
Tooltip Sequence
A simple step by step tooltip helper for any site
Stars: ✭ 287 (+1494.44%)
Mutual labels:  utility, helper
Cs.2click
🔊 A Better Audio Router for a Modular System.
Stars: ✭ 7 (-61.11%)
Mutual labels:  utility, signal
xd-storage-helper
A little helper to make storing key-value-pairs (e.g. settings) for Adobe XD plugins easier.
Stars: ✭ 22 (+22.22%)
Mutual labels:  utility, helper
rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (+50%)
Mutual labels:  utility, functions
bash
A collection of small bash utils.
Stars: ✭ 15 (-16.67%)
Mutual labels:  utility
pscale-workflow-helper-scripts
Workflows and helper scripts around the PlanetScale DB workflow to automate database branch creation, association, update and merge directly out of your pull/merge request or favourite CI/CD.
Stars: ✭ 42 (+133.33%)
Mutual labels:  helper
mini-create-react-context
(A smaller) polyfill for the react context API
Stars: ✭ 34 (+88.89%)
Mutual labels:  context
Data-Science-Tutorials
Python Tutorials for Data Science
Stars: ✭ 104 (+477.78%)
Mutual labels:  functions
gits
A Fast CLI Git manager for multiple repositories
Stars: ✭ 39 (+116.67%)
Mutual labels:  helper
json2xml
json to xml converter in python3
Stars: ✭ 76 (+322.22%)
Mutual labels:  utility
diskusage
FANTASTIC SPEED utility to find out top largest folders/files on the disk.
Stars: ✭ 64 (+255.56%)
Mutual labels:  utility
pynotify
A Python package to send emails like humans.
Stars: ✭ 21 (+16.67%)
Mutual labels:  utility
pyGroff
laTEX is awesome but we are lazy -> groff with markdown syntax and inline code execution
Stars: ✭ 25 (+38.89%)
Mutual labels:  utility
Vutils
Vutils or Vic Utilities is an utility library written in Modern C++ and for Modern C++. It helps your programming go easier, faster, and simpler.
Stars: ✭ 16 (-11.11%)
Mutual labels:  utility

ctxutil

Build Status codecov golangci GoDoc goreadme

Package ctxutil is a collection of functions for contexts.

Functions

func Interrupt

func Interrupt() context.Context

Interrupt is a convenience function for catching SIGINT on a background context.

Example:

func main() {
	ctx := ctxutil.Interrupt()
	// use ctx...
}

func WithSignal

func WithSignal(parent context.Context, sigWhiteList ...os.Signal) context.Context

WithSignal returns a context which is done when an OS signal is sent. parent is a parent context to wrap. sigWhiteList is a list of signals to listen on. According to the signal.Notify behavior, an empty list will listen to any OS signal. If an OS signal closed this context, ErrSignal will be returned in the Err() method of the returned context.

This method creates the signal channel and invokes a goroutine.

Example

func main() {
	// Create a context which will be cancelled on SIGINT.
	ctx := ctxutil.WithSignal(context.Background(), os.Interrupt)
	// use ctx...
}

func WithValues

func WithValues(ctx, values context.Context) context.Context

WithValues composes values from multiple contexts. It returns a context that exposes the deadline and cancel of ctx, and combined values from ctx and values. A value in ctx context overrides a value with the same key in values context.

Consider the following standard HTTP Go server stack:

  1. Middlewares that extract user credentials and request id from the
headers and inject them to the `http.Request` context as values.
  1. The http.Handler launches an asynchronous goroutine task which
needs those values from the context.
  1. After launching the asynchronous task the handler returns 202 to
the client, the goroutine continues to run in background.

Problem Statement:

  • The async task can't use the request context - it is cancelled
as the `http.Handler` returns.
  • There is no way to use the context values in the async task.
  • Specially if those values are used automatically with client
`http.Roundtripper` (extract the request id from the context
and inject it to http headers in a following request.)

The suggested function ctx := ctxutil.WithValues(ctx, values) does the following:

  1. When ctx.Value() is called, the key is searched in the
original `ctx` and if not found it searches in `values`.
  1. When Done()/Deadline()/Err() are called, it is uses
original `ctx`'s state.

Example

This is how an http.Handler should run a goroutine that need values from the context.

func handle(w http.ResponseWriter, r *http.Request) {
	// [ do something ... ]

	// Create async task context that enables it run for 1 minute, for example
	asyncCtx, asyncCancel = ctxutil.WithTimeout(context.Background(), time.Minute)
	// Use values from the request context
	asyncCtx = ctxutil.WithValues(asyncCtx, r.Context())
	// Run the async task with it's context
	go func() {
		defer asyncCancel()
		asyncTask(asyncCtx)
	}()
}

Created by goreadme

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].