From 962588b64970f11c98b79d3cd6345821a97a0ec5 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sun, 12 Apr 2020 11:16:08 +0100 Subject: [PATCH] Strip MetadataKey global var --- api/handler/rpc/rpc.go | 4 ++-- api/router/static/static.go | 4 ++-- metadata/metadata.go | 12 ++++++------ sync/sync.go | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/api/handler/rpc/rpc.go b/api/handler/rpc/rpc.go index 980333b5..7c06b663 100644 --- a/api/handler/rpc/rpc.go +++ b/api/handler/rpc/rpc.go @@ -118,7 +118,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // create context cx := ctx.FromRequest(r) // get context from http handler wrappers - md, ok := r.Context().Value(metadata.MetadataKey{}).(metadata.Metadata) + md, ok := metadata.FromContext(r.Context()) if !ok { md = make(metadata.Metadata) } @@ -293,7 +293,7 @@ func requestPayload(r *http.Request) ([]byte, error) { // otherwise as per usual ctx := r.Context() // dont user meadata.FromContext as it mangles names - md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata) + md, ok := metadata.FromContext(ctx) if !ok { md = make(map[string]string) } diff --git a/api/router/static/static.go b/api/router/static/static.go index 0c659c31..2b0218f1 100644 --- a/api/router/static/static.go +++ b/api/router/static/static.go @@ -271,7 +271,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) { } pMatch = true ctx := req.Context() - md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata) + md, ok := metadata.FromContext(ctx) if !ok { 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["x-api-body"] = ep.apiep.Body - *req = *req.Clone(context.WithValue(ctx, metadata.MetadataKey{}, md)) + *req = *req.Clone(metadata.NewContext(ctx, md)) break pathLoop } if !pMatch { diff --git a/metadata/metadata.go b/metadata/metadata.go index bd539314..c99766a8 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -6,7 +6,7 @@ import ( "strings" ) -type MetadataKey struct{} +type metadataKey struct{} // Metadata is our way of representing request headers internally. // 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 { 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 @@ -80,7 +80,7 @@ func Get(ctx context.Context, key string) (string, bool) { // FromContext returns metadata from the given context func FromContext(ctx context.Context) (Metadata, bool) { - md, ok := ctx.Value(MetadataKey{}).(Metadata) + md, ok := ctx.Value(metadataKey{}).(Metadata) if !ok { return nil, ok } @@ -96,7 +96,7 @@ func FromContext(ctx context.Context) (Metadata, bool) { // NewContext creates a new context with the given metadata 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 @@ -104,7 +104,7 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context if ctx == nil { ctx = context.Background() } - md, _ := ctx.Value(MetadataKey{}).(Metadata) + md, _ := ctx.Value(metadataKey{}).(Metadata) cmd := make(Metadata, len(md)) for k, v := range md { cmd[k] = v @@ -118,5 +118,5 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context delete(cmd, k) } } - return context.WithValue(ctx, MetadataKey{}, cmd) + return context.WithValue(ctx, metadataKey{}, cmd) } diff --git a/sync/sync.go b/sync/sync.go index ebaec6da..0c5203b7 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -28,10 +28,10 @@ type Sync interface { // Leader provides leadership election type Leader interface { - // resign leadership - Resign() error - // status returns when leadership is lost - Status() chan bool + // resign leadership + Resign() error + // status returns when leadership is lost + Status() chan bool } type Options struct { @@ -41,7 +41,7 @@ type Options struct { type Option func(o *Options) -type LeaderOptions struct {} +type LeaderOptions struct{} type LeaderOption func(o *LeaderOptions)