multiclient

package
v0.0.0-...-cbea63e Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2021 License: Apache-2.0 Imports: 19 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(scope Scoper, service string, endpoint string, request proto.Message, response proto.Message) error

func ExplicitScoper

func ExplicitScoper() *explicitScoper

ExplicitScoper mints a Scoper that we can define explicitly things like SessionId and TraceId

func SetCaller

func SetCaller(c Caller)

SetCaller is provided for tests to use to swap out the default calling mechanism for an alternative (eg: stubbed caller)

Types

type Caller

type Caller func(req *client.Request, rsp proto.Message) errors.Error

func ConfiguredHttpCaller

func ConfiguredHttpCaller(opts Options) Caller

ConfiguredHttpCaller with more explicit configuration options than simple HttpCaller

func ErrorCaller

func ErrorCaller(err errors.Error) Caller

ErrorCaller is a very simple caller that just returns an error If no error provided, defaults to a `NotFound` error with code "errorcaller.notfound"

func HttpCaller

func HttpCaller(baseUrl string) Caller

HttpCaller returns a caller that hits a "thin API" by HTTP, where baseUrl is provided and is something like 'https://api-staging.elasticride.com'

func PlatformCaller

func PlatformCaller() Caller

PlatformCaller is the default caller and makes requests via the platform layer RPC mechanism (eg: RabbitMQ)

type Errors

type Errors interface {
	error

	// IgnoreUid removes all errors for the given request uid(s)
	IgnoreUid(uids ...string) Errors
	// IgnoreService removes all errors for requests to the given service(s)
	IgnoreService(services ...string) Errors
	// IgnoreEndpoint removes all errors to a given service endpoint
	IgnoreEndpoint(service, endpoint string) Errors
	// IgnoreType removes all errors of the given type(s)
	IgnoreType(types ...string) Errors
	// IgnoreCode removes all errors with the given code(s)
	IgnoreCode(codes ...string) Errors
	// Errors returns all matching errors, mapped to their request uid
	Errors() map[string]errors.Error
	// Count returns a count of matching errors
	Count() int
	// AnyErrors returns whether there are any contained errors
	AnyErrors() bool
	// ForUid returns the error for a given request uid (or nil)
	ForUid(uid string) errors.Error
	// Suffix sets a dotted suffix to add to any returned errors
	Suffix(suffix string) Errors
	// Combined returns a platform error with the given suffix, scoped correctly (or nil)
	Combined() errors.Error
	// MultiError returns a combined MultiError
	MultiError() *multierror.MultiError
}

type Mock

type Mock struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewMock

func NewMock() *Mock

func (*Mock) Caller

func (m *Mock) Caller() Caller

Caller returns something that implements `Caller` - allowing us to use this as our gateway to service calls - the returned `Caller` is thread safe

func (*Mock) On

func (m *Mock) On(service, endpoint string) *Stub

func (*Mock) Proxy

func (m *Mock) Proxy(p Caller) *Mock

Proxy adds a default proxy to this mock, which will be used as the caller if no stubs are matched (if none provided, then the default will be to return an error) - thread safe

func (*Mock) Stub

func (m *Mock) Stub(s *Stub) *Mock

Stub adds a stubbed endpoint to this mock - thread safe

type MultiClient

type MultiClient interface {
	// DefaultScopeFrom defines a server request that we can leverage for scoping, meaning that any
	// `ScopedReq`s added don't have to include the From scope
	DefaultScopeFrom(s Scoper) MultiClient
	// SetCaller optionally defines a caller for use with this multiclient request (overrides the default)
	SetCaller(c Caller) MultiClient
	// AddScopedReq adds a server-scoped request (from the server request `from`) to our multi-client
	// with the `uid` that uniquely identifies the request within the group (for getting response from `Outcome`)
	AddScopedReq(sr *ScopedReq) MultiClient
	// Reset removes all scoped requests/results ready for re-use
	Reset() MultiClient
	// Execute runs all requests in parallel, blocking until all have completed
	Execute() MultiClient
	// AnyErrors will return true if ANY request resulted in an error
	AnyErrors() bool
	// AnyErrorsIgnoring will return true if ANY request resulted in an error, but ignoring the supplied types/codes
	AnyErrorsIgnoring(types []string, codes []string) bool
	// PlatformError mints a new platform error, with the specified code suffix (and server namespace prefix),
	// describing all errors that occurred
	PlatformError(codeSuffix string) errors.Error
	// Errors returns Errors representing all errors that occurred
	Errors() Errors
	// Succeeded tests if request with this uid had an error
	Succeeded(uid string) errors.Error
	// Response returns the response for request uid
	Response(uid string) proto.Message
}

MultiClient represents a session where we want to make one or more requests

func New

func New() MultiClient

New mints a new default MultiClient

type Options

type Options struct {
	BaseUrl               string
	TlsSkipVerify         bool
	ConnectTimeout        time.Duration
	RequestTimeout        time.Duration
	ResponseHeaderTimeout time.Duration
}

type Responder

type Responder func(invocation int, req *client.Request) (proto.Message, errors.Error)

Responder is a function that yields a mocked response (successful in the form of proto.Message or unsuccessful in the form of errors.Error), in response to some inbound client `req`. The `invocation` parameter will contain the call sequence number, indexed from 1, for example it will be `1` the first time we are asked for a response, `2` on the next invocation etc..

type ScopedReq

type ScopedReq struct {
	Uid      string
	From     Scoper
	Service  string
	Endpoint string
	Req      proto.Message
	Rsp      proto.Message
	Options  client.Options
}

ScopedReq describes a scoped request

type Scoper

type Scoper interface {
	// Name returns the scope name, for example the server name, or scoping request name,
	// which can be used to give context to error messages etc.
	Context() string
	// ScopedRequest yields a new RPC request, preserving scope of trace/auth etc.
	ScopedRequest(service, endpoint string, payload proto.Message) (*client.Request, error)
}

Scoper represents something that generate us a "scoped" request A "scoped" request includes some context, either taken from the "server" (who we are) or an inbound request

type Stub

type Stub struct {
	Service, Endpoint string

	Response  proto.Message // Response to return on every matched request
	Error     errors.Error  // Error to return on every matched request
	Responder Responder     // Responder function to use to give more control over exact response
	// contains filtered or unexported fields
}

func (*Stub) CountCalls

func (s *Stub) CountCalls() int

CountCalls returns the number of calls that this stub has handled

func (*Stub) Fail

func (s *Stub) Fail(err errors.Error) *Stub

func (*Stub) Once

func (s *Stub) Once() *Stub

func (*Stub) Payload

func (s *Stub) Payload(payload proto.Message) *Stub

func (*Stub) Request

func (s *Stub) Request(n int) *client.Request

Request returns the request object of call n, where n is zero-indexed, or nil if there was none (eg: what request caused this stub to be triggered)

func (*Stub) Return

func (s *Stub) Return(response proto.Message) *Stub

func (*Stub) Times

func (s *Stub) Times(limit int) *Stub

Jump to

Keyboard shortcuts

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