Move context to metadata

This commit is contained in:
Asim 2016-01-28 17:55:28 +00:00
parent cd9801891e
commit 9ae0956cea
13 changed files with 60 additions and 60 deletions

View File

@ -8,8 +8,8 @@ import (
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
c "github.com/micro/go-micro/context"
"github.com/micro/go-micro/errors" "github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/selector" "github.com/micro/go-micro/selector"
"github.com/micro/go-micro/transport" "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), Header: make(map[string]string),
} }
md, ok := c.GetMetadata(ctx) md, ok := metadata.FromContext(ctx)
if ok { if ok {
for k, v := range md { for k, v := range md {
msg.Header[k] = v 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), Header: make(map[string]string),
} }
md, ok := c.GetMetadata(ctx) md, ok := metadata.FromContext(ctx)
if ok { if ok {
for k, v := range md { for k, v := range md {
msg.Header[k] = v 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 { func (r *rpcClient) Publish(ctx context.Context, p Publication, opts ...PublishOption) error {
md, ok := c.GetMetadata(ctx) md, ok := metadata.FromContext(ctx)
if !ok { if !ok {
md = make(map[string]string) md = make(map[string]string)
} }

View File

@ -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)
}

View File

@ -7,7 +7,7 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "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/registry"
"github.com/micro/go-micro/selector" "github.com/micro/go-micro/selector"
"golang.org/x/net/context" "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 { 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 { filter := func(services []*registry.Service) []*registry.Service {
for _, service := range services { for _, service := range services {
@ -59,7 +59,7 @@ func call(i int) {
}) })
// create context with metadata // create context with metadata
ctx := c.WithMetadata(context.Background(), map[string]string{ ctx := metadata.NewContext(context.Background(), map[string]string{
"datacenter": "local", "datacenter": "local",
}) })

View File

@ -5,8 +5,8 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example" example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/metadata"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -17,7 +17,7 @@ func pub() {
}) })
// create context with metadata // 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-User-Id": "john",
"X-From-Id": "script", "X-From-Id": "script",
}) })
@ -38,7 +38,7 @@ func call(i int) {
}) })
// create context with metadata // 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-User-Id": "john",
"X-From-Id": "script", "X-From-Id": "script",
}) })

View File

@ -5,8 +5,8 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example" example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/metadata"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -17,7 +17,7 @@ func pub(i int) {
}) })
// create context with metadata // 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-User-Id": "john",
"X-From-Id": "script", "X-From-Id": "script",
}) })

View File

@ -6,8 +6,8 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example" example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/metadata"
"golang.org/x/net/context" "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 { 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()) fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method())
return l.Client.Call(ctx, req, rsp) 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 { 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()), "X-Trace-Id": fmt.Sprintf("%d", time.Now().Unix()),
}) })
return t.Client.Call(ctx, req, rsp) return t.Client.Call(ctx, req, rsp)
@ -53,7 +53,7 @@ func call(i int) {
}) })
// create context with metadata // 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-User-Id": "john",
"X-From-Id": "script", "X-From-Id": "script",
}) })

View File

@ -2,8 +2,8 @@ package handler
import ( import (
log "github.com/golang/glog" log "github.com/golang/glog"
c "github.com/micro/go-micro/context"
example "github.com/micro/go-micro/examples/server/proto/example" example "github.com/micro/go-micro/examples/server/proto/example"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -12,7 +12,7 @@ import (
type Example struct{} type Example struct{}
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error { 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) log.Infof("Received Example.Call request with metadata: %v", md)
rsp.Msg = server.DefaultOptions().Id + ": Hello " + req.Name rsp.Msg = server.DefaultOptions().Id + ": Hello " + req.Name
return nil return nil

View File

@ -7,8 +7,8 @@ import (
"github.com/micro/cli" "github.com/micro/cli"
"github.com/micro/go-micro" "github.com/micro/go-micro"
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
c "github.com/micro/go-micro/context"
proto "github.com/micro/go-micro/examples/service/proto" proto "github.com/micro/go-micro/examples/service/proto"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"golang.org/x/net/context" "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 { 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()) fmt.Printf("[Log Wrapper] ctx: %v service: %s method: %s\n", md, req.Service(), req.Method())
return l.Client.Call(ctx, req, rsp) return l.Client.Call(ctx, req, rsp)
} }

29
metadata/metadata.go Normal file
View File

@ -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)
}

View File

@ -9,7 +9,7 @@ import (
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec" "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/registry"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
@ -79,7 +79,7 @@ func (s *rpcServer) accept(sock transport.Socket) {
} }
delete(hdr, "Content-Type") delete(hdr, "Content-Type")
ctx := c.WithMetadata(context.Background(), hdr) ctx := metadata.NewContext(context.Background(), hdr)
// TODO: needs better error handling // TODO: needs better error handling
if err := s.rpc.serveRequest(ctx, codec, ct); err != nil { if err := s.rpc.serveRequest(ctx, codec, ct); err != nil {

View File

@ -7,7 +7,7 @@ import (
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/codec" "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/registry"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -175,7 +175,7 @@ func (s *rpcServer) createSubHandler(sb *subscriber, opts Options) broker.Handle
hdr[k] = v hdr[k] = v
} }
delete(hdr, "Content-Type") delete(hdr, "Content-Type")
ctx := c.WithMetadata(context.Background(), hdr) ctx := metadata.NewContext(context.Background(), hdr)
for i := 0; i < len(sb.handlers); i++ { for i := 0; i < len(sb.handlers); i++ {
handler := sb.handlers[i] handler := sb.handlers[i]

View File

@ -8,7 +8,7 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd" "github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/context" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
) )
@ -21,7 +21,7 @@ func newService(opts ...Option) Service {
options.Client = &clientWrapper{ options.Client = &clientWrapper{
options.Client, options.Client,
context.Metadata{ metadata.Metadata{
HeaderPrefix + "From-Service": options.Server.Options().Name, HeaderPrefix + "From-Service": options.Server.Options().Name,
}, },
} }

View File

@ -2,27 +2,27 @@ package micro
import ( import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
cx "github.com/micro/go-micro/context" "github.com/micro/go-micro/metadata"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
type clientWrapper struct { type clientWrapper struct {
client.Client client.Client
headers cx.Metadata headers metadata.Metadata
} }
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { 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...) return c.Client.Call(ctx, req, rsp, opts...)
} }
func (c *clientWrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Streamer, error) { 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...) return c.Client.Stream(ctx, req, opts...)
} }
func (c *clientWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error { 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...) return c.Client.Publish(ctx, p, opts...)
} }