Strip MetadataKey global var

This commit is contained in:
Asim Aslam 2020-04-12 11:16:08 +01:00
parent cf67d460b7
commit 962588b649
4 changed files with 15 additions and 15 deletions

View File

@ -118,7 +118,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// create context // create context
cx := ctx.FromRequest(r) cx := ctx.FromRequest(r)
// get context from http handler wrappers // get context from http handler wrappers
md, ok := r.Context().Value(metadata.MetadataKey{}).(metadata.Metadata) md, ok := metadata.FromContext(r.Context())
if !ok { if !ok {
md = make(metadata.Metadata) md = make(metadata.Metadata)
} }
@ -293,7 +293,7 @@ func requestPayload(r *http.Request) ([]byte, error) {
// otherwise as per usual // otherwise as per usual
ctx := r.Context() ctx := r.Context()
// dont user meadata.FromContext as it mangles names // dont user meadata.FromContext as it mangles names
md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata) md, ok := metadata.FromContext(ctx)
if !ok { if !ok {
md = make(map[string]string) md = make(map[string]string)
} }

View File

@ -271,7 +271,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
} }
pMatch = true pMatch = true
ctx := req.Context() ctx := req.Context()
md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata) md, ok := metadata.FromContext(ctx)
if !ok { if !ok {
md = make(metadata.Metadata) md = make(metadata.Metadata)
} }
@ -279,7 +279,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
md[fmt.Sprintf("x-api-field-%s", k)] = v md[fmt.Sprintf("x-api-field-%s", k)] = v
} }
md["x-api-body"] = ep.apiep.Body md["x-api-body"] = ep.apiep.Body
*req = *req.Clone(context.WithValue(ctx, metadata.MetadataKey{}, md)) *req = *req.Clone(metadata.NewContext(ctx, md))
break pathLoop break pathLoop
} }
if !pMatch { if !pMatch {

View File

@ -6,7 +6,7 @@ import (
"strings" "strings"
) )
type MetadataKey struct{} type metadataKey struct{}
// Metadata is our way of representing request headers internally. // Metadata is our way of representing request headers internally.
// They're used at the RPC level and translate back and forth // They're used at the RPC level and translate back and forth
@ -57,7 +57,7 @@ func Set(ctx context.Context, k, v string) context.Context {
} else { } else {
md[k] = v md[k] = v
} }
return context.WithValue(ctx, MetadataKey{}, md) return context.WithValue(ctx, metadataKey{}, md)
} }
// Get returns a single value from metadata in the context // Get returns a single value from metadata in the context
@ -80,7 +80,7 @@ func Get(ctx context.Context, key string) (string, bool) {
// FromContext returns metadata from the given context // FromContext returns metadata from the given context
func FromContext(ctx context.Context) (Metadata, bool) { func FromContext(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(MetadataKey{}).(Metadata) md, ok := ctx.Value(metadataKey{}).(Metadata)
if !ok { if !ok {
return nil, ok return nil, ok
} }
@ -96,7 +96,7 @@ func FromContext(ctx context.Context) (Metadata, bool) {
// NewContext creates a new context with the given metadata // NewContext creates a new context with the given metadata
func NewContext(ctx context.Context, md Metadata) context.Context { func NewContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, MetadataKey{}, md) return context.WithValue(ctx, metadataKey{}, md)
} }
// MergeContext merges metadata to existing metadata, overwriting if specified // MergeContext merges metadata to existing metadata, overwriting if specified
@ -104,7 +104,7 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
if ctx == nil { if ctx == nil {
ctx = context.Background() ctx = context.Background()
} }
md, _ := ctx.Value(MetadataKey{}).(Metadata) md, _ := ctx.Value(metadataKey{}).(Metadata)
cmd := make(Metadata, len(md)) cmd := make(Metadata, len(md))
for k, v := range md { for k, v := range md {
cmd[k] = v cmd[k] = v
@ -118,5 +118,5 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
delete(cmd, k) delete(cmd, k)
} }
} }
return context.WithValue(ctx, MetadataKey{}, cmd) return context.WithValue(ctx, metadataKey{}, cmd)
} }

View File

@ -28,10 +28,10 @@ type Sync interface {
// Leader provides leadership election // Leader provides leadership election
type Leader interface { type Leader interface {
// resign leadership // resign leadership
Resign() error Resign() error
// status returns when leadership is lost // status returns when leadership is lost
Status() chan bool Status() chan bool
} }
type Options struct { type Options struct {
@ -41,7 +41,7 @@ type Options struct {
type Option func(o *Options) type Option func(o *Options)
type LeaderOptions struct {} type LeaderOptions struct{}
type LeaderOption func(o *LeaderOptions) type LeaderOption func(o *LeaderOptions)