runtime

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Binding

type Binding struct {
	Profile string
}

Binding 描述资源应绑定到哪个 Redis profile。

func NewBinding

func NewBinding(profile string) Binding

NewBinding 创建 profile 绑定值对象。

type Config

type Config struct {
	Host                  string   `json:"host" mapstructure:"host"`
	Port                  int      `json:"port" mapstructure:"port"`
	Addrs                 []string `json:"addrs" mapstructure:"addrs"`
	Username              string   `json:"username" mapstructure:"username"`
	Password              string   `json:"password" mapstructure:"password"`
	Database              int      `json:"database" mapstructure:"database"`
	MaxIdle               int      `json:"max-idle" mapstructure:"max-idle"`
	MaxActive             int      `json:"max-active" mapstructure:"max-active"`
	Timeout               int      `json:"timeout" mapstructure:"timeout"`
	MinIdleConns          int      `json:"min-idle-conns" mapstructure:"min-idle-conns"`
	PoolTimeout           int      `json:"pool-timeout" mapstructure:"pool-timeout"`
	DialTimeout           int      `json:"dial-timeout" mapstructure:"dial-timeout"`
	ReadTimeout           int      `json:"read-timeout" mapstructure:"read-timeout"`
	WriteTimeout          int      `json:"write-timeout" mapstructure:"write-timeout"`
	EnableCluster         bool     `json:"enable-cluster" mapstructure:"enable-cluster"`
	UseSSL                bool     `json:"use-ssl" mapstructure:"use-ssl"`
	SSLInsecureSkipVerify bool     `json:"ssl-insecure-skip-verify" mapstructure:"ssl-insecure-skip-verify"`
}

Config 描述单个 Redis profile 的连接策略。

func CloneConfig

func CloneConfig(cfg *Config) *Config

CloneConfig 克隆一份 Redis 配置。

func MergeConfig

func MergeConfig(base, override *Config) *Config

MergeConfig 使用 override 配置覆盖 base 配置。

type Handle

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

Handle 封装了 Redis 客户端及其解析后的 profile 元数据。

func NewHandle

func NewHandle(profile string, config *Config) *Handle

NewHandle 为指定 profile 创建运行时句柄。

func (*Handle) Client

func (h *Handle) Client() goredis.UniversalClient

Client 返回底层 go-redis 客户端。

func (*Handle) Close

func (h *Handle) Close() error

Close 关闭底层 Redis 客户端。

func (*Handle) Config

func (h *Handle) Config() *Config

Config 返回当前 profile 配置的克隆副本。

func (*Handle) Connect

func (h *Handle) Connect() error

Connect 初始化底层 Redis 客户端。

func (*Handle) HealthCheck

func (h *Handle) HealthCheck(ctx context.Context) error

HealthCheck 检查底层 Redis 客户端健康状态。

func (*Handle) Profile

func (h *Handle) Profile() string

Profile 返回句柄绑定的 profile 名称。

type Option

type Option func(*Registry)

func WithFallbackOnMissing

func WithFallbackOnMissing(enabled bool) Option

WithFallbackOnMissing 控制缺失命名 profile 时是否回退到默认 profile。

func WithProfileObserver

func WithProfileObserver(observer observability.ProfileObserver) Option

WithProfileObserver 注册 profile 状态变化观测器。

type ProfileState

type ProfileState string

ProfileState 表示已配置 profile 的健康状态。

const (
	ProfileStateMissing     ProfileState = "missing"
	ProfileStateAvailable   ProfileState = "available"
	ProfileStateUnavailable ProfileState = "unavailable"
)

type ProfileStatus

type ProfileStatus struct {
	Name        string
	State       ProfileState
	Err         error
	NextRetryAt time.Time
}

ProfileStatus 描述 profile 当前的可用性状态。

type RedisProfile

type RedisProfile struct {
	Name   string
	Config Config
	Status ProfileStatus
}

RedisProfile 描述一个已配置的命名 profile。

type Registry

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

Registry 基于默认 profile 与命名 profile 实现 Runtime 门面。

func New

func New(defaultConfig *Config, profiles map[string]*Config, opts ...Option) *Registry

New 创建带可选默认 profile 与命名 profile 的运行时注册表。

func (*Registry) Bind

func (r *Registry) Bind(name string) (*Handle, error)

Bind 根据绑定策略解析到命名或默认 Redis 句柄。

func (*Registry) ByProfile

func (r *Registry) ByProfile(name string) (*Handle, error)

ByProfile 返回精确匹配的命名 profile 句柄。 缺失或不可用的 profile 会直接返回错误。

func (*Registry) Client

func (r *Registry) Client(name string) (goredis.UniversalClient, error)

Client 返回绑定解析后的 Redis 客户端。

func (*Registry) Close

func (r *Registry) Close() error

Close 关闭所有已初始化的 Redis 连接。

func (*Registry) Connect

func (r *Registry) Connect() error

Connect 初始化默认 profile 与命名 profile。

func (*Registry) Default

func (r *Registry) Default() (*Handle, error)

Default 返回默认 Redis 句柄。

func (*Registry) HasConnections

func (r *Registry) HasConnections() bool

HasConnections 返回运行时是否至少存在一个已配置连接。

func (*Registry) HealthCheck

func (r *Registry) HealthCheck(ctx context.Context) error

HealthCheck 检查已配置 Redis 连接,并在可重试时尝试恢复。

func (*Registry) Profiles

func (r *Registry) Profiles() []RedisProfile

Profiles 返回已配置命名 profile 及其状态快照。

func (*Registry) Status

func (r *Registry) Status(name string) ProfileStatus

Status 返回指定命名 profile 的当前状态。

func (*Registry) Statuses

func (r *Registry) Statuses() map[string]ProfileStatus

Statuses 返回所有已配置命名 profile 的状态快照。

type Runtime

type Runtime interface {
	HasConnections() bool
	Connect() error
	Close() error
	HealthCheck(ctx context.Context) error
	Default() (*Handle, error)
	ByProfile(name string) (*Handle, error)
	Bind(name string) (*Handle, error)
	Client(name string) (goredis.UniversalClient, error)
	Status(name string) ProfileStatus
	Statuses() map[string]ProfileStatus
	Profiles() []RedisProfile
}

Runtime 定义稳定的 Redis 运行时门面接口。

Jump to

Keyboard shortcuts

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