Skip to content

Commit

Permalink
Memory alignment on structs (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
chemidy committed Feb 28, 2021
1 parent 2d3ff23 commit fb48a47
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions chain.go
Expand Up @@ -10,21 +10,21 @@ func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares {
// Handler builds and returns a http.Handler from the chain of middlewares,
// with `h http.Handler` as the final handler.
func (mws Middlewares) Handler(h http.Handler) http.Handler {
return &ChainHandler{mws, h, chain(mws, h)}
return &ChainHandler{h, chain(mws, h), mws}
}

// HandlerFunc builds and returns a http.Handler from the chain of middlewares,
// with `h http.Handler` as the final handler.
func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler {
return &ChainHandler{mws, h, chain(mws, h)}
return &ChainHandler{h, chain(mws, h), mws}
}

// ChainHandler is a http.Handler with support for handler composition and
// execution.
type ChainHandler struct {
Middlewares Middlewares
Endpoint http.Handler
chain http.Handler
Middlewares Middlewares
}

func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
26 changes: 13 additions & 13 deletions context.go
Expand Up @@ -45,37 +45,37 @@ var (
type Context struct {
Routes Routes

// parentCtx is the parent of this one, for using Context as a
// context.Context directly. This is an optimization that saves
// 1 allocation.
parentCtx context.Context

// Routing path/method override used during the route search.
// See Mux#routeHTTP method.
RoutePath string
RouteMethod string

// Routing pattern stack throughout the lifecycle of the request,
// across all connected routers. It is a record of all matching
// patterns across a stack of sub-routers.
RoutePatterns []string

// URLParams are the stack of routeParams captured during the
// routing lifecycle across a stack of sub-routers.
URLParams RouteParams

// Route parameters matched for the current sub-router. It is
// intentionally unexported so it cant be tampered.
routeParams RouteParams

// The endpoint routing pattern that matched the request URI path
// or `RoutePath` of the current sub-router. This value will update
// during the lifecycle of a request passing through a stack of
// sub-routers.
routePattern string

// Route parameters matched for the current sub-router. It is
// intentionally unexported so it cant be tampered.
routeParams RouteParams
// Routing pattern stack throughout the lifecycle of the request,
// across all connected routers. It is a record of all matching
// patterns across a stack of sub-routers.
RoutePatterns []string

// methodNotAllowed hint
methodNotAllowed bool

// parentCtx is the parent of this one, for using Context as a
// context.Context directly. This is an optimization that saves
// 1 allocation.
parentCtx context.Context
}

// Reset a routing context to its initial state.
Expand Down
4 changes: 2 additions & 2 deletions middleware/throttle.go
Expand Up @@ -18,10 +18,10 @@ var (

// ThrottleOpts represents a set of throttling options.
type ThrottleOpts struct {
RetryAfterFn func(ctxDone bool) time.Duration
Limit int
BacklogLimit int
BacklogTimeout time.Duration
RetryAfterFn func(ctxDone bool) time.Duration
}

// Throttle is a middleware that limits number of currently processed requests
Expand Down Expand Up @@ -119,8 +119,8 @@ type token struct{}
type throttler struct {
tokens chan token
backlogTokens chan token
backlogTimeout time.Duration
retryAfterFn func(ctxDone bool) time.Duration
backlogTimeout time.Duration
}

// setRetryAfterHeaderIfNeeded sets Retry-After HTTP header if corresponding retryAfterFn option of throttler is initialized.
Expand Down
4 changes: 2 additions & 2 deletions middleware/wrap_writer.go
Expand Up @@ -57,10 +57,10 @@ type WrapResponseWriter interface {
// http.ResponseWriter interface.
type basicWriter struct {
http.ResponseWriter
wroteHeader bool
tee io.Writer
code int
bytes int
tee io.Writer
wroteHeader bool
}

func (b *basicWriter) WriteHeader(code int) {
Expand Down
6 changes: 3 additions & 3 deletions tree.go
Expand Up @@ -631,7 +631,7 @@ func (n *node) routes() []Route {
hs[m] = h.handler
}

rt := Route{p, hs, subroutes}
rt := Route{subroutes, hs, p}
rts = append(rts, rt)
}

Expand Down Expand Up @@ -815,9 +815,9 @@ func (ns nodes) findEdge(label byte) *node {
// Route describes the details of a routing handler.
// Handlers map key is an HTTP method
type Route struct {
Pattern string
Handlers map[string]http.Handler
SubRoutes Routes
Handlers map[string]http.Handler
Pattern string
}

// WalkFunc is the type of the function called for each method and route visited by Walk.
Expand Down

0 comments on commit fb48a47

Please sign in to comment.