2017-01-01 23:30:40 +03:00
|
|
|
// Package http provides a http client
|
2021-10-25 20:35:02 +03:00
|
|
|
package http // import "go.unistack.org/micro-client-http/v3"
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-03-03 15:28:44 +03:00
|
|
|
"context"
|
2017-01-01 21:39:05 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-11-23 01:09:18 +03:00
|
|
|
"os"
|
2021-04-10 12:46:31 +03:00
|
|
|
"strings"
|
2021-04-16 17:09:29 +03:00
|
|
|
"sync"
|
2017-01-01 21:39:05 +03:00
|
|
|
"time"
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2017-01-01 21:39:05 +03:00
|
|
|
)
|
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
var DefaultContentType = "application/json"
|
2021-02-06 18:54:18 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
/*
|
2020-07-17 01:29:28 +03:00
|
|
|
func filterLabel(r []router.Route) []router.Route {
|
|
|
|
// selector.FilterLabel("protocol", "http")
|
|
|
|
return r
|
|
|
|
}
|
2021-04-25 16:26:36 +03:00
|
|
|
*/
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
type httpClient struct {
|
2021-01-10 14:48:10 +03:00
|
|
|
httpcli *http.Client
|
2021-07-05 16:10:38 +03:00
|
|
|
opts client.Options
|
2021-04-16 17:09:29 +03:00
|
|
|
sync.RWMutex
|
2021-07-05 16:10:38 +03:00
|
|
|
init bool
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 16:27:49 +03:00
|
|
|
func newRequest(ctx context.Context, addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) {
|
2021-03-23 00:29:27 +03:00
|
|
|
var tags []string
|
2021-10-25 19:59:37 +03:00
|
|
|
var parameters map[string]map[string]string
|
2021-07-09 16:27:49 +03:00
|
|
|
scheme := "http"
|
|
|
|
method := http.MethodPost
|
|
|
|
body := "*" // as like google api http annotation
|
|
|
|
host := addr
|
|
|
|
path := req.Endpoint()
|
2021-04-25 16:26:36 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
u, err := url.Parse(addr)
|
2021-07-09 16:27:49 +03:00
|
|
|
if err == nil {
|
|
|
|
scheme = u.Scheme
|
|
|
|
path = u.Path
|
|
|
|
host = u.Host
|
|
|
|
} else {
|
|
|
|
u = &url.URL{Scheme: scheme, Path: path, Host: host}
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
// nolint: nestif
|
2021-07-09 16:27:49 +03:00
|
|
|
if opts.Context != nil {
|
|
|
|
if m, ok := opts.Context.Value(methodKey{}).(string); ok {
|
|
|
|
method = m
|
2021-01-10 14:48:10 +03:00
|
|
|
}
|
2021-07-09 16:27:49 +03:00
|
|
|
if p, ok := opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
path += p
|
|
|
|
}
|
|
|
|
if b, ok := opts.Context.Value(bodyKey{}).(string); ok {
|
|
|
|
body = b
|
|
|
|
}
|
|
|
|
if t, ok := opts.Context.Value(structTagsKey{}).([]string); ok && len(t) > 0 {
|
|
|
|
tags = t
|
2021-01-10 14:48:10 +03:00
|
|
|
}
|
2021-10-25 19:59:37 +03:00
|
|
|
if k, ok := opts.Context.Value(headerKey{}).([]string); ok && len(k) > 0 {
|
|
|
|
if parameters == nil {
|
|
|
|
parameters = make(map[string]map[string]string)
|
|
|
|
}
|
|
|
|
m, ok := parameters["header"]
|
|
|
|
if !ok {
|
|
|
|
m = make(map[string]string)
|
|
|
|
parameters["header"] = m
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(k)/2; idx += 2 {
|
|
|
|
m[k[idx]] = k[idx+1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if k, ok := opts.Context.Value(cookieKey{}).([]string); ok && len(k) > 0 {
|
|
|
|
if parameters == nil {
|
|
|
|
parameters = make(map[string]map[string]string)
|
|
|
|
}
|
|
|
|
m, ok := parameters["cookie"]
|
|
|
|
if !ok {
|
|
|
|
m = make(map[string]string)
|
|
|
|
parameters["cookie"] = m
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(k)/2; idx += 2 {
|
|
|
|
m[k[idx]] = k[idx+1]
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
}
|
2021-07-09 16:27:49 +03:00
|
|
|
|
2021-03-23 00:29:27 +03:00
|
|
|
if len(tags) == 0 {
|
|
|
|
switch ct {
|
|
|
|
default:
|
|
|
|
tags = append(tags, "json", "protobuf")
|
|
|
|
case "text/xml":
|
|
|
|
tags = append(tags, "xml")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 16:27:49 +03:00
|
|
|
if path == "" {
|
|
|
|
path = req.Endpoint()
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err = u.Parse(path)
|
2021-01-19 04:12:31 +03:00
|
|
|
if err != nil {
|
2021-07-14 22:16:01 +03:00
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2021-09-01 02:25:29 +03:00
|
|
|
var nmsg interface{}
|
|
|
|
if len(u.Query()) > 0 {
|
2021-10-25 19:59:37 +03:00
|
|
|
path, nmsg, err = newPathRequest(u.Path+"?"+u.RawQuery, method, body, msg, tags, parameters)
|
2021-09-01 02:25:29 +03:00
|
|
|
} else {
|
2021-10-25 19:59:37 +03:00
|
|
|
path, nmsg, err = newPathRequest(u.Path, method, body, msg, tags, parameters)
|
2021-09-01 02:25:29 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 16:27:49 +03:00
|
|
|
if err != nil {
|
2021-07-14 22:16:01 +03:00
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
2021-03-24 10:44:53 +03:00
|
|
|
}
|
2021-07-09 16:27:49 +03:00
|
|
|
|
|
|
|
u, err = url.Parse(fmt.Sprintf("%s://%s%s", scheme, host, path))
|
2021-01-19 04:12:31 +03:00
|
|
|
if err != nil {
|
2021-07-14 22:16:01 +03:00
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
2021-01-19 04:12:31 +03:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:59:37 +03:00
|
|
|
var cookies []*http.Cookie
|
2021-07-09 16:27:49 +03:00
|
|
|
header := make(http.Header)
|
|
|
|
if opts.Context != nil {
|
|
|
|
if md, ok := opts.Context.Value(metadataKey{}).(metadata.Metadata); ok {
|
|
|
|
for k, v := range md {
|
|
|
|
header.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.AuthToken != "" {
|
2021-10-25 19:59:37 +03:00
|
|
|
header.Set(metadata.HeaderAuthorization, opts.AuthToken)
|
2021-03-21 17:44:48 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 16:27:49 +03:00
|
|
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
|
|
for k, v := range md {
|
2021-10-25 19:59:37 +03:00
|
|
|
header.Set(k, v)
|
2021-07-09 16:27:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set timeout in nanoseconds
|
|
|
|
if opts.StreamTimeout > time.Duration(0) {
|
2021-10-25 19:59:37 +03:00
|
|
|
header.Set(metadata.HeaderTimeout, fmt.Sprintf("%d", opts.StreamTimeout))
|
2021-07-09 16:27:49 +03:00
|
|
|
}
|
|
|
|
if opts.RequestTimeout > time.Duration(0) {
|
2021-10-25 19:59:37 +03:00
|
|
|
header.Set(metadata.HeaderTimeout, fmt.Sprintf("%d", opts.RequestTimeout))
|
2021-07-09 16:27:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the content type for the request
|
2021-10-25 19:59:37 +03:00
|
|
|
header.Set(metadata.HeaderContentType, ct)
|
|
|
|
var v interface{}
|
|
|
|
|
|
|
|
for km, vm := range parameters {
|
|
|
|
for k, required := range vm {
|
2021-10-27 13:13:33 +03:00
|
|
|
v, err = rutil.StructFieldByPath(msg, k)
|
2021-10-25 19:59:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
if rutil.IsZero(v) {
|
|
|
|
if required == "true" {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", fmt.Sprintf("required field %s not set", k))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch km {
|
|
|
|
case "header":
|
|
|
|
header.Set(k, fmt.Sprintf("%v", v))
|
|
|
|
case "cookie":
|
|
|
|
cookies = append(cookies, &http.Cookie{Name: k, Value: fmt.Sprintf("%v", v)})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cf.Marshal(nmsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var hreq *http.Request
|
|
|
|
if len(b) > 0 {
|
|
|
|
hreq, err = http.NewRequestWithContext(ctx, method, u.String(), ioutil.NopCloser(bytes.NewBuffer(b)))
|
|
|
|
hreq.ContentLength = int64(len(b))
|
|
|
|
header.Set("Content-Length", fmt.Sprintf("%d", hreq.ContentLength))
|
|
|
|
} else {
|
|
|
|
hreq, err = http.NewRequestWithContext(ctx, method, u.String(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.BadRequest("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
hreq.Header = header
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
hreq.AddCookie(cookie)
|
|
|
|
}
|
2021-07-09 16:27:49 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return hreq, nil
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
2021-03-23 00:29:27 +03:00
|
|
|
ct := req.ContentType()
|
2021-04-09 23:12:25 +03:00
|
|
|
if len(opts.ContentType) > 0 {
|
|
|
|
ct = opts.ContentType
|
|
|
|
}
|
2021-03-23 00:29:27 +03:00
|
|
|
|
2021-04-18 15:45:42 +03:00
|
|
|
cf, err := h.newCodec(ct)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
2021-07-09 16:27:49 +03:00
|
|
|
hreq, err := newRequest(ctx, addr, req, ct, cf, req.Body(), opts)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
2021-01-19 04:12:31 +03:00
|
|
|
return err
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// make the request
|
2021-07-09 16:27:49 +03:00
|
|
|
hrsp, err := h.httpcli.Do(hreq)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
2021-01-19 22:55:24 +03:00
|
|
|
switch err := err.(type) {
|
|
|
|
case *url.Error:
|
|
|
|
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
|
|
|
|
return errors.Timeout("go.micro.client", err.Error())
|
|
|
|
}
|
2021-04-25 16:26:36 +03:00
|
|
|
case net.Error:
|
|
|
|
if err.Timeout() {
|
|
|
|
return errors.Timeout("go.micro.client", err.Error())
|
|
|
|
}
|
2021-01-19 22:55:24 +03:00
|
|
|
}
|
2017-01-01 21:39:05 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
2021-01-19 22:55:24 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
defer hrsp.Body.Close()
|
2017-01-01 21:39:05 +03:00
|
|
|
|
2021-04-18 15:45:42 +03:00
|
|
|
return h.parseRsp(ctx, hrsp, rsp, opts)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) stream(ctx context.Context, addr string, req client.Request, opts client.CallOptions) (client.Stream, error) {
|
2021-03-23 00:29:27 +03:00
|
|
|
ct := req.ContentType()
|
2021-04-09 23:12:25 +03:00
|
|
|
if len(opts.ContentType) > 0 {
|
|
|
|
ct = opts.ContentType
|
|
|
|
}
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
// get codec
|
2021-04-16 17:09:29 +03:00
|
|
|
cf, err := h.newCodec(ct)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:48:39 +03:00
|
|
|
cc, err := (h.httpcli.Transport).(*http.Transport).DialContext(ctx, "tcp", addr)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpStream{
|
2021-01-10 14:48:10 +03:00
|
|
|
address: addr,
|
2017-01-01 21:39:05 +03:00
|
|
|
context: ctx,
|
|
|
|
closed: make(chan bool),
|
2021-01-10 14:48:10 +03:00
|
|
|
opts: opts,
|
2017-01-01 21:39:05 +03:00
|
|
|
conn: cc,
|
2021-03-23 00:29:27 +03:00
|
|
|
ct: ct,
|
|
|
|
cf: cf,
|
2017-01-01 21:39:05 +03:00
|
|
|
reader: bufio.NewReader(cc),
|
|
|
|
request: req,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func (h *httpClient) newCodec(ct string) (codec.Codec, error) {
|
2021-04-16 17:09:29 +03:00
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
|
|
|
|
|
|
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
|
|
ct = ct[:idx]
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
if c, ok := h.opts.Codecs[ct]; ok {
|
2017-01-01 21:39:05 +03:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
return nil, codec.ErrUnknownContentType
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Init(opts ...client.Option) error {
|
2021-03-24 15:49:20 +03:00
|
|
|
if len(opts) == 0 && h.init {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-01 21:39:05 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&h.opts)
|
|
|
|
}
|
2021-03-24 15:49:20 +03:00
|
|
|
|
|
|
|
if err := h.opts.Broker.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Router.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Transport.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Options() client.Options {
|
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:38:14 +03:00
|
|
|
func (h *httpClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
|
2021-01-10 14:48:10 +03:00
|
|
|
return newHTTPMessage(topic, msg, h.opts.ContentType, opts...)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-03-31 10:43:31 +03:00
|
|
|
func (h *httpClient) NewRequest(service, method string, req interface{}, opts ...client.RequestOption) client.Request {
|
|
|
|
return newHTTPRequest(service, method, req, h.opts.ContentType, opts...)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := h.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
var cancel context.CancelFunc
|
2017-01-01 21:39:05 +03:00
|
|
|
// no deadline so we create a new one
|
2021-01-10 14:48:10 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
|
|
|
|
defer cancel()
|
2017-01-01 21:39:05 +03:00
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
2021-04-25 16:26:36 +03:00
|
|
|
opt := client.WithRequestTimeout(time.Until(d))
|
2017-01-01 21:39:05 +03:00
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// make copy of call method
|
|
|
|
hcall := h.call
|
|
|
|
|
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
hcall = callOpts.CallWrappers[i-1](hcall)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
callOpts.Router = h.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = h.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(h.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{h.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err := h.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err := callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
// return errors.New("go.micro.client", "request timeout", 408)
|
|
|
|
call := func(i int) error {
|
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, err := callOpts.Backoff(ctx, req, i)
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
node := next()
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
// make the call
|
2019-01-18 18:47:50 +03:00
|
|
|
err = hcall(ctx, node, req, rsp, callOpts)
|
2021-01-10 14:48:10 +03:00
|
|
|
// record the result of the call to inform future routing decisions
|
|
|
|
if verr := h.opts.Selector.Record(node, err); verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
|
|
|
// try and transform the error to a go-micro error
|
|
|
|
if verr, ok := err.(*errors.Error); ok {
|
|
|
|
return verr
|
|
|
|
}
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, callOpts.Retries)
|
|
|
|
var gerr error
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2017-01-01 21:39:05 +03:00
|
|
|
go func() {
|
|
|
|
ch <- call(i)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case err := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:26:18 +03:00
|
|
|
func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
2017-01-01 21:39:05 +03:00
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := h.opts.CallOptions
|
2021-04-29 22:38:38 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&callOpts)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
2021-04-29 22:38:38 +03:00
|
|
|
if !ok && callOpts.StreamTimeout > time.Duration(0) {
|
2021-01-10 14:48:10 +03:00
|
|
|
var cancel context.CancelFunc
|
2017-01-01 21:39:05 +03:00
|
|
|
// no deadline so we create a new one
|
2021-04-29 22:38:38 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.StreamTimeout)
|
2021-01-10 14:48:10 +03:00
|
|
|
defer cancel()
|
2017-01-01 21:39:05 +03:00
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
2021-04-29 22:38:38 +03:00
|
|
|
o := client.WithStreamTimeout(time.Until(d))
|
|
|
|
o(&callOpts)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
/*
|
|
|
|
// make copy of call method
|
2021-04-29 22:38:38 +03:00
|
|
|
hstream := h.stream
|
2021-01-10 14:48:10 +03:00
|
|
|
// wrap the call in reverse
|
|
|
|
for i := len(callOpts.CallWrappers); i > 0; i-- {
|
|
|
|
hstream = callOpts.CallWrappers[i-1](hstream)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
callOpts.Router = h.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = h.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(h.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{h.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err := h.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err := callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:26:18 +03:00
|
|
|
call := func(i int) (client.Stream, error) {
|
2017-01-01 21:39:05 +03:00
|
|
|
// call backoff first. Someone may want an initial start delay
|
2021-04-25 16:26:36 +03:00
|
|
|
t, cerr := callOpts.Backoff(ctx, req, i)
|
|
|
|
if cerr != nil {
|
|
|
|
return nil, errors.InternalServerError("go.micro.client", cerr.Error())
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
node := next()
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
stream, cerr := h.stream(ctx, node, req, callOpts)
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// record the result of the call to inform future routing decisions
|
2021-04-25 16:26:36 +03:00
|
|
|
if verr := h.opts.Selector.Record(node, cerr); verr != nil {
|
2021-01-10 14:48:10 +03:00
|
|
|
return nil, verr
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
// try and transform the error to a go-micro error
|
2021-04-25 16:26:36 +03:00
|
|
|
if verr, ok := cerr.(*errors.Error); ok {
|
2021-01-10 14:48:10 +03:00
|
|
|
return nil, verr
|
|
|
|
}
|
2020-07-17 01:29:28 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
return stream, cerr
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
2018-04-17 13:26:18 +03:00
|
|
|
stream client.Stream
|
2017-01-01 21:39:05 +03:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan response, callOpts.Retries)
|
|
|
|
var grr error
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
2017-01-01 21:39:05 +03:00
|
|
|
go func() {
|
2021-04-25 16:26:36 +03:00
|
|
|
s, cerr := call(i)
|
|
|
|
ch <- response{s, cerr}
|
2017-01-01 21:39:05 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
case rsp := <-ch:
|
|
|
|
// if the call succeeded lets bail early
|
|
|
|
if rsp.err == nil {
|
|
|
|
return rsp.stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry, rerr := callOpts.Retry(ctx, req, i, err)
|
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return nil, rsp.err
|
|
|
|
}
|
|
|
|
|
|
|
|
grr = rsp.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, grr
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
func (h *httpClient) BatchPublish(ctx context.Context, p []client.Message, opts ...client.PublishOption) error {
|
|
|
|
return h.publish(ctx, p, opts...)
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:26:18 +03:00
|
|
|
func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
2021-07-30 23:02:50 +03:00
|
|
|
return h.publish(ctx, []client.Message{p}, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) publish(ctx context.Context, ps []client.Message, opts ...client.PublishOption) error {
|
2021-01-10 14:48:10 +03:00
|
|
|
options := client.NewPublishOptions(opts...)
|
2019-11-23 01:09:18 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
// get proxy
|
|
|
|
exchange := ""
|
|
|
|
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
|
|
|
exchange = v
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-07-31 14:59:09 +03:00
|
|
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
omd = metadata.New(2)
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
msgs := make([]*broker.Message, 0, len(ps))
|
2017-01-01 21:39:05 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
for _, p := range ps {
|
2021-07-31 14:59:09 +03:00
|
|
|
md := metadata.Copy(omd)
|
2021-07-30 23:02:50 +03:00
|
|
|
md[metadata.HeaderContentType] = p.ContentType()
|
|
|
|
md[metadata.HeaderTopic] = p.Topic()
|
2019-11-23 01:09:18 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
cf, err := h.newCodec(p.ContentType())
|
|
|
|
if err != nil {
|
2019-11-23 01:09:18 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
2017-01-01 21:39:05 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
var body []byte
|
2019-11-23 01:09:18 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
// passed in raw data
|
|
|
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
|
|
|
body = d.Data
|
|
|
|
} else {
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
if err := cf.Write(b, &codec.Message{Type: codec.Event}, p.Payload()); err != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
body = b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := p.Topic()
|
|
|
|
if len(exchange) > 0 {
|
|
|
|
topic = exchange
|
|
|
|
}
|
2019-11-23 01:09:18 +03:00
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
md.Set(metadata.HeaderTopic, topic)
|
|
|
|
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
2019-11-23 01:09:18 +03:00
|
|
|
}
|
|
|
|
|
2021-07-30 23:02:50 +03:00
|
|
|
return h.opts.Broker.BatchPublish(ctx, msgs,
|
2021-06-11 15:21:36 +03:00
|
|
|
broker.PublishContext(ctx),
|
|
|
|
broker.PublishBodyOnly(options.BodyOnly),
|
|
|
|
)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpClient) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:11:04 +03:00
|
|
|
func (h *httpClient) Name() string {
|
|
|
|
return h.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
func NewClient(opts ...client.Option) client.Client {
|
|
|
|
options := client.NewOptions(opts...)
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
if len(options.ContentType) == 0 {
|
2021-02-06 18:54:18 +03:00
|
|
|
options.ContentType = DefaultContentType
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
rc := &httpClient{
|
|
|
|
opts: options,
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-03-26 15:48:39 +03:00
|
|
|
dialer, ok := options.Context.Value(httpDialerKey{}).(*net.Dialer)
|
|
|
|
if !ok {
|
|
|
|
dialer = &net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
if httpcli, ok := options.Context.Value(httpClientKey{}).(*http.Client); ok {
|
|
|
|
rc.httpcli = httpcli
|
|
|
|
} else {
|
2021-03-26 15:52:52 +03:00
|
|
|
// TODO customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
2021-03-26 15:48:39 +03:00
|
|
|
tr := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: dialer.DialContext,
|
|
|
|
ForceAttemptHTTP2: true,
|
|
|
|
MaxConnsPerHost: 100,
|
|
|
|
MaxIdleConns: 20,
|
|
|
|
IdleConnTimeout: 60 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
TLSClientConfig: options.TLSConfig,
|
|
|
|
}
|
|
|
|
rc.httpcli = &http.Client{Transport: tr}
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
c := client.Client(rc)
|
|
|
|
|
|
|
|
// wrap in reverse
|
|
|
|
for i := len(options.Wrappers); i > 0; i-- {
|
|
|
|
c = options.Wrappers[i-1](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|