ctxmap

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

ctxmap

Go Reference

Package ctxmap implements a registry for global context.Context for use in web applications.

Based on work from github.com/gorilla/context, this package simplifies the storage by mapping a pointer to an http.Request to a context.Context. This allows applications to use Google's standard context mechanism to pass state around their web applications, while sticking to the standard http.HandlerFunc implementation for their middleware implementations.

As a result of the simplification, the runtime overhead of the package is reduced by 35 to 50 percent in my tests. However, it would be common to store a map of values or a pointer to a structure in the Context object, and my testing does not account for time taken beyond calling Context.Value().

Benchmark Readers Writers Iterations Map Ops context ctxmap
BenchmarkMutexSameReadWrite1 1 1 32 64 208.91 ns 102.63 ns
BenchmarkMutexSameReadWrite2 2 2 32 128 211.27 ns 103.97 ns
BenchmarkMutexSameReadWrite4 4 4 32 256 216.21 ns 101.54 ns
BenchmarkMutex1 2 8 32 320 252.26 ns 92.62 ns
BenchmarkMutex2 16 4 64 1280 166.85 ns 108.31 ns
BenchmarkMutex3 1 2 128 384 221.67 ns 78.47 ns
BenchmarkMutex4 128 32 256 40960 179.70 ns 107.31 ns
BenchmarkMutex5 1024 2048 64 196608 233.41 ns 90.10 ns
BenchmarkMutex6 2048 1024 512 1572864 183.25 ns 92.33 ns

Documentation

Overview

Package ctxmap implements a registry for global context.Context for use in web applications.

Based on work from github.com/gorilla/context, this package simplifies the storage by mapping a pointer to an http.Request to a context.Context. This allows applications to use Google's standard context mechanism to pass state around their web applications, while sticking to the standard http.HandlerFunc implementation for their middleware implementations.

As a result of the simplification, the runtime overhead of the package is reduced by 30 to 40 percent in my tests. However, it would be common to store a map of values or a pointer to a structure in the Context object, and my testing does not account for time taken beyond calling Context.Value().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearHandler

func ClearHandler(h http.Handler) http.Handler

ClearHandler wraps an http.Handler and clears request values at the end of a request lifetime.

func Delete

func Delete(r *http.Request)

Delete removes the context for a given request.

This is usually called by a handler wrapper to clean up request variables at the end of a request lifetime. See ClearHandler().

func Get

func Get(r *http.Request) context.Context

Get returns a context for given request.

func GetOk

func GetOk(r *http.Request) (context.Context, bool)

GetOk returns stored context and presence state like multi-value return of map access.

func Set

func Set(r *http.Request, ctx context.Context)

Set stores a context for a given request.

Types

This section is empty.

Jump to

Keyboard shortcuts

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