Skip to content
A library to merge existing Go contexts
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Project init Aug 2, 2019
LICENSE Create LICENSE Aug 2, 2019 Update Aug 2, 2019
go.mod Project init Aug 2, 2019
go.sum Project init Aug 2, 2019
onecontext.go Fixing deadline max time Aug 5, 2019
onecontext_test.go Doc Aug 2, 2019


Go Report Card


Have you ever faced the situation where you have to merge multiple existing contexts? If not, then you might, eventually.

For example, we can face the situation where we are building an application using a library that gives us a global context (for example urfave/cli). This context expires once the application is stopped.

Meanwhile, we are exposing a gRPC service like this:

func (f *Foo) Get(ctx context.Context, bar *Bar) (*Baz, error) {

Here, we receive another context provided by gRPC.

Then, in the Get implementation, we want for example to query a database and we must provide a context for that.

Ideally, we would like to provide a merged context that would expire either:

  • When the application is stopped
  • Or when the received gRPC context expires

This is exactly the purpose of this library.

In our case, we can now merge the two contexts in a single one like this:

ctx, cancel := onecontext.Merge(ctx1, ctx2)

This returns a merged context that we can now propagate.


go get

You can’t perform that action at this time.