event

package module
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 10 Imported by: 4

README

event

event 是 infrago 的模块

包定位

  • 类型:模块
  • 作用:事件模块,负责事件发布、订阅与分发。

主要功能

  • 对上提供统一模块接口
  • 对下通过驱动接口接入具体后端
  • 支持按配置切换驱动实现

快速接入

import _ "github.com/infrago/event"
[event]
driver = "default"

驱动实现接口列表

以下接口由驱动实现(来自模块 driver.go):

Driver
  • Connect(*Instance) (Connection, error)
Connection
  • Open() error
  • Close() error
  • Start() error
  • Stop() error
  • Register(name, group string) error
  • Publish(name string, data []byte) error

全局配置项(所有配置键)

配置段:[event]

  • driver
  • external
  • codec
  • prefix
  • weight
  • setting

说明

  • setting 一般用于向具体驱动透传专用参数
  • 多实例配置请参考模块源码中的 Config/configure 处理逻辑

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEventRunning    = errors.New("event is running")
	ErrEventNotRunning = errors.New("event is not running")
	ErrEventQueueFull  = errors.New("event queue is full")
)
View Source
var (
	ErrInvalidEvent        = errors.New("invalid event name")
	ErrNoConnection        = errors.New("invalid event connection")
	ErrInvalidEventPayload = errors.New("invalid event payload")
)

Functions

func Broadcast added in v0.7.0

func Broadcast(name string, values ...Map) error

func BroadcastTo added in v0.7.0

func BroadcastTo(conn, name string, values ...Map) error

func BroadcastToWithMeta added in v0.27.0

func BroadcastToWithMeta(meta *infra.Meta, conn, name string, values ...Map) error

func BroadcastWithMeta added in v0.27.0

func BroadcastWithMeta(meta *infra.Meta, name string, values ...Map) error

func DurationSetting added in v0.28.0

func DurationSetting(setting Map, key string, def time.Duration) time.Duration

func Int64Setting added in v0.28.0

func Int64Setting(setting Map, key string, def int64) int64

func IntSetting added in v0.28.0

func IntSetting(setting Map, key string, def int) int

func Publish

func Publish(name string, values ...Map) error

func PublishTo

func PublishTo(conn, name string, values ...Map) error

func PublishToWithMeta added in v0.27.0

func PublishToWithMeta(meta *infra.Meta, conn, name string, values ...Map) error

func PublishWithMeta added in v0.27.0

func PublishWithMeta(meta *infra.Meta, name string, values ...Map) error

func StringSetting added in v0.28.0

func StringSetting(setting Map, key, def string) string

Types

type Config

type Config struct {
	Driver       string
	External     bool
	Codec        string
	Weight       int
	Workers      int
	Queue        int
	QueuePolicy  string
	DrainTimeout time.Duration
	Prefix       string
	Setting      Map
}

type Configs

type Configs map[string]Config

type Connection added in v0.7.0

type Connection interface {
	Open() error
	Close() error
	Start() error
	Stop() error

	Register(name, group string) error
	Publish(name string, data []byte) error
}

type Context

type Context struct {
	*infra.Meta

	Name    string
	Config  *Event
	Setting Map

	Value  Map
	Args   Map
	Locals Map
	Body   Any
	// contains filtered or unexported fields
}

func (*Context) Broadcast added in v0.27.0

func (ctx *Context) Broadcast(name string, values ...Map) error

func (*Context) BroadcastTo added in v0.27.0

func (ctx *Context) BroadcastTo(conn, name string, values ...Map) error

func (*Context) Denied

func (ctx *Context) Denied(res Res)

func (*Context) Error added in v0.7.0

func (ctx *Context) Error(res Res)

func (*Context) Failed

func (ctx *Context) Failed(res Res)

func (*Context) Found

func (ctx *Context) Found()

func (*Context) Next

func (ctx *Context) Next()

func (*Context) Publish added in v0.27.0

func (ctx *Context) Publish(name string, values ...Map) error

func (*Context) PublishTo added in v0.27.0

func (ctx *Context) PublishTo(conn, name string, values ...Map) error

func (*Context) Retry added in v0.28.0

func (ctx *Context) Retry(res Res)

type Declare

type Declare struct {
	Alias    []string `json:"alias"`
	Name     string   `json:"name"`
	Desc     string   `json:"desc"`
	Nullable bool     `json:"-"`
	Args     Vars     `json:"args"`
}

type Driver

type Driver interface {
	Connect(*Instance) (Connection, error)
}

type Event

type Event struct {
	Alias    []string `json:"alias"`
	Name     string   `json:"name"`
	Desc     string   `json:"desc"`
	Nullable bool     `json:"-"`
	Args     Vars     `json:"args"`
	Setting  Map      `json:"setting"`

	Action  ctxFunc   `json:"-"`
	Actions []ctxFunc `json:"-"`

	Found  ctxFunc `json:"-"`
	Error  ctxFunc `json:"-"`
	Failed ctxFunc `json:"-"`
	Denied ctxFunc `json:"-"`

	Connect string `json:"connect"`
}

func (Event) RegistryComponent added in v0.7.0

func (Event) RegistryComponent() string

type Events added in v0.7.0

type Events map[string]Event

func (Events) RegistryComponent added in v0.7.0

func (Events) RegistryComponent() string

type Filter

type Filter struct {
	Name     string  `json:"name"`
	Desc     string  `json:"desc"`
	Serve    ctxFunc `json:"-"`
	Request  ctxFunc `json:"-"`
	Execute  ctxFunc `json:"-"`
	Response ctxFunc `json:"-"`
}

type Handler

type Handler struct {
	Name   string  `json:"name"`
	Desc   string  `json:"desc"`
	Found  ctxFunc `json:"-"`
	Error  ctxFunc `json:"-"`
	Failed ctxFunc `json:"-"`
	Denied ctxFunc `json:"-"`
}

type Instance

type Instance struct {
	Name    string
	Config  Config
	Setting base.Map
	// contains filtered or unexported fields
}

func (*Instance) Serve

func (inst *Instance) Serve(name string, data []byte)

func (*Instance) ServeSync added in v0.27.0

func (inst *Instance) ServeSync(name string, data []byte) (ok bool)

func (*Instance) Submit

func (inst *Instance) Submit(next func())

type Module

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

func (*Module) Close added in v0.7.0

func (m *Module) Close()

func (*Module) Config

func (m *Module) Config(global Map)

func (*Module) Open added in v0.7.0

func (m *Module) Open()

func (*Module) Register

func (m *Module) Register(name string, value Any)

func (*Module) RegisterConfig added in v0.7.0

func (m *Module) RegisterConfig(name string, cfg Config)

func (*Module) RegisterConfigs added in v0.7.0

func (m *Module) RegisterConfigs(configs Configs)

func (*Module) RegisterDeclare added in v0.7.0

func (m *Module) RegisterDeclare(name string, cfg Declare)

func (*Module) RegisterDriver added in v0.7.0

func (m *Module) RegisterDriver(name string, driver Driver)

func (*Module) RegisterEvent added in v0.7.0

func (m *Module) RegisterEvent(name string, cfg Event)

func (*Module) RegisterEvents added in v0.7.0

func (m *Module) RegisterEvents(prefix string, events Events)

func (*Module) RegisterFilter added in v0.7.0

func (m *Module) RegisterFilter(name string, cfg Filter)

func (*Module) RegisterHandler added in v0.7.0

func (m *Module) RegisterHandler(name string, cfg Handler)

func (*Module) Setup added in v0.7.0

func (m *Module) Setup()

func (*Module) Start added in v0.7.0

func (m *Module) Start()

func (*Module) Stop added in v0.7.0

func (m *Module) Stop()

type PayloadError added in v0.28.0

type PayloadError struct {
	Event  string
	Result Res
}

func (*PayloadError) Error added in v0.28.0

func (err *PayloadError) Error() string

func (*PayloadError) Unwrap added in v0.28.0

func (err *PayloadError) Unwrap() error

Jump to

Keyboard shortcuts

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