2020-09-05 02:11:29 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-16 16:35:46 +03:00
|
|
|
"fmt"
|
2023-12-21 00:13:08 +03:00
|
|
|
"os"
|
2024-04-22 23:39:21 +03:00
|
|
|
"strconv"
|
2022-04-16 16:35:46 +03:00
|
|
|
"time"
|
2020-09-05 02:11:29 +03:00
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2022-05-03 15:59:38 +03:00
|
|
|
"go.unistack.org/micro/v3/selector"
|
2024-04-22 23:39:21 +03:00
|
|
|
"go.unistack.org/micro/v3/semconv"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2020-09-05 02:11:29 +03:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultCodecs will be used to encode/decode data
|
|
|
|
var DefaultCodecs = map[string]codec.Codec{
|
|
|
|
"application/octet-stream": codec.NewCodec(),
|
|
|
|
}
|
2020-11-23 16:18:47 +03:00
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
type noopClient struct {
|
2024-04-22 08:47:50 +03:00
|
|
|
funcPublish FuncPublish
|
|
|
|
funcBatchPublish FuncBatchPublish
|
|
|
|
funcCall FuncCall
|
|
|
|
funcStream FuncStream
|
|
|
|
opts Options
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type noopMessage struct {
|
2020-10-09 13:43:04 +03:00
|
|
|
topic string
|
|
|
|
payload interface{}
|
|
|
|
opts MessageOptions
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type noopRequest struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
body interface{}
|
|
|
|
codec codec.Codec
|
2020-09-05 02:11:29 +03:00
|
|
|
service string
|
|
|
|
method string
|
|
|
|
endpoint string
|
|
|
|
contentType string
|
|
|
|
stream bool
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewClient returns new noop client
|
|
|
|
func NewClient(opts ...Option) Client {
|
2024-04-22 08:47:50 +03:00
|
|
|
n := &noopClient{opts: NewOptions(opts...)}
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
n.funcCall = n.fnCall
|
|
|
|
n.funcStream = n.fnStream
|
|
|
|
n.funcPublish = n.fnPublish
|
|
|
|
n.funcBatchPublish = n.fnBatchPublish
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
return n
|
2020-11-03 01:08:23 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (n *noopClient) Name() string {
|
|
|
|
return n.opts.Name
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
func (n *noopRequest) Service() string {
|
|
|
|
return n.service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Method() string {
|
|
|
|
return n.method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Endpoint() string {
|
|
|
|
return n.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) ContentType() string {
|
|
|
|
return n.contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Body() interface{} {
|
|
|
|
return n.body
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopRequest) Codec() codec.Codec {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopRequest) Stream() bool {
|
|
|
|
return n.stream
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopResponse struct {
|
2020-11-23 16:18:47 +03:00
|
|
|
codec codec.Codec
|
2020-11-18 16:50:41 +03:00
|
|
|
header metadata.Metadata
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopResponse) Codec() codec.Codec {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.codec
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:50:41 +03:00
|
|
|
func (n *noopResponse) Header() metadata.Metadata {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopResponse) Read() ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 23:39:21 +03:00
|
|
|
type noopStream struct {
|
|
|
|
err error
|
|
|
|
ctx context.Context
|
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
|
|
|
|
func (n *noopStream) Context() context.Context {
|
2024-04-22 23:39:21 +03:00
|
|
|
return n.ctx
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Request() Request {
|
|
|
|
return &noopRequest{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Response() Response {
|
|
|
|
return &noopResponse{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Send(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Recv(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *noopStream) SendMsg(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) RecvMsg(interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
func (n *noopStream) Error() error {
|
2024-04-22 23:39:21 +03:00
|
|
|
return n.err
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopStream) Close() error {
|
2024-04-22 23:39:21 +03:00
|
|
|
if sp, ok := tracer.SpanFromContext(n.ctx); ok && sp != nil {
|
|
|
|
if n.err != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, n.err.Error())
|
|
|
|
}
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
return n.err
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
func (n *noopStream) CloseSend() error {
|
2024-04-22 23:39:21 +03:00
|
|
|
return n.err
|
2022-01-10 16:47:37 +03:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
func (n *noopMessage) Topic() string {
|
|
|
|
return n.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopMessage) Payload() interface{} {
|
|
|
|
return n.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopMessage) ContentType() string {
|
2020-10-09 13:43:04 +03:00
|
|
|
return n.opts.ContentType
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:03:42 +03:00
|
|
|
func (n *noopMessage) Metadata() metadata.Metadata {
|
|
|
|
return n.opts.Metadata
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
func (n *noopClient) newCodec(contentType string) (codec.Codec, error) {
|
|
|
|
if cf, ok := n.opts.Codecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
return nil, codec.ErrUnknownContentType
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Init(opts ...Option) error {
|
2020-09-05 02:11:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.opts)
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
n.funcCall = n.fnCall
|
|
|
|
n.funcStream = n.fnStream
|
|
|
|
n.funcPublish = n.fnPublish
|
|
|
|
n.funcBatchPublish = n.fnBatchPublish
|
|
|
|
|
|
|
|
n.opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
switch h := hook.(type) {
|
|
|
|
case HookCall:
|
|
|
|
n.funcCall = h(n.funcCall)
|
|
|
|
case HookStream:
|
|
|
|
n.funcStream = h(n.funcStream)
|
|
|
|
case HookPublish:
|
|
|
|
n.funcPublish = h(n.funcPublish)
|
|
|
|
case HookBatchPublish:
|
|
|
|
n.funcBatchPublish = h(n.funcBatchPublish)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Options() Options {
|
2020-09-05 02:11:29 +03:00
|
|
|
return n.opts
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) String() string {
|
2020-09-05 02:11:29 +03:00
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error {
|
2024-04-22 23:39:21 +03:00
|
|
|
ts := time.Now()
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
|
|
|
var sp tracer.Span
|
2024-12-09 00:41:08 +03:00
|
|
|
ctx, sp = n.opts.Tracer.Start(ctx, "rpc-client",
|
2024-04-22 23:39:21 +03:00
|
|
|
tracer.WithSpanKind(tracer.SpanKindClient),
|
|
|
|
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
|
|
|
)
|
|
|
|
err := n.funcCall(ctx, req, rsp, opts...)
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Dec()
|
|
|
|
te := time.Since(ts)
|
|
|
|
n.opts.Meter.Summary(semconv.ClientRequestLatencyMicroseconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
n.opts.Meter.Histogram(semconv.ClientRequestDurationSeconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
|
|
|
if me := errors.FromError(err); me == nil {
|
|
|
|
sp.Finish()
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "success", "code", strconv.Itoa(int(200))).Inc()
|
|
|
|
} else {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "failure", "code", strconv.Itoa(int(me.Code))).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopClient) fnCall(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error {
|
2022-04-16 16:35:46 +03:00
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := n.opts.CallOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
// no deadline so we create a new one
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
|
|
|
opt := WithRequestTimeout(time.Until(d))
|
|
|
|
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
|
2024-04-22 08:47:50 +03:00
|
|
|
hcall := func(ctx context.Context, addr string, req Request, rsp interface{}, opts CallOptions) error {
|
|
|
|
return nil
|
2022-04-16 16:35:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the router passed as a call option, or fallback to the rpc clients router
|
|
|
|
if callOpts.Router == nil {
|
|
|
|
callOpts.Router = n.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = n.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(n.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{n.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:59:38 +03:00
|
|
|
var next selector.Next
|
2022-04-16 16:35:46 +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 {
|
2024-12-09 13:06:43 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", "%s", err)
|
2022-04-16 16:35:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:59:38 +03:00
|
|
|
if next == nil {
|
|
|
|
var routes []string
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err = n.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
2024-12-09 13:06:43 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", "%s", err)
|
2022-05-03 15:59:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err = callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 16:35:46 +03:00
|
|
|
node := next()
|
|
|
|
|
|
|
|
// make the call
|
|
|
|
err = hcall(ctx, node, req, rsp, callOpts)
|
|
|
|
// record the result of the call to inform future routing decisions
|
|
|
|
if verr := n.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
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, callOpts.Retries)
|
|
|
|
var gerr error
|
|
|
|
|
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-12-09 13:06:43 +03:00
|
|
|
func (n *noopClient) NewRequest(service, endpoint string, _ interface{}, _ ...RequestOption) Request {
|
2021-02-18 15:57:42 +03:00
|
|
|
return &noopRequest{service: service, endpoint: endpoint}
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message {
|
2021-07-27 23:58:06 +03:00
|
|
|
options := NewMessageOptions(append([]MessageOption{MessageContentType(n.opts.ContentType)}, opts...)...)
|
2020-10-09 13:43:04 +03:00
|
|
|
return &noopMessage{topic: topic, payload: msg, opts: options}
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) {
|
2024-04-22 23:39:21 +03:00
|
|
|
ts := time.Now()
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
|
|
|
var sp tracer.Span
|
2024-12-09 00:41:08 +03:00
|
|
|
ctx, sp = n.opts.Tracer.Start(ctx, "rpc-client",
|
2024-04-22 23:39:21 +03:00
|
|
|
tracer.WithSpanKind(tracer.SpanKindClient),
|
|
|
|
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
|
|
|
)
|
|
|
|
stream, err := n.funcStream(ctx, req, opts...)
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Dec()
|
|
|
|
te := time.Since(ts)
|
|
|
|
n.opts.Meter.Summary(semconv.ClientRequestLatencyMicroseconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
n.opts.Meter.Histogram(semconv.ClientRequestDurationSeconds, "endpoint", req.Endpoint()).Update(te.Seconds())
|
|
|
|
|
|
|
|
if me := errors.FromError(err); me == nil {
|
|
|
|
sp.Finish()
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "success", "code", strconv.Itoa(int(200))).Inc()
|
|
|
|
} else {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
n.opts.Meter.Counter(semconv.ClientRequestTotal, "endpoint", req.Endpoint(), "status", "failure", "code", strconv.Itoa(int(me.Code))).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream, err
|
2024-04-22 08:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopClient) fnStream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) {
|
2022-05-03 15:59:38 +03:00
|
|
|
var err error
|
|
|
|
|
2022-04-16 16:35:46 +03:00
|
|
|
// make a copy of call opts
|
|
|
|
callOpts := n.opts.CallOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we already have a deadline
|
|
|
|
d, ok := ctx.Deadline()
|
|
|
|
if !ok && callOpts.StreamTimeout > time.Duration(0) {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
// no deadline so we create a new one
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, callOpts.StreamTimeout)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
// got a deadline so no need to setup context
|
|
|
|
// but we need to set the timeout we pass along
|
|
|
|
o := WithStreamTimeout(time.Until(d))
|
|
|
|
o(&callOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we noop right here?
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, errors.New("go.micro.client", fmt.Sprintf("%v", ctx.Err()), 408)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// make copy of call method
|
|
|
|
hstream := h.stream
|
|
|
|
// 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 = n.opts.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
if callOpts.Selector == nil {
|
|
|
|
callOpts.Selector = n.opts.Selector
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject proxy address
|
|
|
|
// TODO: don't even bother using Lookup/Select in this case
|
|
|
|
if len(n.opts.Proxy) > 0 {
|
|
|
|
callOpts.Address = []string{n.opts.Proxy}
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:59:38 +03:00
|
|
|
var next selector.Next
|
2022-04-16 16:35:46 +03:00
|
|
|
|
|
|
|
call := func(i int) (Stream, error) {
|
|
|
|
// call backoff first. Someone may want an initial start delay
|
|
|
|
t, cerr := callOpts.Backoff(ctx, req, i)
|
|
|
|
if cerr != nil {
|
2024-12-09 13:06:43 +03:00
|
|
|
return nil, errors.InternalServerError("go.micro.client", "%s", cerr)
|
2022-04-16 16:35:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only sleep if greater than 0
|
|
|
|
if t.Seconds() > 0 {
|
|
|
|
time.Sleep(t)
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:59:38 +03:00
|
|
|
if next == nil {
|
|
|
|
var routes []string
|
|
|
|
// lookup the route to send the reques to
|
|
|
|
// TODO apply any filtering here
|
|
|
|
routes, err = n.opts.Lookup(ctx, req, callOpts)
|
|
|
|
if err != nil {
|
2024-12-09 13:06:43 +03:00
|
|
|
return nil, errors.InternalServerError("go.micro.client", "%s", err)
|
2022-05-03 15:59:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// balance the list of nodes
|
|
|
|
next, err = callOpts.Selector.Select(routes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 16:35:46 +03:00
|
|
|
node := next()
|
|
|
|
|
|
|
|
stream, cerr := n.stream(ctx, node, req, callOpts)
|
|
|
|
|
|
|
|
// record the result of the call to inform future routing decisions
|
|
|
|
if verr := n.opts.Selector.Record(node, cerr); verr != nil {
|
|
|
|
return nil, verr
|
|
|
|
}
|
|
|
|
|
|
|
|
// try and transform the error to a go-micro error
|
|
|
|
if verr, ok := cerr.(*errors.Error); ok {
|
|
|
|
return nil, verr
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream, cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
stream Stream
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan response, callOpts.Retries)
|
|
|
|
var grr error
|
|
|
|
|
|
|
|
for i := 0; i <= callOpts.Retries; i++ {
|
|
|
|
go func() {
|
|
|
|
s, cerr := call(i)
|
|
|
|
ch <- response{s, cerr}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-12-09 13:06:43 +03:00
|
|
|
func (n *noopClient) stream(ctx context.Context, _ string, _ Request, _ CallOptions) (Stream, error) {
|
2024-04-22 23:39:21 +03:00
|
|
|
return &noopStream{ctx: ctx}, nil
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
func (n *noopClient) BatchPublish(ctx context.Context, ps []Message, opts ...PublishOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcBatchPublish(ctx, ps, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopClient) fnBatchPublish(ctx context.Context, ps []Message, opts ...PublishOption) error {
|
2021-07-24 16:14:42 +03:00
|
|
|
return n.publish(ctx, ps, opts...)
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
func (n *noopClient) Publish(ctx context.Context, p Message, opts ...PublishOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
return n.funcPublish(ctx, p, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopClient) fnPublish(ctx context.Context, p Message, opts ...PublishOption) error {
|
2021-07-24 16:14:42 +03:00
|
|
|
return n.publish(ctx, []Message{p}, opts...)
|
|
|
|
}
|
2020-10-09 13:43:04 +03:00
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
func (n *noopClient) publish(ctx context.Context, ps []Message, opts ...PublishOption) error {
|
2020-10-16 09:38:57 +03:00
|
|
|
options := NewPublishOptions(opts...)
|
2020-10-09 13:43:04 +03:00
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
msgs := make([]*broker.Message, 0, len(ps))
|
|
|
|
|
2023-12-21 00:13:08 +03:00
|
|
|
// get proxy
|
|
|
|
exchange := ""
|
|
|
|
if v, ok := os.LookupEnv("MICRO_PROXY"); ok {
|
|
|
|
exchange = v
|
|
|
|
}
|
|
|
|
// get the exchange
|
|
|
|
if len(options.Exchange) > 0 {
|
|
|
|
exchange = options.Exchange
|
|
|
|
}
|
|
|
|
|
|
|
|
omd, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
omd = metadata.New(0)
|
|
|
|
}
|
2023-10-26 03:06:39 +03:00
|
|
|
|
2023-12-21 00:13:08 +03:00
|
|
|
for _, p := range ps {
|
2023-12-21 00:06:56 +03:00
|
|
|
md := metadata.Copy(omd)
|
2023-12-21 00:13:08 +03:00
|
|
|
md[metadata.HeaderContentType] = p.ContentType()
|
|
|
|
topic := p.Topic()
|
|
|
|
if len(exchange) > 0 {
|
|
|
|
topic = exchange
|
|
|
|
}
|
|
|
|
md[metadata.HeaderTopic] = topic
|
2023-10-26 03:06:39 +03:00
|
|
|
iter := p.Metadata().Iterator()
|
|
|
|
var k, v string
|
|
|
|
for iter.Next(&k, &v) {
|
|
|
|
md.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
var body []byte
|
|
|
|
|
|
|
|
// passed in raw data
|
|
|
|
if d, ok := p.Payload().(*codec.Frame); ok {
|
|
|
|
body = d.Data
|
|
|
|
} else {
|
|
|
|
// use codec for payload
|
|
|
|
cf, err := n.newCodec(p.ContentType())
|
|
|
|
if err != nil {
|
2024-12-09 13:06:43 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", "%s", err)
|
2021-07-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the body
|
|
|
|
b, err := cf.Marshal(p.Payload())
|
|
|
|
if err != nil {
|
2024-12-09 13:06:43 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", "%s", err)
|
2021-07-24 16:14:42 +03:00
|
|
|
}
|
|
|
|
body = b
|
|
|
|
}
|
2020-10-09 13:43:04 +03:00
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
msgs = append(msgs, &broker.Message{Header: md, Body: body})
|
2020-10-09 13:43:04 +03:00
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
if len(msgs) == 1 {
|
|
|
|
return n.opts.Broker.Publish(ctx, msgs[0].Header[metadata.HeaderTopic], msgs[0],
|
|
|
|
broker.PublishContext(options.Context),
|
|
|
|
broker.PublishBodyOnly(options.BodyOnly),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-24 16:14:42 +03:00
|
|
|
return n.opts.Broker.BatchPublish(ctx, msgs,
|
2021-06-13 12:20:35 +03:00
|
|
|
broker.PublishContext(options.Context),
|
|
|
|
broker.PublishBodyOnly(options.BodyOnly),
|
|
|
|
)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|