Documentation
¶
Index ¶
- Constants
- Variables
- type ZookeeperConnectionManager
- func NewZookeeperConnectionManagerFromClient(client *zk.Conn) *ZookeeperConnectionManager
- func NewZookeeperConnectionManagerFromHosts(hosts []string) *ZookeeperConnectionManager
- func NewZookeeperConnectionManagerFromHostsWithTimeout(hosts []string, sessionTimeout time.Duration) *ZookeeperConnectionManager
- type ZookeeperStorage
- func (x *ZookeeperStorage) Capabilities() []storage.StorageCapability
- func (x *ZookeeperStorage) Close(ctx context.Context) error
- func (x *ZookeeperStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, ...) error
- func (x *ZookeeperStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, ...) error
- func (x *ZookeeperStorage) Get(ctx context.Context, lockId string) (string, error)
- func (x *ZookeeperStorage) GetName() string
- func (x *ZookeeperStorage) GetTime(ctx context.Context) (time.Time, error)
- func (x *ZookeeperStorage) Init(ctx context.Context) error
- func (x *ZookeeperStorage) List(ctx context.Context) (go_iterator.Iterator[*storage.LockInformation], error)
- func (x *ZookeeperStorage) UpdateWithVersion(ctx context.Context, lockId string, ...) error
- func (x *ZookeeperStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
- type ZookeeperStorageOptions
- func (x *ZookeeperStorageOptions) Check() error
- func (x *ZookeeperStorageOptions) SetConnectionManager(connectionManager storage.ConnectionManager[*zk.Conn]) *ZookeeperStorageOptions
- func (x *ZookeeperStorageOptions) SetKeyPrefix(keyPrefix string) *ZookeeperStorageOptions
- func (x *ZookeeperStorageOptions) SetTimeProvider(timeProvider storage.TimeProvider) *ZookeeperStorageOptions
Constants ¶
const ZookeeperStorageName = "zookeeper-storage"
Variables ¶
var ( // ErrKeyPrefixEmpty 参数中 KeyPrefix 为空 ErrKeyPrefixEmpty = fmt.Errorf("KeyPrefix can not empty") // ErrConnectionManagerNil 连接管理器没有指定 ErrConnectionManagerNil = fmt.Errorf("ConnectionManager can not nil") )
Functions ¶
This section is empty.
Types ¶
type ZookeeperConnectionManager ¶
type ZookeeperConnectionManager struct {
// Hosts ZooKeeper 服务器地址列表
Hosts []string
// SessionTimeout 会话超时时间
SessionTimeout time.Duration
// contains filtered or unexported fields
}
ZookeeperConnectionManager 管理 ZooKeeper 连接
func NewZookeeperConnectionManagerFromClient ¶
func NewZookeeperConnectionManagerFromClient(client *zk.Conn) *ZookeeperConnectionManager
NewZookeeperConnectionManagerFromClient 从已有客户端创建连接管理器
func NewZookeeperConnectionManagerFromHosts ¶
func NewZookeeperConnectionManagerFromHosts(hosts []string) *ZookeeperConnectionManager
NewZookeeperConnectionManagerFromHosts 从服务器地址创建连接管理器
func NewZookeeperConnectionManagerFromHostsWithTimeout ¶
func NewZookeeperConnectionManagerFromHostsWithTimeout(hosts []string, sessionTimeout time.Duration) *ZookeeperConnectionManager
NewZookeeperConnectionManagerFromHostsWithTimeout 从服务器地址和超时创建连接管理器
func (*ZookeeperConnectionManager) Name ¶
func (x *ZookeeperConnectionManager) Name() string
type ZookeeperStorage ¶
type ZookeeperStorage struct {
// contains filtered or unexported fields
}
ZookeeperStorage 基于 ZooKeeper 的存储引擎实现
ZooKeeper 通过 Create(节点不存在才创建)、Set(version 匹配才更新) 和 Delete(version 匹配才删除)提供原生 CAS 支持。 这是与 Redis Lua、etcd Txn、Memcache Add+CAS 不同的第四种 CAS 范式: 每个写操作都原生支持条件判断,无需组合多个原语。
存储模型:每个锁对应一个 ZooKeeper 持久节点(PERSISTENT), 节点路径 = /<KeyPrefix>/lock/<lockId>, 节点数据 = "<version>\x00<json>"(与 memcache 相同的编码格式)。 ZooKeeper 节点的 dataVersion 即为 CAS version。
func NewZookeeperStorage ¶
func NewZookeeperStorage(ctx context.Context, options *ZookeeperStorageOptions) (*ZookeeperStorage, error)
NewZookeeperStorage 创建一个基于 ZooKeeper 的存储引擎
func (*ZookeeperStorage) Capabilities ¶
func (x *ZookeeperStorage) Capabilities() []storage.StorageCapability
Capabilities 声明 ZooKeeper 存储支持的能力 ZooKeeper 原生支持条件创建(Create 返回 ErrNodeExists)、 条件更新(Set with version)、条件删除(Delete with version), 因此同时声明 CAS、AtomicDelete 能力。 时间源默认使用本地时间(ZooKeeper 无时间 API)。
func (*ZookeeperStorage) Close ¶
func (x *ZookeeperStorage) Close(ctx context.Context) error
Close 关闭存储引擎 连接由 ConnectionManager 管理,Storage 只清空引用
func (*ZookeeperStorage) CreateWithVersion ¶
func (x *ZookeeperStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error
CreateWithVersion 原子创建锁记录 ZooKeeper Create 命令:节点不存在时才创建,否则返回 ErrNodeExists
func (*ZookeeperStorage) DeleteWithVersion ¶
func (x *ZookeeperStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error
DeleteWithVersion 原子删除锁记录 ZooKeeper Delete 命令:version 匹配才删除,否则返回 ErrBadVersion 这是真正的原子条件删除,不需要墓碑降级
func (*ZookeeperStorage) GetName ¶
func (x *ZookeeperStorage) GetName() string
func (*ZookeeperStorage) List ¶
func (x *ZookeeperStorage) List(ctx context.Context) (go_iterator.Iterator[*storage.LockInformation], error)
List 列出当前所有的锁信息 ZooKeeper 支持通过 GetChildren 枚举子节点
func (*ZookeeperStorage) UpdateWithVersion ¶
func (x *ZookeeperStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error
UpdateWithVersion 原子更新锁记录 ZooKeeper Set 命令:version 匹配才更新,否则返回 ErrBadVersion
func (*ZookeeperStorage) Watch ¶
func (x *ZookeeperStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
type ZookeeperStorageOptions ¶
type ZookeeperStorageOptions struct {
// 获取连接
ConnectionManager storage.ConnectionManager[*zk.Conn]
// KeyPrefix ZooKeeper 中路径的前缀,用于隔离不同的锁实例
// 实际的路径格式为:/<KeyPrefix>/lock/<lockId>
KeyPrefix string
// TimeProvider 可选的时间源,默认使用 time.Now()
// ZooKeeper 没有时间 API,默认使用本地时间
// 在需要更可靠时间源的场景,可注入 NTP 时间源
TimeProvider storage.TimeProvider
}
ZookeeperStorageOptions ZooKeeper 的存储选项
func NewZookeeperStorageOptions ¶
func NewZookeeperStorageOptions() *ZookeeperStorageOptions
func NewZookeeperStorageOptionsWithHosts ¶
func NewZookeeperStorageOptionsWithHosts(hosts []string) *ZookeeperStorageOptions
func (*ZookeeperStorageOptions) Check ¶
func (x *ZookeeperStorageOptions) Check() error
func (*ZookeeperStorageOptions) SetConnectionManager ¶
func (x *ZookeeperStorageOptions) SetConnectionManager(connectionManager storage.ConnectionManager[*zk.Conn]) *ZookeeperStorageOptions
func (*ZookeeperStorageOptions) SetKeyPrefix ¶
func (x *ZookeeperStorageOptions) SetKeyPrefix(keyPrefix string) *ZookeeperStorageOptions
func (*ZookeeperStorageOptions) SetTimeProvider ¶
func (x *ZookeeperStorageOptions) SetTimeProvider(timeProvider storage.TimeProvider) *ZookeeperStorageOptions