reqctx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

reqctx

Package reqctx provides a way to set http request context without creating a shallow copy.

Please don't use in production!

Current implementation of WithContext is following:

func (r *Request) WithContext(ctx context.Context) *Request {
	if ctx == nil {
		panic("nil context")
	}
	r2 := new(Request)
	*r2 = *r
	r2.ctx = ctx
	r2.URL = cloneURL(r.URL) // legacy behavior; TODO: try to remove. Issue 23544
	return r2
}

The Set function is effectively doing the following:

func Set(r *Request, ctx context.Context) {
	r.ctx = ctx
}

So, instead of doing that:

func Middleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), "foo", "bar")
		next.ServeHTTP(w, r.WithContext(ctx))
	})
}

You can now do this:

package reqctx_test

import (
	"context"
	"net/http"
	
	"github.com/ernado/reqctx"
)

func MiddlewareFast(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		reqctx.SetValue(r, "foo", "bar")
		next.ServeHTTP(w, r)
	})
}

Benchmarks

goos: linux
goarch: amd64
BenchmarkSet-12          1000000000  0.532 ns/op  0 B/op    0 allocs/op
BenchmarkWithContext-12  26447012    45.8 ns/op   128 B/op  1 allocs/op
PASS

Documentation

Overview

Package reqctx provides a totally unsafe, but fast way to set request context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

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

Set sets request context without shallow copy of request.

func SetValue

func SetValue(req *http.Request, k, v interface{})

SetValue wraps context.WithValue call on request context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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