context

package module
v0.0.0-...-c90fd68 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: MIT Imports: 5 Imported by: 3

README

A drop-in replacement for the "context" and "golang.org/x/net/context" packages which works seamlessly across all Go 1 versions in standard Go environments, and also Google App Engine Go environments

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Namespace = func(ctx Context) (string, error) {
	return "", nil
}

Namespace is a overrideable function which returns the namespace associated with the given context.

Functions

func Hostname

func Hostname(ctx Context, r *http.Request) (string, error)

Hostname returns the hostname of the current instance

func WithDeadline

func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)

WithDeadline returns a copy of the parent context with the deadline adjusted to be no later than d. If the parent's deadline is already earlier than d, WithDeadline(parent, d) is semantically equivalent to parent. The returned context's Done channel is closed when the deadline expires, when the returned cancel function is called, or when the parent context's Done channel is closed, whichever happens first.

func WithTimeout

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).

Types

type CancelFunc

type CancelFunc func()

CancelFunc is a context cancel func, matching the "context".CancelFunc interface

type Context

type Context interface {
	Deadline() (deadline time.Time, ok bool)
	Done() <-chan struct{}
	Err() error
	Value(key interface{}) interface{}
}

Context has the same interface as "context" and "golang.org/x/net/context" to handle imports in all go version

func Background

func Background() Context

Background returns a new background context

func NewContext

func NewContext(r *http.Request) Context

NewContext creates a new context associated with the request.

func SetCode

func SetCode(ctx Context, code int) Context

SetCode sets the HTTP response code associated with the context

func WithValue

func WithValue(ctx Context, key, val interface{}) Context

WithValue returns a copy of parent in which the value associated with key is val.

type Key

type Key string

Key is a string type for context value key names

var (
	ContextKeyNamepace       Key = "namespace"
	ContextKeyRequestBody    Key = "request.body"
	ContextKeyRequestVars    Key = "request.vars"
	ContextKeyResponseWriter Key = "http.responsewriter"
	ContextKeyRequest        Key = "request"
	ContextKeyResponseCode   Key = "response.code"
	ContextKeyInitialized    Key = "initialized"
	ContextKeyResponseBody   Key = "response.body"
	ContextKeyEnvironment    Key = "environment"
)

Context key location definitions

Jump to

Keyboard shortcuts

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