queue

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NatsStorageFile   = "file"   // 文件存储(持久化,生产默认)
	NatsStorageMemory = "memory" // 内存存储(易失,低延迟场景)
)

JetStream 存储后端(对应官方 nats.StorageType)

View Source
const (
	NatsRetentionLimits    = "limits"    // 基于限制保留(MaxAge/MaxBytes 约束)
	NatsRetentionInterest  = "interest"  // 基于兴趣保留(全部消费者确认后删除)
	NatsRetentionWorkQueue = "workqueue" // 工作队列(单个消费者确认即删除)
)

JetStream 保留策略(对应官方 nats.RetentionPolicy)

Variables

This section is empty.

Functions

This section is empty.

Types

type Mqtt

type Mqtt struct {
	Endpoint             string `mapstructure:"endpoint" yaml:"endpoint" json:"endpoint"        validate:"required,url"`                           // Mqtt 代理服务器端点地址
	ClientID             string `mapstructure:"client-id" yaml:"client-id" json:"clientId"`                                                        // 客户端标识符
	ProtocolVersion      uint   `mapstructure:"protocol-version" yaml:"protocol-version" json:"protocolVersion" validate:"required"`               // Mqtt 协议版本号,4 是 3.1.1,3 是 3.1
	KeepAlive            int    `mapstructure:"keep-alive" yaml:"keep-alive" json:"keepAlive"      validate:"required,min=1"`                      // 保活时间间隔,最小值为 1 秒
	MaxReconnectInterval int    `mapstructure:"max-reconnect-interval" yaml:"max-reconnect-interval" json:"maxReconnectInterval" validate:"min=1"` // 最大连接间隔时间,单位:秒,最小值为 1 秒
	PingTimeout          int    `mapstructure:"ping-timeout" yaml:"ping-timeout" json:"pingTimeout"      validate:"min=1"`                         // ping 超时时间,单位:秒,最小值为 1 秒
	WriteTimeout         int    `mapstructure:"write-timeout" yaml:"write-timeout" json:"writeTimeout"     validate:"min=1"`                       // 写超时时间,单位:秒,最小值为 1 秒
	ConnectTimeout       int    `mapstructure:"connect-timeout" yaml:"connect-timeout" json:"connectTimeout"   validate:"min=1"`                   // 连接超时时间,单位:秒,最小值为 1 秒
	Username             string `mapstructure:"username" yaml:"username" json:"username"`                                                          // 连接到代理服务器的用户名
	Password             string `mapstructure:"password" yaml:"password" json:"password"`                                                          // 密码
	CleanSession         bool   `mapstructure:"clean-session" yaml:"clean-session" json:"cleanSession"`                                            // 设置客户端掉线服务端是否清除 session
	AutoReconnect        bool   `mapstructure:"auto-reconnect" yaml:"auto-reconnect" json:"autoReconnect"`                                         // 断开后是否重新连接
	WillTopic            string `mapstructure:"will-topic" yaml:"will-topic" json:"willTopic"`                                                     // 遗言发送的 topic
	ModuleName           string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`                                                  // 模块名称
}

Mqtt 结构体用于配置 MQTT 相关参数

func Default

func Default() *Mqtt

Default 返回默认Mqtt配置的指针,支持链式调用

func DefaultMqtt

func DefaultMqtt() Mqtt

DefaultMqtt 返回默认Mqtt配置

func NewMqtt

func NewMqtt(opt *Mqtt) *Mqtt

NewMqtt 创建一个新的 Mqtt 实例

func (*Mqtt) Clone

func (m *Mqtt) Clone() internal.Configurable

Clone 返回 Mqtt 配置的副本

func (*Mqtt) Get

func (m *Mqtt) Get() interface{}

Get 返回 Mqtt 配置的所有字段

func (*Mqtt) Set

func (m *Mqtt) Set(data interface{})

Set 更新 Mqtt 配置的字段

func (*Mqtt) Validate

func (m *Mqtt) Validate() error

Validate 验证 Mqtt 配置的有效性

func (*Mqtt) WithAutoReconnect

func (m *Mqtt) WithAutoReconnect(autoReconnect bool) *Mqtt

WithAutoReconnect 设置是否自动重连

func (*Mqtt) WithCleanSession

func (m *Mqtt) WithCleanSession(cleanSession bool) *Mqtt

WithCleanSession 设置是否清除会话

func (*Mqtt) WithClientID

func (m *Mqtt) WithClientID(clientID string) *Mqtt

WithClientID 设置客户端标识符

func (*Mqtt) WithConnectTimeout

func (m *Mqtt) WithConnectTimeout(connectTimeout int) *Mqtt

WithConnectTimeout 设置连接超时时间

func (*Mqtt) WithEndpoint

func (m *Mqtt) WithEndpoint(endpoint string) *Mqtt

WithEndpoint 设置MQTT代理服务器端点地址

func (*Mqtt) WithKeepAlive

func (m *Mqtt) WithKeepAlive(keepAlive int) *Mqtt

WithKeepAlive 设置保活时间间隔

func (*Mqtt) WithMaxReconnectInterval

func (m *Mqtt) WithMaxReconnectInterval(maxReconnectInterval int) *Mqtt

WithMaxReconnectInterval 设置最大重连间隔

func (*Mqtt) WithModuleName

func (m *Mqtt) WithModuleName(moduleName string) *Mqtt

WithModuleName 设置模块名称

func (*Mqtt) WithPassword

func (m *Mqtt) WithPassword(password string) *Mqtt

WithPassword 设置密码

func (*Mqtt) WithPingTimeout

func (m *Mqtt) WithPingTimeout(pingTimeout int) *Mqtt

WithPingTimeout 设置ping超时时间

func (*Mqtt) WithProtocolVersion

func (m *Mqtt) WithProtocolVersion(protocolVersion uint) *Mqtt

WithProtocolVersion 设置MQTT协议版本

func (*Mqtt) WithUsername

func (m *Mqtt) WithUsername(username string) *Mqtt

WithUsername 设置用户名

func (*Mqtt) WithWillTopic

func (m *Mqtt) WithWillTopic(willTopic string) *Mqtt

WithWillTopic 设置遗言主题

func (*Mqtt) WithWriteTimeout

func (m *Mqtt) WithWriteTimeout(writeTimeout int) *Mqtt

WithWriteTimeout 设置写超时时间

type Nats

type Nats struct {
	Enabled  bool   `mapstructure:"enabled" yaml:"enabled" json:"enabled"`         // 是否启用
	URL      string `mapstructure:"url" yaml:"url" json:"url" validate:"required"` // NATS 服务器地址,如 nats://127.0.0.1:4222
	Name     string `mapstructure:"name" yaml:"name" json:"name"`                  // 客户端名称,用于服务端识别
	Username string `mapstructure:"username" yaml:"username" json:"username"`      // 用户名(可选)
	Password string `mapstructure:"password" yaml:"password" json:"password"`      // 密码(可选)
	Token    string `mapstructure:"token" yaml:"token" json:"token"`               // Token 鉴权(可选)

	// === 连接级(对齐官方 nats.Options,0 值走官方库默认) ===
	ConnectTimeout   int   `mapstructure:"connect-timeout" yaml:"connect-timeout" json:"connectTimeout" validate:"min=1"` // 连接超时(秒),官方默认 2s
	ReconnectWait    int   `mapstructure:"reconnect-wait" yaml:"reconnect-wait" json:"reconnectWait" validate:"min=1"`    // 重连等待时间(秒),官方默认 2s
	ReconnectJitter  int   `mapstructure:"reconnect-jitter" yaml:"reconnect-jitter" json:"reconnectJitter"`               // 重连抖动上限(秒,0=官方默认 100ms)
	ReconnectBufSize int64 `mapstructure:"reconnect-buf-size" yaml:"reconnect-buf-size" json:"reconnectBufSize"`          // 重连期间发布缓冲区上限(字节,0=官方默认 8MB)
	MaxReconnects    int   `mapstructure:"max-reconnects" yaml:"max-reconnects" json:"maxReconnects"`                     // 最大重连次数,-1 表示无限
	PingInterval     int   `mapstructure:"ping-interval" yaml:"ping-interval" json:"pingInterval"`                        // 心跳 ping 间隔(秒,0=官方默认 120s)
	MaxPingsOut      int   `mapstructure:"max-pings-out" yaml:"max-pings-out" json:"maxPingsOut"`                         // 未回应 ping 上限(0=官方默认 2)
	FlushTimeout     int   `mapstructure:"flush-timeout" yaml:"flush-timeout" json:"flushTimeout"`                        // Flush 等待超时(秒,0=官方默认 10s)
	DrainTimeout     int   `mapstructure:"drain-timeout" yaml:"drain-timeout" json:"drainTimeout"`                        // Drain 排空超时(秒,0=官方默认 30s)

	// === JetStream 流级运维参数(对齐官方 nats.StreamConfig;流名由业务建流时决策) ===
	JetStream        bool   `mapstructure:"jet-stream" yaml:"jet-stream" json:"jetStream"`                      // 是否启用 JetStream 持久化
	Replicas         int    `mapstructure:"replicas" yaml:"replicas" json:"replicas"`                           // 副本数(0=单副本;超出集群规模时建流报错)
	Storage          string `mapstructure:"storage" yaml:"storage" json:"storage"`                              // 存储后端:file|memory(空=file)
	Retention        string `mapstructure:"retention" yaml:"retention" json:"retention"`                        // 保留策略:limits|interest|workqueue(空=limits)
	MaxAge           int64  `mapstructure:"max-age" yaml:"max-age" json:"maxAge"`                               // 消息最大保留时长(秒,0=不限)
	MaxBytes         int64  `mapstructure:"max-bytes" yaml:"max-bytes" json:"maxBytes"`                         // 流最大体积(字节,0=不限)
	DuplicatesWindow int64  `mapstructure:"duplicates-window" yaml:"duplicates-window" json:"duplicatesWindow"` // Publish 去重窗口(秒,0=官方默认 120s)

	ModuleName      string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`                  // 模块名称
	WorkerPoolSize  int    `mapstructure:"worker-pool-size" yaml:"worker-pool-size" json:"workerPoolSize"`    // 全局消费者 WorkerPool 大小(0 表示不初始化)
	WorkerQueueSize int    `mapstructure:"worker-queue-size" yaml:"worker-queue-size" json:"workerQueueSize"` // 全局消费者 WorkerPool 任务队列大小(0 表示不初始化)
}

Nats 结构体用于配置 NATS 相关参数

NATS 是一款高性能、轻量级的云原生消息系统,常用于:

  • 分布式服务间低延迟消息广播
  • 事件驱动架构中的发布/订阅
  • 启用 JetStream 后支持消息持久化、重放

func DefaultNats

func DefaultNats() Nats

DefaultNats 返回默认 NATS 配置(连接级 0 值项由官方库默认兜底)

func DefaultNatsPtr

func DefaultNatsPtr() *Nats

DefaultNatsPtr 返回默认 NATS 配置的指针,支持链式调用

func NewNats

func NewNats(opt *Nats) *Nats

NewNats 创建一个新的 Nats 实例

func (*Nats) Clone

func (n *Nats) Clone() internal.Configurable

Clone 返回 Nats 配置的副本

func (*Nats) Get

func (n *Nats) Get() interface{}

Get 返回 Nats 配置

func (*Nats) IsEnabled

func (n *Nats) IsEnabled() bool

IsEnabled 检查NATS启用状态

func (*Nats) Set

func (n *Nats) Set(data interface{})

Set 更新 Nats 配置的字段

func (*Nats) Validate

func (n *Nats) Validate() error

Validate 验证 Nats 配置的有效性

func (*Nats) WithConnectTimeout

func (n *Nats) WithConnectTimeout(seconds int) *Nats

WithConnectTimeout 设置连接超时时间(秒)

func (*Nats) WithDrainTimeout

func (n *Nats) WithDrainTimeout(seconds int) *Nats

WithDrainTimeout 设置 Drain 排空超时(秒,0=官方默认)

func (*Nats) WithDuplicatesWindow

func (n *Nats) WithDuplicatesWindow(seconds int64) *Nats

WithDuplicatesWindow 设置 JetStream Publish 去重窗口(秒,0=官方默认)

func (*Nats) WithEnabled

func (n *Nats) WithEnabled(enabled bool) *Nats

WithEnabled 设置是否启用NATS

func (*Nats) WithFlushTimeout

func (n *Nats) WithFlushTimeout(seconds int) *Nats

WithFlushTimeout 设置 Flush 等待超时(秒,0=官方默认)

func (*Nats) WithJetStream

func (n *Nats) WithJetStream(enabled bool) *Nats

WithJetStream 设置是否启用 JetStream

func (*Nats) WithMaxAge

func (n *Nats) WithMaxAge(seconds int64) *Nats

WithMaxAge 设置 JetStream 消息最大保留时长(秒,0=不限)

func (*Nats) WithMaxBytes

func (n *Nats) WithMaxBytes(bytes int64) *Nats

WithMaxBytes 设置 JetStream 流最大体积(字节,0=不限)

func (*Nats) WithMaxPingsOut

func (n *Nats) WithMaxPingsOut(max int) *Nats

WithMaxPingsOut 设置未回应 ping 上限(0=官方默认)

func (*Nats) WithMaxReconnects

func (n *Nats) WithMaxReconnects(max int) *Nats

WithMaxReconnects 设置最大重连次数

func (*Nats) WithModuleName

func (n *Nats) WithModuleName(moduleName string) *Nats

WithModuleName 设置模块名称

func (*Nats) WithName

func (n *Nats) WithName(name string) *Nats

WithName 设置客户端名称

func (*Nats) WithPassword

func (n *Nats) WithPassword(password string) *Nats

WithPassword 设置密码

func (*Nats) WithPingInterval

func (n *Nats) WithPingInterval(seconds int) *Nats

WithPingInterval 设置心跳 ping 间隔(秒,0=官方默认)

func (*Nats) WithReconnectBufSize

func (n *Nats) WithReconnectBufSize(bytes int64) *Nats

WithReconnectBufSize 设置重连期间发布缓冲区上限(字节,0=官方默认)

func (*Nats) WithReconnectJitter

func (n *Nats) WithReconnectJitter(seconds int) *Nats

WithReconnectJitter 设置重连抖动上限(秒,0=官方默认)

func (*Nats) WithReconnectWait

func (n *Nats) WithReconnectWait(seconds int) *Nats

WithReconnectWait 设置重连等待时间(秒)

func (*Nats) WithReplicas

func (n *Nats) WithReplicas(replicas int) *Nats

WithReplicas 设置 JetStream 副本数

func (*Nats) WithRetention

func (n *Nats) WithRetention(retention string) *Nats

WithRetention 设置 JetStream 保留策略(limits|interest|workqueue)

func (*Nats) WithStorage

func (n *Nats) WithStorage(storage string) *Nats

WithStorage 设置 JetStream 存储后端(file|memory)

func (*Nats) WithToken

func (n *Nats) WithToken(token string) *Nats

WithToken 设置 Token

func (*Nats) WithURL

func (n *Nats) WithURL(url string) *Nats

WithURL 设置 NATS 服务器地址

func (*Nats) WithUsername

func (n *Nats) WithUsername(username string) *Nats

WithUsername 设置用户名

func (*Nats) WithWorkerPoolSize

func (n *Nats) WithWorkerPoolSize(size int) *Nats

WithWorkerPoolSize 设置全局消费者 WorkerPool 大小

func (*Nats) WithWorkerQueueSize

func (n *Nats) WithWorkerQueueSize(size int) *Nats

WithWorkerQueueSize 设置全局消费者 WorkerPool 任务队列大小

Jump to

Keyboard shortcuts

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