Rework use of context

This commit is contained in:
Asim
2015-05-23 11:53:40 +01:00
parent d67c1ba111
commit 3db51216b2
16 changed files with 79 additions and 248 deletions

View File

@@ -5,16 +5,17 @@ import (
h "github.com/grpc/grpc-common/go/helloworld"
"github.com/myodc/go-micro/client"
"golang.org/x/net/context"
)
// run github.com/grpc/grpc-common/go/greeter_server/main.go
func main() {
req := client.NewRpcRequest("helloworld.Greeter", "SayHello", &h.HelloRequest{
req := client.NewRpcRequest("helloworld.Greeter", "helloworld.Greeter/SayHello", &h.HelloRequest{
Name: "John",
}, "application/grpc")
rsp := &h.HelloReply{}
err := client.CallRemote("localhost:50051", "helloworld.Greeter/SayHello", req, rsp)
err := client.CallRemote(context.Background(), "localhost:50051", req, rsp)
if err != nil {
fmt.Println(err)
}

View File

@@ -5,7 +5,9 @@ import (
"github.com/myodc/go-micro/client"
"github.com/myodc/go-micro/cmd"
c "github.com/myodc/go-micro/context"
example "github.com/myodc/go-micro/template/proto/example"
"golang.org/x/net/context"
)
func main() {
@@ -16,14 +18,16 @@ func main() {
Name: "John",
})
// Set arbitrary headers
req.Headers().Set("X-User-Id", "john")
req.Headers().Set("X-From-Id", "script")
// create context with metadata
ctx := c.WithMetaData(context.Background(), map[string]string{
"X-User-Id": "john",
"X-From-Id": "script",
})
rsp := &example.Response{}
// Call service
if err := client.Call(req, rsp); err != nil {
if err := client.Call(ctx, req, rsp); err != nil {
fmt.Println(err)
return
}