github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/client.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/client/client.go
    16  
    17  // Package client is an interface for an RPC client
    18  package client
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/util/codec"
    25  )
    26  
    27  var (
    28  	// DefaultClient implementation
    29  	DefaultClient Client
    30  )
    31  
    32  // Client is the interface used to make requests to services.
    33  // It supports Request/Response via Transport and Publishing via the Broker.
    34  // It also supports bidirectional streaming of requests.
    35  type Client interface {
    36  	Init(...Option) error
    37  	Options() Options
    38  	NewMessage(topic string, msg interface{}, opts ...MessageOption) Message
    39  	NewRequest(service, endpoint string, req interface{}, reqOpts ...RequestOption) Request
    40  	Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error
    41  	Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error)
    42  	Publish(ctx context.Context, msg Message, opts ...PublishOption) error
    43  	String() string
    44  }
    45  
    46  // Message is the interface for publishing asynchronously
    47  type Message interface {
    48  	Topic() string
    49  	Payload() interface{}
    50  	ContentType() string
    51  }
    52  
    53  // Request is the interface for a synchronous request used by Call or Stream
    54  type Request interface {
    55  	// The service to call
    56  	Service() string
    57  	// The action to take
    58  	Method() string
    59  	// The endpoint to invoke
    60  	Endpoint() string
    61  	// The content type
    62  	ContentType() string
    63  	// The unencoded request body
    64  	Body() interface{}
    65  	// Write to the encoded request writer. This is nil before a call is made
    66  	Codec() codec.Writer
    67  	// indicates whether the request will be a streaming one rather than unary
    68  	Stream() bool
    69  }
    70  
    71  // Response is the response received from a service
    72  type Response interface {
    73  	// Read the response
    74  	Codec() codec.Reader
    75  	// read the header
    76  	Header() map[string]string
    77  	// Read the undecoded response
    78  	Read() ([]byte, error)
    79  }
    80  
    81  // Stream is the interface for a bidirectional synchronous stream
    82  type Stream interface {
    83  	// Context for the stream
    84  	Context() context.Context
    85  	// The request made
    86  	Request() Request
    87  	// The response read
    88  	Response() Response
    89  	// Send will encode and send a request
    90  	Send(interface{}) error
    91  	// Recv will decode and read a response
    92  	Recv(interface{}) error
    93  	// Error returns the stream error
    94  	Error() error
    95  	// Close closes the stream
    96  	Close() error
    97  }
    98  
    99  // Option used by the Client
   100  type Option func(*Options)
   101  
   102  // CallOption used by Call or Stream
   103  type CallOption func(*CallOptions)
   104  
   105  // PublishOption used by Publish
   106  type PublishOption func(*PublishOptions)
   107  
   108  // MessageOption used by NewMessage
   109  type MessageOption func(*MessageOptions)
   110  
   111  // RequestOption used by NewRequest
   112  type RequestOption func(*RequestOptions)
   113  
   114  var (
   115  	// DefaultBackoff is the default backoff function for retries
   116  	DefaultBackoff = exponentialBackoff
   117  	// DefaultRetry is the default check-for-retry function for retries
   118  	DefaultRetry = RetryOnConnectFailure
   119  	// DefaultRetries is the default number of times a request is tried
   120  	DefaultRetries = 1
   121  	// DefaultRequestTimeout is the default request timeout
   122  	DefaultRequestTimeout = time.Second * 10
   123  	// DefaultPoolSize sets the connection pool size
   124  	DefaultPoolSize = 100
   125  	// DefaultPoolTTL sets the connection pool ttl
   126  	DefaultPoolTTL = time.Minute
   127  )
   128  
   129  // NewMessage returns a message which can be published
   130  func NewMessage(topic string, msg interface{}) Message {
   131  	return DefaultClient.NewMessage(topic, msg)
   132  }
   133  
   134  // NewRequest returns a request can which be executed using Call or Stream
   135  func NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request {
   136  	return DefaultClient.NewRequest(service, endpoint, req, opts...)
   137  }
   138  
   139  // Call performs a request
   140  func Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error {
   141  	return DefaultClient.Call(ctx, req, rsp, opts...)
   142  }
   143  
   144  // Publish a message
   145  func Publish(ctx context.Context, msg Message) error {
   146  	return DefaultClient.Publish(ctx, msg)
   147  }