ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - posener/ctxutil: utils for Go context
utils for Go context. Contribute to posener/ctxutil development by creating an account on GitHub.
Visit Site

GitHub - posener/ctxutil: utils for Go context

GitHub - posener/ctxutil: utils for Go context

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

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory