Skip to content

Commit

Permalink
feat(http): http client support service discovery in Do (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
letian0805 authored Nov 5, 2021
1 parent 4f013de commit 32272fe
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions transport/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,27 +232,7 @@ func (client *Client) Invoke(ctx context.Context, method, path string, args inte

func (client *Client) invoke(ctx context.Context, req *http.Request, args interface{}, reply interface{}, c callInfo, opts ...CallOption) error {
h := func(ctx context.Context, in interface{}) (interface{}, error) {
var done func(context.Context, selector.DoneInfo)
if client.r != nil {
var (
err error
node selector.Node
)
if node, done, err = client.opts.selector.Select(ctx); err != nil {
return nil, errors.ServiceUnavailable("NODE_NOT_FOUND", err.Error())
}
if client.insecure {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = "https"
}
req.URL.Host = node.Address()
req.Host = node.Address()
}
res, err := client.do(ctx, req, c)
if done != nil {
done(ctx, selector.DoneInfo{Err: err})
}
if res != nil {
cs := csAttempt{res: res}
for _, o := range opts {
Expand Down Expand Up @@ -284,16 +264,39 @@ func (client *Client) Do(req *http.Request, opts ...CallOption) (*http.Response,
return nil, err
}
}
return client.do(req.Context(), req, c)
ctx := req.Context()

return client.do(ctx, req, c)
}

func (client *Client) do(ctx context.Context, req *http.Request, c callInfo) (*http.Response, error) {
var done func(context.Context, selector.DoneInfo)
if client.r != nil {
var (
err error
node selector.Node
)
if node, done, err = client.opts.selector.Select(ctx); err != nil {
return nil, errors.ServiceUnavailable("NODE_NOT_FOUND", err.Error())
}
if client.insecure {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = "https"
}
req.URL.Host = node.Address()
req.Host = node.Address()
}
resp, err := client.cc.Do(req)
if err == nil {
err = client.opts.errorDecoder(ctx, resp)
}

if err != nil {
return nil, err
}
if err := client.opts.errorDecoder(ctx, resp); err != nil {
return nil, err
if done != nil {
done(ctx, selector.DoneInfo{Err: err})
}
return resp, nil
}
Expand Down

0 comments on commit 32272fe

Please sign in to comment.