events

package
v0.0.0-...-6596822 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package events 数据库层事件包: 查询与模型生命周期事件对象、分发器接口与默认总线实现。

Index

Constants

View Source
const (
	StatementPrepared     = "statementPrepared"
	TransactionBeginning  = "transactionBeginning"
	TransactionCommitting = "transactionCommitting"
	TransactionCommitted  = "transactionCommitted"
	TransactionRolledBack = "transactionRolledBack"
	ConnectionEstablished = "connectionEstablished"
)

生命周期事件名。

View Source
const (
	Retrieving    = "retrieving"
	Retrieved     = "retrieved"
	Creating      = "creating"
	Created       = "created"
	Updating      = "updating"
	Updated       = "updated"
	Saving        = "saving"
	Saved         = "saved"
	Deleting      = "deleting"
	Deleted       = "deleted"
	Trashed       = "trashed"
	Restoring     = "restoring"
	Restored      = "restored"
	Replicating   = "replicating"
	ForceDeleting = "forceDeleting"
	ForceDeleted  = "forceDeleted"
)

模型生命周期事件名(与观察者钩子同名)。

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionEstablishedEvent

type ConnectionEstablishedEvent struct {
	ConnectionName string
}

ConnectionEstablishedEvent 首次建连事件(对应 ConnectionEstablished)。

func (ConnectionEstablishedEvent) EventName

func (e ConnectionEstablishedEvent) EventName() string

type Dispatcher

type Dispatcher interface {
	Dispatch(e Event)
}

Dispatcher 事件分发器接口。

type Event

type Event interface {
	EventName() string
}

Event 事件接口。

type EventBus

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

EventBus 默认分发器实现:按事件名注册监听器。在既有精确名注册之上支持:

  • 通配监听:Listen 的 name 形如 "event.*"(尾部 "*"),分发时精确匹配 在前,其后按通配前缀最长优先;
  • 优先级:ListenWithPriority 数字小先执行,同优先级按注册序(稳定);
  • 可中断传播:ListenerUntil + DispatchUntil;
  • 一次性监听:Once。

并发安全(沿用 RWMutex 风格)。

func NewEventBus

func NewEventBus() *EventBus

func (*EventBus) Dispatch

func (b *EventBus) Dispatch(e Event)

Dispatch 分发事件(监听器同步执行:精确匹配在前按优先级/注册序,其后 命中的通配监听按前缀最长优先)。ListenerUntil 监听器在此忽略返回值。

func (*EventBus) DispatchUntil

func (b *EventBus) DispatchUntil(e Event) bool

DispatchUntil 分发事件直至某监听器中断:ListenerUntil 返回 false 时立即 停止传播并返回 false(until/halt 语义);全部执行完毕 返回 true。普通监听器(无返回值)视为继续传播。

func (*EventBus) Listen

func (b *EventBus) Listen(name string, l Listener)

Listen 注册监听器。name 支持 "event.*" 尾部通配模式。

func (*EventBus) ListenUntil

func (b *EventBus) ListenUntil(name string, l ListenerUntil)

ListenUntil 注册可中断监听器(配合 DispatchUntil:返回 false 停止传播)。

func (*EventBus) ListenUntilWithPriority

func (b *EventBus) ListenUntilWithPriority(name string, l ListenerUntil, priority int)

ListenUntilWithPriority 同 ListenWithPriority,但监听器可中断传播 (配合 DispatchUntil)。

func (*EventBus) ListenWithPriority

func (b *EventBus) ListenWithPriority(name string, l Listener, priority int)

ListenWithPriority 带优先级注册监听器:数字小先执行,同优先级按注册序 (稳定);通配模式监听同样适用。

func (*EventBus) Once

func (b *EventBus) Once(name string, l Listener)

Once 注册一次性监听器:首次分发执行一次后失效(并发分发也仅执行一次)。

type Listener

type Listener func(e Event)

Listener 事件监听器。

type ListenerUntil

type ListenerUntil func(e Event) bool

ListenerUntil 可中断监听器:返回 false 时停止后续传播(halt 语义,配合 DispatchUntil 使用;返回 true 继续传播)。

type ModelEvent

type ModelEvent struct {
	Name  string
	Model any
}

ModelEvent 模型生命周期事件对象。

func (ModelEvent) EventName

func (e ModelEvent) EventName() string

EventName 实现 Event 接口。

type QueryExecuted

type QueryExecuted struct {
	Sql            string
	Bindings       []any
	TimeNs         int64
	RawSql         string // 绑定插值后的 SQL(对应 toRawSql)
	ConnectionName string // 连接标识(对应 $connectionName)
	ReadWriteType  string // "read" / "write",读写分离时区分(对应 $readWriteType)
}

QueryExecuted 携带一条已执行查询的信息。

func (QueryExecuted) TimeMs

func (q QueryExecuted) TimeMs() float64

TimeMs 以毫秒返回执行耗时(float 毫秒)。

type StatementPreparedEvent

type StatementPreparedEvent struct {
	Sql            string
	ConnectionName string
}

StatementPreparedEvent 预处理完成事件(对应 StatementPrepared,携带语句与 连接名;不携带 PDOStatement,Go 侧无对应句柄)。

func (StatementPreparedEvent) EventName

func (e StatementPreparedEvent) EventName() string

type TransactionEvent

type TransactionEvent struct {
	Name             string
	ConnectionName   string
	TransactionLevel int
}

TransactionEvent 事务生命周期事件(对应 TransactionBeginning/Committing/ Committed/RolledBack,均携带连接名与事务层数)。

func (TransactionEvent) EventName

func (e TransactionEvent) EventName() string

Jump to

Keyboard shortcuts

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