Rework use of context
This commit is contained in:
@@ -2,14 +2,15 @@ package client
|
||||
|
||||
import (
|
||||
"github.com/myodc/go-micro/transport"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
NewRequest(string, string, interface{}) Request
|
||||
NewProtoRequest(string, string, interface{}) Request
|
||||
NewJsonRequest(string, string, interface{}) Request
|
||||
Call(Request, interface{}) error
|
||||
CallRemote(string, string, Request, interface{}) error
|
||||
Call(context.Context, Request, interface{}) error
|
||||
CallRemote(context.Context, string, Request, interface{}) error
|
||||
}
|
||||
|
||||
type options struct {
|
||||
@@ -28,12 +29,12 @@ func Transport(t transport.Transport) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func Call(request Request, response interface{}) error {
|
||||
return DefaultClient.Call(request, response)
|
||||
func Call(ctx context.Context, request Request, response interface{}) error {
|
||||
return DefaultClient.Call(ctx, request, response)
|
||||
}
|
||||
|
||||
func CallRemote(address, path string, request Request, response interface{}) error {
|
||||
return DefaultClient.CallRemote(address, path, request, response)
|
||||
func CallRemote(ctx context.Context, address string, request Request, response interface{}) error {
|
||||
return DefaultClient.CallRemote(ctx, address, request, response)
|
||||
}
|
||||
|
||||
func NewRequest(service, method string, request interface{}) Request {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package client
|
||||
|
||||
type Headers interface {
|
||||
Add(string, string)
|
||||
Del(string)
|
||||
Get(string) string
|
||||
Set(string, string)
|
||||
}
|
||||
@@ -5,5 +5,4 @@ type Request interface {
|
||||
Method() string
|
||||
ContentType() string
|
||||
Request() interface{}
|
||||
Headers() Headers
|
||||
}
|
||||
|
||||
@@ -7,13 +7,16 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
c "github.com/myodc/go-micro/context"
|
||||
"github.com/myodc/go-micro/errors"
|
||||
"github.com/myodc/go-micro/registry"
|
||||
"github.com/myodc/go-micro/transport"
|
||||
|
||||
rpc "github.com/youtube/vitess/go/rpcplus"
|
||||
js "github.com/youtube/vitess/go/rpcplus/jsonrpc"
|
||||
pb "github.com/youtube/vitess/go/rpcplus/pbrpc"
|
||||
ctx "golang.org/x/net/context"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
@@ -34,14 +37,14 @@ func (t *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)
|
||||
return t.r.RoundTrip(r)
|
||||
}
|
||||
|
||||
func (r *RpcClient) call(address, path string, request Request, response interface{}) error {
|
||||
func (r *RpcClient) call(ctx context.Context, address string, request Request, response interface{}) error {
|
||||
switch request.ContentType() {
|
||||
case "application/grpc":
|
||||
cc, err := grpc.Dial(address)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error connecting to server: %v", err))
|
||||
}
|
||||
if err := grpc.Invoke(ctx.Background(), path, request.Request(), response, cc); err != nil {
|
||||
if err := grpc.Invoke(ctx, request.Method(), request.Request(), response, cc); err != nil {
|
||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||
}
|
||||
return nil
|
||||
@@ -77,10 +80,10 @@ func (r *RpcClient) call(address, path string, request Request, response interfa
|
||||
Body: reqB.Bytes(),
|
||||
}
|
||||
|
||||
h, _ := request.Headers().(http.Header)
|
||||
for k, v := range h {
|
||||
if len(v) > 0 {
|
||||
msg.Header[k] = v[0]
|
||||
md, ok := c.GetMetaData(ctx)
|
||||
if ok {
|
||||
for k, v := range md {
|
||||
msg.Header[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,12 +132,12 @@ func (r *RpcClient) call(address, path string, request Request, response interfa
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RpcClient) CallRemote(address, path string, request Request, response interface{}) error {
|
||||
return r.call(address, path, request, response)
|
||||
func (r *RpcClient) CallRemote(ctx context.Context, address string, request Request, response interface{}) error {
|
||||
return r.call(ctx, address, request, response)
|
||||
}
|
||||
|
||||
// TODO: Call(..., opts *Options) error {
|
||||
func (r *RpcClient) Call(request Request, response interface{}) error {
|
||||
func (r *RpcClient) Call(ctx context.Context, request Request, response interface{}) error {
|
||||
service, err := registry.GetService(request.Service())
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
@@ -152,7 +155,7 @@ func (r *RpcClient) Call(request Request, response interface{}) error {
|
||||
address = fmt.Sprintf("%s:%d", address, node.Port())
|
||||
}
|
||||
|
||||
return r.call(address, "", request, response)
|
||||
return r.call(ctx, address, request, response)
|
||||
}
|
||||
|
||||
func (r *RpcClient) NewRequest(service, method string, request interface{}) Request {
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RpcRequest struct {
|
||||
service, method, contentType string
|
||||
request interface{}
|
||||
headers http.Header
|
||||
service string
|
||||
method string
|
||||
contentType string
|
||||
request interface{}
|
||||
}
|
||||
|
||||
func newRpcRequest(service, method string, request interface{}, contentType string) *RpcRequest {
|
||||
@@ -16,7 +13,6 @@ func newRpcRequest(service, method string, request interface{}, contentType stri
|
||||
method: method,
|
||||
request: request,
|
||||
contentType: contentType,
|
||||
headers: make(http.Header),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +20,6 @@ func (r *RpcRequest) ContentType() string {
|
||||
return r.contentType
|
||||
}
|
||||
|
||||
func (r *RpcRequest) Headers() Headers {
|
||||
return r.headers
|
||||
}
|
||||
|
||||
func (r *RpcRequest) Service() string {
|
||||
return r.service
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user