commit
6b7f2f3bd3
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
@ -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",
|
||||
})
|
||||
|
||||
|
@ -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",
|
||||
})
|
||||
|
@ -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",
|
||||
})
|
||||
|
@ -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",
|
||||
})
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
25
metadata/metadata.go
Normal file
25
metadata/metadata.go
Normal file
@ -0,0 +1,25 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type metaKey struct{}
|
||||
|
||||
type Metadata map[string]string
|
||||
|
||||
func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
md, ok := ctx.Value(metaKey{}).(Metadata)
|
||||
return md, ok
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
||||
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, metaKey{}, md)
|
||||
}
|
@ -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 {
|
||||
|
@ -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]
|
||||
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
10
wrapper.go
10
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...)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user