botstate

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Default StateType = "init"
	Noop    EventType = "noop"
)

Variables

View Source
var (
	ErrEventRejected   = errors.New("event rejected")
	ErrSessionNotFound = errors.New("not found")
	ErrStateNotFound   = errors.New("not found")
)

Functions

This section is empty.

Types

type Action

type Action interface {
	Execute(eventCtx EventContext) EventType
}

type Backend

type Backend interface {
	Get(ctx context.Context, k string) ([]byte, error)
	Set(ctx context.Context, k string, v []byte) error
	Delete(ctx context.Context, k string) error
}

type EventContext

type EventContext interface{}

type EventType

type EventType string

type Events

type Events map[EventType]StateType

type InMemoryBackend

type InMemoryBackend struct{}

func NewInMemoryBackend

func NewInMemoryBackend() *InMemoryBackend

func (InMemoryBackend) Delete

func (i InMemoryBackend) Delete(ctx context.Context, k string) error

func (InMemoryBackend) Get

func (i InMemoryBackend) Get(ctx context.Context, k string) ([]byte, error)

func (InMemoryBackend) Set

func (i InMemoryBackend) Set(ctx context.Context, k string, v []byte) error

type NoopBackend

type NoopBackend struct{}

func NewNoopBackend

func NewNoopBackend() *NoopBackend

func (NoopBackend) Delete

func (n NoopBackend) Delete(_ context.Context, _ string) error

func (NoopBackend) Get

func (n NoopBackend) Get(_ context.Context, _ string) ([]byte, error)

func (NoopBackend) Set

func (n NoopBackend) Set(_ context.Context, _ string, _ []byte) error

type Options

type Options struct{}

type RedisBackend

type RedisBackend struct {
	// contains filtered or unexported fields
}

func NewRedis

func NewRedis(opt RedisConfig) (*RedisBackend, error)

func (RedisBackend) Delete

func (r RedisBackend) Delete(ctx context.Context, k string) error

func (RedisBackend) Get

func (r RedisBackend) Get(ctx context.Context, k string) ([]byte, error)

func (*RedisBackend) Ping

func (r *RedisBackend) Ping() error

func (RedisBackend) Set

func (r RedisBackend) Set(ctx context.Context, k string, v []byte) error

type RedisConfig

type RedisConfig struct {
	Expiration time.Duration
	// The network type, either tcp or unix.
	// Default is tcp.
	Network string
	// host:port address.
	Addr string
	// Dialer creates new network connection and has priority over
	// Network and Addr options.
	Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
	// Use the specified Username to authenticate the current connection
	// with one of the connections defined in the ACL list when connecting
	// to a Redis 6.0 instance, or greater, that is using the Redis ACL system.
	Username string
	// Optional password. Must match the password specified in the
	// requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower),
	// or the User Password when connecting to a Redis 6.0 instance, or greater,
	// that is using the Redis ACL system.
	Password string

	// Database to be selected after connecting to the server.
	DB int

	// Maximum number of retries before giving up.
	// Default is 3 retries; -1 (not 0) disables retries.
	MaxRetries int
	// Minimum backoff between each retry.
	// Default is 8 milliseconds; -1 disables backoff.
	MinRetryBackoff time.Duration
	// Maximum backoff between each retry.
	// Default is 512 milliseconds; -1 disables backoff.
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
	// Default is 3 seconds.
	ReadTimeout time.Duration
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	PoolSize int
	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	MinIdleConns int
	// Connection age at which client retires (closes) the connection.
	// Default is to not close aged connections.
	MaxConnAge time.Duration
	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration
	// Amount of time after which client closes idle connections.
	// Should be less than server's timeout.
	// Default is 5 minutes. -1 disables idle timeout check.
	IdleTimeout time.Duration
	// Frequency of idle checks made by idle connections reaper.
	// Default is 1 minute. -1 disables idle connections reaper,
	// but idle connections are still discarded by the client
	// if IdleTimeout is set.
	IdleCheckFrequency time.Duration
	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config
}

type Session

type Session struct {
	*StateMachine
	// contains filtered or unexported fields
}

func NewSession

func NewSession(identity string, backend Backend, state *StateMachine) *Session

func (*Session) Clean

func (s *Session) Clean(ctx context.Context) error

func (*Session) Flush

func (s *Session) Flush(ctx context.Context) error

Flush save fsm state to backend

func (*Session) Load

func (s *Session) Load(ctx context.Context) error

Load method load session data from backend and current state

type State

type State struct {
	Action Action
	Events Events
}

type StateEncoded

type StateEncoded struct {
	Current  StateType `json:"current"`
	Previous StateType `json:"previous"`
}

type StateMachine

type StateMachine struct {
	// contains filtered or unexported fields
}

func NewStateMachine

func NewStateMachine(states States) *StateMachine

func (*StateMachine) Current

func (s *StateMachine) Current() StateType

func (*StateMachine) Previous

func (s *StateMachine) Previous() StateType

func (*StateMachine) SendEvent

func (s *StateMachine) SendEvent(event EventType, eventCtx EventContext) error

type StateType

type StateType string

type States

type States map[StateType]State

Jump to

Keyboard shortcuts

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