onecontext

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2021 License: Apache-2.0 Imports: 5 Imported by: 30

README

teivah/onecontext

Go Report Card

teivah/onecontext is a set of context utilities.

One Context

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 to query a database and 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

It's is precisely 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)

It returns a merged context that we can now propagate.

Detach

onecontext.Detach returns a context detached from the original cancellation signal. It can be helpful, for example, if we implement an HTTP handler and that we have to pass a context to another goroutine that may expire after we send back a response.

Reset Values

onecontext.ResetValues reset the values of an existing context.

Documentation

Overview

Package onecontext provides a mechanism to merge multiple existing contexts.

Index

Constants

This section is empty.

Variables

View Source
var ErrCanceled = errors.New("context canceled")

ErrCanceled is the returned when the CancelFunc returned by Merge is called.

Functions

func Merge

Merge allows to merge multiple contexts. It returns the merged context and a CancelFunc to cancel it.

Types

type DetachedContext

type DetachedContext struct {
	// contains filtered or unexported fields
}

DetachedContext holds the logic to detach a cancellation signal from a context.

func Detach

func Detach(ctx context.Context) (*DetachedContext, func())

Detach detaches the cancellation signal from a context.

func (*DetachedContext) Deadline

func (c *DetachedContext) Deadline() (time.Time, bool)

Deadline returns a nil deadline.

func (*DetachedContext) Done

func (c *DetachedContext) Done() <-chan struct{}

Done returns a cancellation signal that expires only when the context is canceled from the cancel function returned in Detach.

func (*DetachedContext) Err

func (c *DetachedContext) Err() error

Err returns an error if the context is canceled from the cancel function returned in Detach.

func (*DetachedContext) Value

func (c *DetachedContext) Value(key interface{}) interface{}

Value returns the value associated with the key from the original context.

type OneContext

type OneContext struct {
	// contains filtered or unexported fields
}

OneContext is the struct holding the context grouping logic.

func (*OneContext) Deadline

func (o *OneContext) Deadline() (time.Time, bool)

Deadline returns the minimum deadline among all the contexts.

func (*OneContext) Done

func (o *OneContext) Done() <-chan struct{}

Done returns a channel for cancellation.

func (*OneContext) Err

func (o *OneContext) Err() error

Err returns the first error raised by the contexts, otherwise a nil error.

func (*OneContext) Value

func (o *OneContext) Value(key interface{}) interface{}

Value returns the value associated with the key from one of the contexts.

type ResetValuesContext

type ResetValuesContext struct {
	// contains filtered or unexported fields
}

ResetValuesContext holds the logic reset the values of a context.

func ResetValues

func ResetValues(ctx context.Context) *ResetValuesContext

ResetValues reset the values of a context.

func (*ResetValuesContext) Deadline

func (c *ResetValuesContext) Deadline() (time.Time, bool)

Deadline returns the original context deadline.

func (*ResetValuesContext) Done

func (c *ResetValuesContext) Done() <-chan struct{}

Done returns the original done channel.

func (*ResetValuesContext) Err

func (c *ResetValuesContext) Err() error

Err returns the original context error.

func (*ResetValuesContext) Value

func (c *ResetValuesContext) Value(_ interface{}) interface{}

Value returns nil regardless of the key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL