mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Strip MetadataKey global var
This commit is contained in:
parent
cf67d460b7
commit
962588b649
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
10
sync/sync.go
10
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user