act

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TerminateDefaultParamCount is the default parameter count for the terminate action.
	TerminateDefaultParamCount = 2

	// LogDefaultKeyCount is the default key count in the metadata for the log action.
	LogDefaultKeyCount = 3

	// These are the keys used to pass the logger and the result to the built-in actions.
	LoggerKey = "__logger__"
	ResultKey = "__result__"
)

Variables

This section is empty.

Functions

func BuiltinActions

func BuiltinActions() map[string]*sdkAct.Action

BuiltinActions returns a map of built-in actions.

func BuiltinPolicies

func BuiltinPolicies() map[string]*sdkAct.Policy

BuiltinPolicies returns a map of built-in policies.

func BuiltinSignals

func BuiltinSignals() map[string]*sdkAct.Signal

BuiltinSignals returns a map of built-in signals.

func Log

func Log(data map[string]any, params ...sdkAct.Parameter) (any, error)

Log is a built-in action that logs the data received from the plugin.

func Passthrough

func Passthrough(map[string]any, ...sdkAct.Parameter) (any, error)

Passthrough is a built-in action that always returns true and no error.

func Terminate

func Terminate(_ map[string]any, params ...sdkAct.Parameter) (any, error)

Terminate is a built-in action that terminates the connection if the terminate signal is true and the policy is set to "stop". The action can optionally receive a result parameter.

func WithLogger

func WithLogger(logger zerolog.Logger) sdkAct.Parameter

WithLogger returns a parameter with the Logger to be used by the action. This is automatically prepended to the parameters when running an action.

func WithResult

func WithResult(result map[string]any) sdkAct.Parameter

WithResult returns a parameter with the result of the plugin hook to be used by the action.

Types

type AsyncActionMessage added in v0.9.6

type AsyncActionMessage struct {
	Output *sdkAct.Output
	Params []sdkAct.Parameter
}

func (*AsyncActionMessage) Decode added in v0.9.6

func (msg *AsyncActionMessage) Decode(data []byte) error

func (*AsyncActionMessage) Encode added in v0.9.6

func (msg *AsyncActionMessage) Encode() ([]byte, error)

Encode marshals the AsyncActionMessage struct to JSON bytes.

type IPublisher added in v0.9.6

type IPublisher interface {
	Publish(ctx context.Context, payload []byte) error
}

type IRegistry

type IRegistry interface {
	Add(policy *sdkAct.Policy)
	Apply(signals []sdkAct.Signal, hook sdkAct.Hook) []*sdkAct.Output
	Run(output *sdkAct.Output, params ...sdkAct.Parameter) (any, *gerr.GatewayDError)
	RunAll(result map[string]any) map[string]any
	ShouldTerminate(result map[string]any) bool
}

type Publisher added in v0.9.6

type Publisher struct {
	Logger      zerolog.Logger
	RedisDB     redis.Cmdable
	ChannelName string
}

func NewPublisher added in v0.9.6

func NewPublisher(publisher Publisher) (*Publisher, error)

func (*Publisher) Publish added in v0.9.6

func (p *Publisher) Publish(ctx context.Context, payload []byte) error

type Registry

type Registry struct {
	Logger zerolog.Logger
	// Timeout for policy evaluation.
	PolicyTimeout time.Duration
	// Default timeout for running actions
	DefaultActionTimeout time.Duration

	// TaskPublisher is the publisher for async actions.
	// if not given, will invoke simple goroutine to run async actions
	TaskPublisher *Publisher

	Signals           map[string]*sdkAct.Signal
	Policies          map[string]*sdkAct.Policy
	Actions           map[string]*sdkAct.Action
	DefaultPolicyName string
	DefaultPolicy     *sdkAct.Policy
	DefaultSignal     *sdkAct.Signal
}

Registry keeps track of all policies and actions.

func NewActRegistry

func NewActRegistry(
	registry Registry,
) *Registry

NewActRegistry creates a new act registry with the specified default policy and timeout and the builtin signals, policies, and actions.

func (*Registry) Add

func (r *Registry) Add(policy *sdkAct.Policy)

Add adds a policy to the registry.

func (*Registry) Apply

func (r *Registry) Apply(signals []sdkAct.Signal, hook sdkAct.Hook) []*sdkAct.Output

Apply applies the signals to the registry and returns the outputs.

func (*Registry) Run

func (r *Registry) Run(
	output *sdkAct.Output, params ...sdkAct.Parameter,
) (any, *gerr.GatewayDError)

Run runs the function associated with the output.MatchedPolicy and returns its result. If the action is synchronous, the result is returned immediately. If the action is asynchronous, the result is nil and the error is ErrAsyncAction, which is a sentinel error to indicate that the action is running asynchronously.

func (*Registry) RunAll added in v0.9.9

func (r *Registry) RunAll(result map[string]any) map[string]any

RunAll run all the actions in the outputs and returns the end result.

func (*Registry) ShouldTerminate added in v0.9.9

func (r *Registry) ShouldTerminate(result map[string]any) bool

ShouldTerminate checks if any of the actions are terminal, indicating that the request should be terminated. This is an optimization to avoid executing the actions' functions unnecessarily. The __terminal__ field is only set when an action intends to terminate the request.

Jump to

Keyboard shortcuts

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