client

package
v0.0.0-...-c31ea8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2016 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Req

func Req(ctx context.Context, service, endpoint string, req, res interface{}) error

Req sends a synchronous request to a service using a new client, and unmarshals the response into the supplied protobuf

func SetDefaultMiddleware

func SetDefaultMiddleware(middleware []ClientMiddleware)

Types

type Call

type Call struct {
	// Uid represents a unique identifier for this call within the scope of a client.
	Uid string
	// Service to receive the call.
	Service string
	// Endpoint of the receiving service.
	Endpoint string
	// Body will be serialised to form the Payload of the request.
	Body interface{}
	// Headers to send on the request (these may be augmented by the client).
	Headers map[string]string
	// Response is a protocol into which the response's Payload should be unmarshaled.
	Response interface{}
	// Context is a context for the request. This should nearly always be the parent request (if any).
	Context context.Context
}

A Call is a convenient way to form a Request for an RPC call.

func (Call) Request

func (c Call) Request() (mercury.Request, error)

Request yields a Request formed from this Call

type Client

type Client interface {
	// Add a Call to the internal request set.
	Add(Call) Client
	// Add a Request to the internal request set (requests added this way will not benefit from automatic body
	// unmarshaling).
	AddRequest(uid string, req mercury.Request) Client
	// SetTimeout sets a timeout within which all requests must be received. Any response not received within this
	// window will result in an error being added to the error set.
	SetTimeout(timeout time.Duration) Client
	// Go fires off the requests. It does not wait until the requests have completed to return.
	Go() Client
	// Wait blocks until all requests have finished executing.
	Wait() Client
	// Execute fires off all requests and waits until all requests have completed before returning.
	Execute() Client
	// SetTransport configures a Transport to use with this Client. By default, it uses the default transport.
	SetTransport(t transport.Transport) Client

	// WaitC returns a channel which will be closed when all requests have finished.
	WaitC() <-chan struct{}
	// Errors returns an ErrorSet of all errors generated during execution (if any).
	Errors() ErrorSet
	// Response retrieves the Response for the request given by its uid. If no such uid is known, returns nil.
	Response(uid string) mercury.Response

	// Middleware returns the ClientMiddleware stack currently installed. This is not a copy, so it's advisable not to
	// f*ck around with it.
	//
	// Client middleware is used to act upon or transform an RPC request or its response. Middleware is applied in order
	// during the request phase, and in reverse order during the response phase.
	//
	// Beware: client middleware can cause the timeouts to be exceeded. They must be fast, and certainly should not
	// make any remote calls themselves.
	Middleware() []ClientMiddleware
	// SetMiddleware replaces the client's Client stack.
	SetMiddleware([]ClientMiddleware) Client
	// AddMiddleware appends the given ClientMiddleware to the stack.
	AddMiddleware(ClientMiddleware) Client
}

A Client is a convenient way to make Requests (potentially in parallel) and access their Responses/Errors.

func NewClient

func NewClient() Client

type ClientMiddleware

type ClientMiddleware interface {
	// ProcessClientRequest is called on every outbound request, before it is sent to a transport.
	//
	// The middleware may mutate the request, or by returning nil, prevent the request from being sent entirely.
	ProcessClientRequest(req mercury.Request) mercury.Request
	// ProcessClientResponse is called on responses before they are available to the caller. If a call fails, or
	// returns an error, ProcessClientError is invoked instead of this method for that request.
	//
	// Note that response middleware are applied in reverse order.
	ProcessClientResponse(rsp mercury.Response, req mercury.Request) mercury.Response
	// ProcessClientError is called whenever a remote call results in an error (either local or remote).
	//
	// Note that error middleware are applied in reverse order.
	ProcessClientError(err *terrors.Error, req mercury.Request)
}

type ErrorSet

type ErrorSet []*terrors.Error

func (ErrorSet) Any

func (es ErrorSet) Any() bool

Any returns whether there are any contained errors

func (ErrorSet) Combined

func (es ErrorSet) Combined() error

Combined returns a combined error from the set. If there is only one error, it is returned unmolested. If there are more, they are all "flattened" into a single error. Where codes differ, they are normalised to that with the lowest index.

func (ErrorSet) Copy

func (es ErrorSet) Copy() ErrorSet

Copy returns a new ErrorSet containing the same errors as the receiver

func (ErrorSet) Error

func (es ErrorSet) Error() string

Error satisfies Go's Error interface

func (ErrorSet) Errors

func (es ErrorSet) Errors() map[string]*terrors.Error

Errors returns a map of request uids to their error, for requests which had errors

func (ErrorSet) ForUid

func (es ErrorSet) ForUid(uid string) *terrors.Error

ForUid returns the error for a given request uid (or nil)

func (ErrorSet) IgnoreCode

func (es ErrorSet) IgnoreCode(codes ...string) ErrorSet

IgnoreCode returns a new ErrorSet without errors of the given codes

func (ErrorSet) IgnoreEndpoint

func (es ErrorSet) IgnoreEndpoint(service, endpoint string) ErrorSet

IgnoreEndpoint returns a new ErrorSet without errors from the given service endpoint

func (ErrorSet) IgnoreService

func (es ErrorSet) IgnoreService(services ...string) ErrorSet

IgnoreService returns a new ErrorSet without errors from the given service(s)

func (ErrorSet) IgnoreUid

func (es ErrorSet) IgnoreUid(uids ...string) ErrorSet

IgnoreUid returns a new ErrorSet without errors from the given request uid(s)

func (ErrorSet) String

func (es ErrorSet) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL