Add Clone method to Context (#1157)
* Add Clone method to Context This adds the ability to clone Chi contexts so that they may be used outside of the request/response lifecycle. This is useful in scenarios where the context may be held _after_ a request has been served to a user, like asynchronous background tasks, mirror testing, etc. * Migrate to use slices.Clone * Detach Clone from request lifecycle and document its constraints - Nil out parentCtx in the clone: nothing reads it after routing, and keeping it pins the finished request's context in memory. - Document that Clone must be called before the handler returns. - Note on the Context struct that new reference fields must be deep-copied in Clone. - Rename Clone receiver to x for consistency with the rest of the file. --------- Co-authored-by: Blake Williams <blake@blakewilliams.me>
This commit is contained in:
parent
6adf733e0e
commit
1576723359
2 changed files with 79 additions and 1 deletions
27
context.go
27
context.go
|
|
@ -3,6 +3,7 @@ package chi
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -42,6 +43,8 @@ var (
|
|||
// Context is the default routing context set on the root node of a
|
||||
// request context to track route patterns, URL parameters and
|
||||
// an optional routing path.
|
||||
//
|
||||
// NOTE: New reference fields (slices, maps, pointers) must be deep-copied in Clone.
|
||||
type Context struct {
|
||||
Routes Routes
|
||||
|
||||
|
|
@ -95,6 +98,30 @@ func (x *Context) Reset() {
|
|||
x.parentCtx = nil
|
||||
}
|
||||
|
||||
// Clone a routing context so that it may be used outside of the request/response
|
||||
// lifecycle, e.g. in a goroutine that outlives the request handler.
|
||||
//
|
||||
// Clone must be called before the request handler returns; after that, the
|
||||
// original context is reset and put back into the pool for reuse by another
|
||||
// request, racing with the copy.
|
||||
func (x *Context) Clone() *Context {
|
||||
clone := *x
|
||||
|
||||
// Detach from the request's context, which is canceled once the request finishes.
|
||||
clone.parentCtx = nil
|
||||
|
||||
clone.URLParams.Keys = slices.Clone(x.URLParams.Keys)
|
||||
clone.URLParams.Values = slices.Clone(x.URLParams.Values)
|
||||
|
||||
clone.routeParams.Keys = slices.Clone(x.routeParams.Keys)
|
||||
clone.routeParams.Values = slices.Clone(x.routeParams.Values)
|
||||
|
||||
clone.RoutePatterns = slices.Clone(x.RoutePatterns)
|
||||
clone.methodsAllowed = slices.Clone(x.methodsAllowed)
|
||||
|
||||
return &clone
|
||||
}
|
||||
|
||||
// URLParam returns the corresponding URL parameter value from the request
|
||||
// routing context.
|
||||
func (x *Context) URLParam(key string) string {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package chi
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRoutePattern tests correct in-the-middle wildcard removals.
|
||||
// If user organizes a router like this:
|
||||
|
|
@ -102,3 +105,51 @@ func TestReplaceWildcardsConsecutive(t *testing.T) {
|
|||
t.Fatalf("unexpected trailing wildcard behavior: %s", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_Clone(t *testing.T) {
|
||||
orig := &Context{
|
||||
parentCtx: context.Background(),
|
||||
RoutePatterns: []string{"/v1", "/resources/{id}"},
|
||||
methodsAllowed: []methodTyp{mHEAD, mGET},
|
||||
URLParams: RouteParams{
|
||||
Keys: []string{"foo"},
|
||||
Values: []string{"bar"},
|
||||
},
|
||||
routeParams: RouteParams{
|
||||
Keys: []string{"id"},
|
||||
Values: []string{"123"},
|
||||
},
|
||||
}
|
||||
|
||||
clone := orig.Clone()
|
||||
orig.Reset()
|
||||
|
||||
orig.URLParams.Keys = append(orig.URLParams.Keys, "bar")
|
||||
orig.URLParams.Values = append(orig.URLParams.Values, "baz")
|
||||
orig.routeParams.Keys = append(orig.routeParams.Keys, "name")
|
||||
orig.routeParams.Values = append(orig.routeParams.Values, "foxmulder")
|
||||
orig.RoutePatterns = append(orig.RoutePatterns, "/mutated")
|
||||
orig.methodsAllowed = append(orig.methodsAllowed, mPOST)
|
||||
|
||||
if got := clone.URLParams.Keys[0]; got != "foo" {
|
||||
t.Fatalf("clone URLParams.Keys was corrupted, want %q got %q", "foo", got)
|
||||
}
|
||||
if got := clone.URLParams.Values[0]; got != "bar" {
|
||||
t.Fatalf("clone URLParams.Values was corrupted, want %q got %q", "bar", got)
|
||||
}
|
||||
if got := clone.routeParams.Keys[0]; got != "id" {
|
||||
t.Fatalf("clone routeParams.Keys was corrupted, want %q got %q", "id", got)
|
||||
}
|
||||
if got := clone.routeParams.Values[0]; got != "123" {
|
||||
t.Fatalf("clone routeParams.Values was corrupted, want %q got %q", "123", got)
|
||||
}
|
||||
if got := clone.RoutePatterns[0]; got != "/v1" {
|
||||
t.Fatalf("clone RoutePatterns[0] was corrupted, want %q got %q", "/v1", got)
|
||||
}
|
||||
if got := clone.methodsAllowed[0]; got != mHEAD {
|
||||
t.Fatalf("clone methodsAllowed[0] was corrupted, want %d got %d", mHEAD, got)
|
||||
}
|
||||
if clone.parentCtx != nil {
|
||||
t.Fatalf("clone parentCtx should be detached, got %v", clone.parentCtx)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue