Dont expose rpc client/server

This commit is contained in:
Asim
2015-05-23 17:40:53 +01:00
parent 3d8dd6e121
commit e192f335da
8 changed files with 73 additions and 83 deletions

View File

@@ -20,7 +20,7 @@ type options struct {
type Option func(*options)
var (
DefaultClient Client = NewRpcClient()
DefaultClient Client = newRpcClient()
)
func Transport(t transport.Transport) Option {
@@ -37,6 +37,10 @@ func CallRemote(ctx context.Context, address string, request Request, response i
return DefaultClient.CallRemote(ctx, address, request, response)
}
func New(opt ...Option) Client {
return newRpcClient(opt...)
}
func NewRequest(service, method string, request interface{}) Request {
return DefaultClient.NewRequest(service, method, request)
}

View File

@@ -24,7 +24,7 @@ type headerRoundTripper struct {
r http.RoundTripper
}
type RpcClient struct {
type rpcClient struct {
opts options
}
@@ -32,12 +32,28 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
func newRpcClient(opt ...Option) Client {
var opts options
for _, o := range opt {
o(&opts)
}
if opts.transport == nil {
opts.transport = transport.DefaultTransport
}
return &rpcClient{
opts: opts,
}
}
func (t *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("X-Client-Version", "1.0")
return t.r.RoundTrip(r)
}
func (r *RpcClient) call(ctx context.Context, address 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)
@@ -132,12 +148,12 @@ func (r *RpcClient) call(ctx context.Context, address string, request Request, r
return nil
}
func (r *RpcClient) CallRemote(ctx context.Context, address string, request Request, response interface{}) error {
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(ctx context.Context, 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())
@@ -158,30 +174,14 @@ func (r *RpcClient) Call(ctx context.Context, request Request, response interfac
return r.call(ctx, address, request, response)
}
func (r *RpcClient) NewRequest(service, method string, request interface{}) Request {
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{}) Request {
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{}) Request {
func (r *rpcClient) NewJsonRequest(service, method string, request interface{}) Request {
return newRpcRequest(service, method, request, "application/json")
}
func NewRpcClient(opt ...Option) *RpcClient {
var opts options
for _, o := range opt {
o(&opts)
}
if opts.transport == nil {
opts.transport = transport.DefaultTransport
}
return &RpcClient{
opts: opts,
}
}

View File

@@ -1,14 +1,14 @@
package client
type RpcRequest struct {
type rpcRequest struct {
service string
method string
contentType string
request interface{}
}
func newRpcRequest(service, method string, request interface{}, contentType string) *RpcRequest {
return &RpcRequest{
func newRpcRequest(service, method string, request interface{}, contentType string) Request {
return &rpcRequest{
service: service,
method: method,
request: request,
@@ -16,22 +16,18 @@ func newRpcRequest(service, method string, request interface{}, contentType stri
}
}
func (r *RpcRequest) ContentType() string {
func (r *rpcRequest) ContentType() string {
return r.contentType
}
func (r *RpcRequest) Service() string {
func (r *rpcRequest) Service() string {
return r.service
}
func (r *RpcRequest) Method() string {
func (r *rpcRequest) Method() string {
return r.method
}
func (r *RpcRequest) Request() interface{} {
func (r *rpcRequest) Request() interface{} {
return r.request
}
func NewRpcRequest(service, method string, request interface{}, contentType string) *RpcRequest {
return newRpcRequest(service, method, request, contentType)
}