event

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 11 Imported by: 6

Documentation

Overview

Package event contains an interface as well as helpers for go-orb events.

Index

Constants

View Source
const ComponentType = "event"

ComponentType is the client component type name.

Variables

View Source
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
)
View Source
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.

func NewConfig

func NewConfig(opts ...Option) Config

NewConfig generates a new config with all the defaults.

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.

func Provide

func Provide(
	name types.ServiceName,
	configs types.ConfigData,
	logger log.Logger,
	opts ...Option) (Handler, error)

Provide creates a new client instance with the implementation from cfg.Plugin.

type Option

type Option func(ConfigType)

Option is a functional option type for the registry.

func WithClientPlugin

func WithClientPlugin(n string) Option

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

func NewRequest[TResp, TReq any](req TReq) *Req[TReq, TResp]

NewRequest creates a event for the given topic.

func (*Req[TReq, TResp]) Request

func (e *Req[TReq, TResp]) Request(ctx context.Context, handler Handler, topic string, opts ...RequestOption) (*TResp, error)

Request runs a REST like call on the events topic.

func (*Req[TReq, TResp]) SetReplyFunc

func (e *Req[TReq, TResp]) SetReplyFunc(h func(ctx context.Context, result TResp, err error))

SetReplyFunc sets the internal reply func (for example nats.Msg) for the client.

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.

Jump to

Keyboard shortcuts

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