oplog

package
v1.62.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package oplog 提供异步操作日志队列: 请求/业务动作日志先入内存队列,由后台消费者批量写入 Sink, 不阻塞请求链路(go-admin 内存队列思路)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Time      time.Time     // 操作时间
	UserID    int64         // 操作用户 ID(0 表示未认证)
	UserEmail string        // 操作用户邮箱
	Method    string        // HTTP 方法
	Path      string        // 请求路径
	Status    int           // HTTP 状态码
	Duration  time.Duration // 处理耗时
	RequestID string        // 链路 Request ID
	Action    string        // 业务动作(如 order.create)
	Detail    string        // 业务附加信息
}

Entry 是单条操作日志。

type Queue

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

Queue 是异步操作日志队列。 Enqueue 非阻塞:队列满时丢弃新条目并计数,保证请求链路不被日志拖慢。

func NewQueue

func NewQueue(sink Sink, bufferSize int) *Queue

NewQueue 创建异步队列并启动后台消费者。 bufferSize 非正数时使用默认 1024。

func (*Queue) Close

func (q *Queue) Close(ctx context.Context) error

Close 停止接收新条目并等待消费者排空队列(幂等)。

func (*Queue) Dropped

func (q *Queue) Dropped() int64

Dropped 返回因队列满或关闭而被丢弃的条目数。

func (*Queue) Enqueue

func (q *Queue) Enqueue(entry Entry)

Enqueue 非阻塞入队;队列已满或已关闭时丢弃并计数。

func (*Queue) Failed

func (q *Queue) Failed() int64

Failed 返回写入 Sink 失败的批次条目数。

func (*Queue) Middleware

func (q *Queue) Middleware() iris.Handler

Middleware 返回 Iris 操作日志中间件,把请求信息入队。 注册顺序建议在 Auth 之后(可读取用户身份)。

type Sink

type Sink interface {
	// Write 批量写入条目;返回错误时该批条目被丢弃并计入失败计数。
	Write(ctx context.Context, entries []Entry) error
}

Sink 是操作日志的落库目标;业务方实现(写数据库、ES、文件等)。

Jump to

Keyboard shortcuts

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