From 9ae0956cea2aaa705453f41ddd5d127ca544b8e3 Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 28 Jan 2016 17:55:28 +0000 Subject: [PATCH 1/2] Move context to metadata --- client/rpc_client.go | 8 +++---- context/context.go | 29 -------------------------- examples/client/dc_filter/dc_filter.go | 6 +++--- examples/client/main.go | 6 +++--- examples/client/pub/pub.go | 4 ++-- examples/client/wrapper/wrapper.go | 8 +++---- examples/server/handler/example.go | 4 ++-- examples/service/wrapper/main.go | 4 ++-- metadata/metadata.go | 29 ++++++++++++++++++++++++++ server/rpc_server.go | 4 ++-- server/subscriber.go | 4 ++-- service.go | 4 ++-- wrapper.go | 10 ++++----- 13 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 context/context.go create mode 100644 metadata/metadata.go diff --git a/client/rpc_client.go b/client/rpc_client.go index 9189f8ff..d9628697 100644 --- a/client/rpc_client.go +++ b/client/rpc_client.go @@ -8,8 +8,8 @@ import ( "github.com/micro/go-micro/broker" "github.com/micro/go-micro/codec" - c "github.com/micro/go-micro/context" "github.com/micro/go-micro/errors" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/selector" "github.com/micro/go-micro/transport" @@ -56,7 +56,7 @@ func (r *rpcClient) call(ctx context.Context, address string, req Request, resp Header: make(map[string]string), } - md, ok := c.GetMetadata(ctx) + md, ok := metadata.FromContext(ctx) if ok { for k, v := range md { msg.Header[k] = v @@ -120,7 +120,7 @@ func (r *rpcClient) stream(ctx context.Context, address string, req Request) (St Header: make(map[string]string), } - md, ok := c.GetMetadata(ctx) + md, ok := metadata.FromContext(ctx) if ok { for k, v := range md { msg.Header[k] = v @@ -264,7 +264,7 @@ func (r *rpcClient) Stream(ctx context.Context, request Request, opts ...CallOpt } func (r *rpcClient) Publish(ctx context.Context, p Publication, opts ...PublishOption) error { - md, ok := c.GetMetadata(ctx) + md, ok := metadata.FromContext(ctx) if !ok { md = make(map[string]string) } diff --git a/context/context.go b/context/context.go deleted file mode 100644 index b7c8ab93..00000000 --- a/context/context.go +++ /dev/null @@ -1,29 +0,0 @@ -package context - -import ( - "golang.org/x/net/context" -) - -type key int - -const ( - mdKey = key(0) -) - -type Metadata map[string]string - -func GetMetadata(ctx context.Context) (Metadata, bool) { - md, ok := ctx.Value(mdKey).(Metadata) - return md, ok -} - -func WithMetadata(ctx context.Context, md Metadata) context.Context { - if emd, ok := ctx.Value(mdKey).(Metadata); ok { - for k, v := range emd { - if _, ok := md[k]; !ok { - md[k] = v - } - } - } - return context.WithValue(ctx, mdKey, md) -} diff --git a/examples/client/dc_filter/dc_filter.go b/examples/client/dc_filter/dc_filter.go index cd2c7cec..49d52a0e 100644 --- a/examples/client/dc_filter/dc_filter.go +++ b/examples/client/dc_filter/dc_filter.go @@ -7,7 +7,7 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/cmd" - c "github.com/micro/go-micro/context" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/selector" "golang.org/x/net/context" @@ -25,7 +25,7 @@ type dcWrapper struct { } func (dc *dcWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - md, _ := c.GetMetadata(ctx) + md, _ := metadata.FromContext(ctx) filter := func(services []*registry.Service) []*registry.Service { for _, service := range services { @@ -59,7 +59,7 @@ func call(i int) { }) // create context with metadata - ctx := c.WithMetadata(context.Background(), map[string]string{ + ctx := metadata.NewContext(context.Background(), map[string]string{ "datacenter": "local", }) diff --git a/examples/client/main.go b/examples/client/main.go index 2fb229e5..3c86d94e 100644 --- a/examples/client/main.go +++ b/examples/client/main.go @@ -5,8 +5,8 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/cmd" - c "github.com/micro/go-micro/context" example "github.com/micro/go-micro/examples/server/proto/example" + "github.com/micro/go-micro/metadata" "golang.org/x/net/context" ) @@ -17,7 +17,7 @@ func pub() { }) // create context with metadata - ctx := c.WithMetadata(context.Background(), map[string]string{ + ctx := metadata.NewContext(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) @@ -38,7 +38,7 @@ func call(i int) { }) // create context with metadata - ctx := c.WithMetadata(context.Background(), map[string]string{ + ctx := metadata.NewContext(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) diff --git a/examples/client/pub/pub.go b/examples/client/pub/pub.go index b522fd43..83d804e5 100644 --- a/examples/client/pub/pub.go +++ b/examples/client/pub/pub.go @@ -5,8 +5,8 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/cmd" - c "github.com/micro/go-micro/context" example "github.com/micro/go-micro/examples/server/proto/example" + "github.com/micro/go-micro/metadata" "golang.org/x/net/context" ) @@ -17,7 +17,7 @@ func pub(i int) { }) // create context with metadata - ctx := c.WithMetadata(context.Background(), map[string]string{ + ctx := metadata.NewContext(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) diff --git a/examples/client/wrapper/wrapper.go b/examples/client/wrapper/wrapper.go index ca35772e..b87c62af 100644 --- a/examples/client/wrapper/wrapper.go +++ b/examples/client/wrapper/wrapper.go @@ -6,8 +6,8 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/cmd" - c "github.com/micro/go-micro/context" example "github.com/micro/go-micro/examples/server/proto/example" + "github.com/micro/go-micro/metadata" "golang.org/x/net/context" ) @@ -19,7 +19,7 @@ type logWrapper struct { } func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - md, _ := c.GetMetadata(ctx) + md, _ := metadata.FromContext(ctx) fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method()) return l.Client.Call(ctx, req, rsp) } @@ -30,7 +30,7 @@ type traceWrapper struct { } func (t *traceWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - ctx = c.WithMetadata(ctx, map[string]string{ + ctx = metadata.NewContext(ctx, map[string]string{ "X-Trace-Id": fmt.Sprintf("%d", time.Now().Unix()), }) return t.Client.Call(ctx, req, rsp) @@ -53,7 +53,7 @@ func call(i int) { }) // create context with metadata - ctx := c.WithMetadata(context.Background(), map[string]string{ + ctx := metadata.NewContext(context.Background(), map[string]string{ "X-User-Id": "john", "X-From-Id": "script", }) diff --git a/examples/server/handler/example.go b/examples/server/handler/example.go index e79a574f..f2fe3b52 100644 --- a/examples/server/handler/example.go +++ b/examples/server/handler/example.go @@ -2,8 +2,8 @@ package handler import ( log "github.com/golang/glog" - c "github.com/micro/go-micro/context" example "github.com/micro/go-micro/examples/server/proto/example" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/server" "golang.org/x/net/context" @@ -12,7 +12,7 @@ import ( type Example struct{} func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error { - md, _ := c.GetMetadata(ctx) + md, _ := metadata.FromContext(ctx) log.Infof("Received Example.Call request with metadata: %v", md) rsp.Msg = server.DefaultOptions().Id + ": Hello " + req.Name return nil diff --git a/examples/service/wrapper/main.go b/examples/service/wrapper/main.go index bcfe646a..d8f3b6eb 100644 --- a/examples/service/wrapper/main.go +++ b/examples/service/wrapper/main.go @@ -7,8 +7,8 @@ import ( "github.com/micro/cli" "github.com/micro/go-micro" "github.com/micro/go-micro/client" - c "github.com/micro/go-micro/context" proto "github.com/micro/go-micro/examples/service/proto" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/server" "golang.org/x/net/context" ) @@ -24,7 +24,7 @@ type logWrapper struct { } func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - md, _ := c.GetMetadata(ctx) + md, _ := metadata.FromContext(ctx) fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method()) return l.Client.Call(ctx, req, rsp) } diff --git a/metadata/metadata.go b/metadata/metadata.go new file mode 100644 index 00000000..57e0a11b --- /dev/null +++ b/metadata/metadata.go @@ -0,0 +1,29 @@ +package metadata + +import ( + "golang.org/x/net/context" +) + +type contextKeyT string + +const ( + contextKey = contextKeyT("github.com/micro/go-micro/metadata") +) + +type Metadata map[string]string + +func FromContext(ctx context.Context) (Metadata, bool) { + md, ok := ctx.Value(contextKey).(Metadata) + return md, ok +} + +func NewContext(ctx context.Context, md Metadata) context.Context { + if emd, ok := ctx.Value(contextKey).(Metadata); ok { + for k, v := range emd { + if _, ok := md[k]; !ok { + md[k] = v + } + } + } + return context.WithValue(ctx, contextKey, md) +} diff --git a/server/rpc_server.go b/server/rpc_server.go index 97f1f921..374642e7 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -9,7 +9,7 @@ import ( "github.com/micro/go-micro/broker" "github.com/micro/go-micro/codec" - c "github.com/micro/go-micro/context" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/transport" @@ -79,7 +79,7 @@ func (s *rpcServer) accept(sock transport.Socket) { } delete(hdr, "Content-Type") - ctx := c.WithMetadata(context.Background(), hdr) + ctx := metadata.NewContext(context.Background(), hdr) // TODO: needs better error handling if err := s.rpc.serveRequest(ctx, codec, ct); err != nil { diff --git a/server/subscriber.go b/server/subscriber.go index 04b5cb0b..c615bf4c 100644 --- a/server/subscriber.go +++ b/server/subscriber.go @@ -7,7 +7,7 @@ import ( "github.com/micro/go-micro/broker" "github.com/micro/go-micro/codec" - c "github.com/micro/go-micro/context" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/registry" "golang.org/x/net/context" ) @@ -175,7 +175,7 @@ func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handle hdr[k] = v } delete(hdr, "Content-Type") - ctx := c.WithMetadata(context.Background(), hdr) + ctx := metadata.NewContext(context.Background(), hdr) for i := 0; i < len(sb.handlers); i++ { handler := sb.handlers[i] diff --git a/service.go b/service.go index f50fb254..3b4017af 100644 --- a/service.go +++ b/service.go @@ -8,7 +8,7 @@ import ( "github.com/micro/go-micro/client" "github.com/micro/go-micro/cmd" - "github.com/micro/go-micro/context" + "github.com/micro/go-micro/metadata" "github.com/micro/go-micro/server" ) @@ -21,7 +21,7 @@ func newService(opts ...Option) Service { options.Client = &clientWrapper{ options.Client, - context.Metadata{ + metadata.Metadata{ HeaderPrefix + "From-Service": options.Server.Options().Name, }, } diff --git a/wrapper.go b/wrapper.go index c75dbbc5..961d7793 100644 --- a/wrapper.go +++ b/wrapper.go @@ -2,27 +2,27 @@ package micro import ( "github.com/micro/go-micro/client" - cx "github.com/micro/go-micro/context" + "github.com/micro/go-micro/metadata" "golang.org/x/net/context" ) type clientWrapper struct { client.Client - headers cx.Metadata + headers metadata.Metadata } func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { - ctx = cx.WithMetadata(ctx, c.headers) + ctx = metadata.NewContext(ctx, c.headers) return c.Client.Call(ctx, req, rsp, opts...) } func (c *clientWrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Streamer, error) { - ctx = cx.WithMetadata(ctx, c.headers) + ctx = metadata.NewContext(ctx, c.headers) return c.Client.Stream(ctx, req, opts...) } func (c *clientWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error { - ctx = cx.WithMetadata(ctx, c.headers) + ctx = metadata.NewContext(ctx, c.headers) return c.Client.Publish(ctx, p, opts...) } From b9f4e17d4cc3a97ce93e57d81ed782cb8038f632 Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 28 Jan 2016 18:24:56 +0000 Subject: [PATCH 2/2] Meta key change --- metadata/metadata.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 57e0a11b..530e6014 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -4,26 +4,22 @@ import ( "golang.org/x/net/context" ) -type contextKeyT string - -const ( - contextKey = contextKeyT("github.com/micro/go-micro/metadata") -) +type metaKey struct{} type Metadata map[string]string func FromContext(ctx context.Context) (Metadata, bool) { - md, ok := ctx.Value(contextKey).(Metadata) + md, ok := ctx.Value(metaKey{}).(Metadata) return md, ok } func NewContext(ctx context.Context, md Metadata) context.Context { - if emd, ok := ctx.Value(contextKey).(Metadata); ok { + if emd, ok := ctx.Value(metaKey{}).(Metadata); ok { for k, v := range emd { if _, ok := md[k]; !ok { md[k] = v } } } - return context.WithValue(ctx, contextKey, md) + return context.WithValue(ctx, metaKey{}, md) }