{"id":13414120,"url":"https://github.com/posener/ctxutil","last_synced_at":"2025-05-04T08:02:21.746Z","repository":{"id":57487982,"uuid":"142866506","full_name":"posener/ctxutil","owner":"posener","description":"utils for Go context","archived":false,"fork":false,"pushed_at":"2020-03-01T00:49:08.000Z","size":19,"stargazers_count":25,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-04T08:01:46.125Z","etag":null,"topics":["async","context","functions","go","golang","helper","signal","utility"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/posener.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-30T11:28:57.000Z","updated_at":"2023-12-29T07:49:07.000Z","dependencies_parsed_at":"2022-08-29T11:22:23.650Z","dependency_job_id":null,"html_url":"https://github.com/posener/ctxutil","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fctxutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fctxutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fctxutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Fctxutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posener","download_url":"https://codeload.github.com/posener/ctxutil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252305223,"owners_count":21726621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","context","functions","go","golang","helper","signal","utility"],"created_at":"2024-07-30T20:01:58.211Z","updated_at":"2025-05-04T08:02:21.688Z","avatar_url":"https://github.com/posener.png","language":"Go","readme":"# ctxutil\n\n[![Build Status](https://travis-ci.org/posener/ctxutil.svg?branch=master)](https://travis-ci.org/posener/ctxutil)\n[![codecov](https://codecov.io/gh/posener/ctxutil/branch/master/graph/badge.svg)](https://codecov.io/gh/posener/ctxutil)\n[![golangci](https://golangci.com/badges/github.com/posener/ctxutil.svg)](https://golangci.com/r/github.com/posener/ctxutil)\n[![GoDoc](https://godoc.org/github.com/posener/ctxutil?status.svg)](http://godoc.org/github.com/posener/ctxutil)\n[![goreadme](https://goreadme.herokuapp.com/badge/posener/ctxutil.svg)](https://goreadme.herokuapp.com)\n\nPackage ctxutil is a collection of functions for contexts.\n\n## Functions\n\n### func [Interrupt](https://github.com/posener/ctxutil/blob/master/signal.go#L60)\n\n`func Interrupt() context.Context`\n\nInterrupt is a convenience function for catching SIGINT on a\nbackground context.\n\n### Example:\n\n```go\nfunc main() {\n\tctx := ctxutil.Interrupt()\n\t// use ctx...\n}\n```\n\n### func [WithSignal](https://github.com/posener/ctxutil/blob/master/signal.go#L37)\n\n`func WithSignal(parent context.Context, sigWhiteList ...os.Signal) context.Context`\n\nWithSignal returns a context which is done when an OS signal is sent.\nparent is a parent context to wrap.\nsigWhiteList is a list of signals to listen on.\nAccording to the signal.Notify behavior, an empty list will listen\nto any OS signal.\nIf an OS signal closed this context, ErrSignal will be returned in\nthe Err() method of the returned context.\n\nThis method creates the signal channel and invokes a goroutine.\n\n### Example\n\n```go\nfunc main() {\n\t// Create a context which will be cancelled on SIGINT.\n\tctx := ctxutil.WithSignal(context.Background(), os.Interrupt)\n\t// use ctx...\n}\n```\n\n### func [WithValues](https://github.com/posener/ctxutil/blob/master/values.go#L53)\n\n`func WithValues(ctx, values context.Context) context.Context`\n\nWithValues composes values from multiple contexts.\nIt returns a context that exposes the deadline and cancel of `ctx`,\nand combined values from `ctx` and `values`.\nA value in `ctx` context overrides a value with the same key in `values` context.\n\nConsider the following standard HTTP Go server stack:\n\n1. Middlewares that extract user credentials and request id from the\n\n```go\nheaders and inject them to the `http.Request` context as values.\n```\n\n2. The `http.Handler` launches an asynchronous goroutine task which\n\n```go\nneeds those values from the context.\n```\n\n3. After launching the asynchronous task the handler returns 202 to\n\n```go\nthe client, the goroutine continues to run in background.\n```\n\nProblem Statement:\n* The async task can't use the request context - it is cancelled\n\n```go\nas the `http.Handler` returns.\n```\n\n* There is no way to use the context values in the async task.\n* Specially if those values are used automatically with client\n\n```go\n`http.Roundtripper` (extract the request id from the context\nand inject it to http headers in a following request.)\n```\n\nThe suggested function `ctx := ctxutil.WithValues(ctx, values)`\ndoes the following:\n\n1. When `ctx.Value()` is called, the key is searched in the\n\n```go\noriginal `ctx` and if not found it searches in `values`.\n```\n\n2. When `Done()`/`Deadline()`/`Err()` are called, it is uses\n\n```go\noriginal `ctx`'s state.\n```\n\n### Example\n\nThis is how an `http.Handler` should run a goroutine that need\nvalues from the context.\n\n```go\nfunc handle(w http.ResponseWriter, r *http.Request) {\n\t// [ do something ... ]\n\n\t// Create async task context that enables it run for 1 minute, for example\n\tasyncCtx, asyncCancel = ctxutil.WithTimeout(context.Background(), time.Minute)\n\t// Use values from the request context\n\tasyncCtx = ctxutil.WithValues(asyncCtx, r.Context())\n\t// Run the async task with it's context\n\tgo func() {\n\t\tdefer asyncCancel()\n\t\tasyncTask(asyncCtx)\n\t}()\n}\n```\n\n\n---\n\nCreated by [goreadme](https://github.com/apps/goreadme)\n","funding_links":[],"categories":["Utilities","工具库`可以提升效率的通用代码库和工具`","公用事业公司","Utility","工具库"],"sub_categories":["Utility/Miscellaneous","查询语","实用程序/Miscellaneous","HTTP Clients","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Fctxutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposener%2Fctxutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Fctxutil/lists"}