Documentation
¶
Overview ¶
Package event contains an interface as well as helpers for go-orb events.
Index ¶
- Constants
- Variables
- func HandleRequest[TReq any, TResp any](ctx context.Context, handler Handler, topic string, ...)
- func Register(name string, factory ProviderFunc)
- func Request[TResp any, TReq any](ctx context.Context, handler Handler, topic string, req TReq, ...) (*TResp, error)
- type Config
- type ConfigType
- type Handler
- type Option
- type ProviderFunc
- type Req
- type RequestOption
- type RequestOptions
Constants ¶
const ComponentType = "event"
ComponentType is the client component type name.
Variables ¶
var ( // DefaultEventPlugin is the default client implementation to use. DefaultEventPlugin = "natsjs" // DefaultConfigSection is the default config section for the client. DefaultConfigSection = "client" // DefaultContentType is the default content type used to transport data around. DefaultContentType = "application/x-protobuf" // DefaultRequestTimeout is the default request timeout. DefaultRequestTimeout = time.Second * 30 )
var ( // ErrMissingTopic happens whenever the user doesnt give a topic. ErrMissingTopic = orberrors.New(http.StatusInternalServerError, "missing topic") )
Functions ¶
func HandleRequest ¶
func HandleRequest[TReq any, TResp any]( ctx context.Context, handler Handler, topic string, callback func(ctx context.Context, req *TReq) (*TResp, error), )
HandleRequest subscribes to the given topic and handles the requests.
func Register ¶
func Register(name string, factory ProviderFunc)
Register makes a plugin available by the provided name.
func Request ¶
func Request[TResp any, TReq any]( ctx context.Context, handler Handler, topic string, req TReq, opts ...RequestOption, ) (*TResp, error)
Request makes a request with using events, it's a shortcut for NewRequest(...).Request(...) Example:
resp , err := events.Request[FooResponse](context.Background(), eventsHandler, "user.new", fooRequest)
Response will be of type *FooResponse.
Types ¶
type Config ¶
type Config struct {
// Plugin selects the client implementation.
Plugin string `json:"plugin" yaml:"plugin"`
}
Config are the Client options.
type ConfigType ¶
type ConfigType interface {
// contains filtered or unexported methods
}
ConfigType is used in the functional options as type to identify a registry option. It is used over a static *Config type as this way plugins can also easilty set functional options without the complication of contexts, as was done in v4. This is possible because plugins will nest the registry.Config type, and thus inherit the interface that is used to identify the registry config.
type Handler ¶
type Handler interface {
types.Component
// Request runs a REST like call on the given topic.
// This is an internal function, clients MUST use event.Request().
Request(ctx context.Context, req *Req[[]byte, any], opts ...RequestOption) ([]byte, error)
// HandleRequest subscribes to the given topic and handles the requests.
// This is a blocking operation.
// This is an internal function, clients MUST use event.HandleRequest().
HandleRequest(ctx context.Context, topic string, cb func(context.Context, *Req[[]byte, []byte]))
// Clone creates a clone of the handler, this is useful for parallel requests.
Clone() Handler
}
Handler is the interface for events plugins.
type Option ¶
type Option func(ConfigType)
Option is a functional option type for the registry.
func WithClientPlugin ¶
WithClientPlugin set the client implementation to use.
type ProviderFunc ¶
type ProviderFunc func( name types.ServiceName, data types.ConfigData, logger log.Logger, opts ...Option, ) (Handler, error)
ProviderFunc is provider function type used by plugins to create a new client.
type Req ¶
type Req[TReq any, TResp any] struct { Topic string `json:"topic"` ContentType string `json:"contentType"` // The Data of type TReq Data TReq `json:"data" yaml:"data"` // Err is an error that might happened during encoding. Err error // contains filtered or unexported fields }
Req contains all data for a request call.
func NewRequest ¶
NewRequest creates a event for the given topic.
type RequestOption ¶
type RequestOption func(o *RequestOptions)
RequestOption sets attributes on Calloptions.
func WithRequestContentType ¶
func WithRequestContentType(ct string) RequestOption
WithRequestContentType sets the ContentType field to use for transporting the message.
func WithRequestResponseMetadata ¶
func WithRequestResponseMetadata(md map[string]string) RequestOption
WithRequestResponseMetadata will write response Metadata into the given map.
func WithRequestTimeout ¶
func WithRequestTimeout(t time.Duration) RequestOption
WithRequestTimeout sets the timeout for a request.
type RequestOptions ¶
type RequestOptions struct {
// ContentType for transporting the message.
ContentType string
// Metadata contains keys which can be used to query the data, for example a customer id
Metadata map[string]string
// RequestTimeout defines how long to wait for the server to reply on a request.
RequestTimeout time.Duration
}
RequestOptions contains options for a call.
func NewRequestOptions ¶
func NewRequestOptions(opts ...RequestOption) RequestOptions
NewRequestOptions generates new calloptions with defaults.