further transport rework

This commit is contained in:
Asim
2015-05-21 19:24:57 +01:00
parent b555109fbb
commit 9d514f0e60
11 changed files with 209 additions and 137 deletions

View File

@@ -1,33 +1,43 @@
package client
import (
"github.com/myodc/go-micro/transport"
)
type Client interface {
NewRequest(string, string, interface{}) Request
NewProtoRequest(string, string, interface{}) Request
NewJsonRequest(string, string, interface{}) Request
Call(interface{}, interface{}) error
CallRemote(string, string, interface{}, interface{}) error
Call(Request, interface{}) error
CallRemote(string, string, Request, interface{}) error
}
type options struct {
transport transport.Transport
}
type Options func(*options)
var (
client = NewRpcClient()
DefaultClient Client = NewRpcClient()
)
func Call(request Request, response interface{}) error {
return client.Call(request, response)
return DefaultClient.Call(request, response)
}
func CallRemote(address, path string, request Request, response interface{}) error {
return client.CallRemote(address, path, request, response)
return DefaultClient.CallRemote(address, path, request, response)
}
func NewRequest(service, method string, request interface{}) Request {
return client.NewRequest(service, method, request)
return DefaultClient.NewRequest(service, method, request)
}
func NewProtoRequest(service, method string, request interface{}) Request {
return client.NewProtoRequest(service, method, request)
return DefaultClient.NewProtoRequest(service, method, request)
}
func NewJsonRequest(service, method string, request interface{}) Request {
return client.NewJsonRequest(service, method, request)
return DefaultClient.NewJsonRequest(service, method, request)
}

View File

@@ -22,7 +22,7 @@ type headerRoundTripper struct {
}
type RpcClient struct {
transport transport.Transport
opts options
}
func init() {
@@ -86,7 +86,7 @@ func (r *RpcClient) call(address, path string, request Request, response interfa
msg.Header["Content-Type"] = request.ContentType()
c, err := r.transport.NewClient(request.Service(), address)
c, err := r.opts.transport.NewClient(address)
if err != nil {
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
}
@@ -146,24 +146,39 @@ func (r *RpcClient) Call(request Request, response interface{}) error {
n := rand.Int() % len(service.Nodes())
node := service.Nodes()[n]
address := fmt.Sprintf("%s:%d", node.Address(), node.Port())
return r.call(address, "/_rpc", request, response)
address := node.Address()
if node.Port() > 0 {
address = fmt.Sprintf("%s:%d", address, node.Port())
}
return r.call(address, "", request, response)
}
func (r *RpcClient) NewRequest(service, method string, request interface{}) *RpcRequest {
func (r *RpcClient) NewRequest(service, method string, request interface{}) Request {
return r.NewProtoRequest(service, method, request)
}
func (r *RpcClient) NewProtoRequest(service, method string, request interface{}) *RpcRequest {
func (r *RpcClient) NewProtoRequest(service, method string, request interface{}) Request {
return newRpcRequest(service, method, request, "application/octet-stream")
}
func (r *RpcClient) NewJsonRequest(service, method string, request interface{}) *RpcRequest {
func (r *RpcClient) NewJsonRequest(service, method string, request interface{}) Request {
return newRpcRequest(service, method, request, "application/json")
}
func NewRpcClient() *RpcClient {
func NewRpcClient(opt ...Options) *RpcClient {
var opts options
for _, o := range opt {
o(&opts)
}
if opts.transport == nil {
opts.transport = transport.DefaultTransport
}
return &RpcClient{
transport: transport.DefaultTransport,
opts: opts,
}
}