Documentation
¶
Index ¶
- Constants
- Variables
- type ListMongoLockIterator
- type MongoConnectionManager
- type MongoLock
- type MongoStorage
- func (x *MongoStorage) Capabilities() []storage.StorageCapability
- func (x *MongoStorage) Close(ctx context.Context) error
- func (x *MongoStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, ...) error
- func (x *MongoStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, ...) error
- func (x *MongoStorage) Get(ctx context.Context, lockId string) (string, error)
- func (x *MongoStorage) GetName() string
- func (x *MongoStorage) GetTime(ctx context.Context) (time.Time, error)
- func (x *MongoStorage) Init(ctx context.Context) error
- func (x *MongoStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
- func (x *MongoStorage) UpdateWithVersion(ctx context.Context, lockId string, ...) error
- func (x *MongoStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
- type MongoStorageOptions
- func (x *MongoStorageOptions) Check() error
- func (x *MongoStorageOptions) SetCollectionName(collectionName string) *MongoStorageOptions
- func (x *MongoStorageOptions) SetConnectionManager(connectionProvider storage.ConnectionManager[*mongo.Client]) *MongoStorageOptions
- func (x *MongoStorageOptions) SetDatabaseName(databaseName string) *MongoStorageOptions
- func (x *MongoStorageOptions) SetTimeProvider(timeProvider storage.TimeProvider) *MongoStorageOptions
Constants ¶
const MongoConnectionManagerName = "mongodb-connection-manager"
const MongoStorageName = "mongodb-storage"
Variables ¶
var ( // ErrDatabaseNameEmpty 参数中数据库名字为空 ErrDatabaseNameEmpty = fmt.Errorf("DatabaseName can not empty") // ErrCollectionNameEmpty 参数中集合名字为空 ErrCollectionNameEmpty = fmt.Errorf("CollectionName can not empty") // ErrConnectionManagerNil 连接管理器没有指定 ErrConnectionManagerNil = fmt.Errorf("ConnectionManager can not nil") // ErrMongoNotInit 存储尚未初始化(GetTime 需要客户端连接) ErrMongoNotInit = fmt.Errorf("mongodb storage is not initialized") ErrMongoServerTimeUnavailable = fmt.Errorf("mongodb server time is unavailable") )
Functions ¶
This section is empty.
Types ¶
type ListMongoLockIterator ¶
type ListMongoLockIterator struct {
// contains filtered or unexported fields
}
ListMongoLockIterator 用于迭代列出mongo中的所有的锁
func NewListMongoLockIterator ¶
func NewListMongoLockIterator(cursor *mongo.Cursor) *ListMongoLockIterator
func (*ListMongoLockIterator) Next ¶
func (x *ListMongoLockIterator) Next() bool
func (*ListMongoLockIterator) Value ¶
func (x *ListMongoLockIterator) Value() *storage.LockInformation
Value 返回当前游标指向的锁信息。
MongoDB 中锁记录以 MongoLock(bson 字段:_id/owner_id/version/lock_json_string)存储, 真正的 LockInformation(包含 LockId/LockCount/ReadLock 等字段)被序列化保存在 lock_json_string 字段里。之前直接把 bson 文档 Decode 到 storage.LockInformation (它只有 json tag,没有 bson tag)会导致除 OwnerId/Version 外的字段全部零值, ReadLock/LockCount/LeaseExpireTime 等信息在 List 结果中全部丢失。 因此这里先 decode 到 MongoLock,再从 lock_json_string 解析出真正的 LockInformation。
type MongoConnectionManager ¶
type MongoConnectionManager struct {
// 连接到数据库的地址
URI string
// contains filtered or unexported fields
}
MongoConnectionManager 负责维护与Mongo数据库的连接
func NewMongoConnectionManagerFromClient ¶
func NewMongoConnectionManagerFromClient(client *mongo.Client) *MongoConnectionManager
NewMongoConnectionManagerFromClient 复用已经存在的mongo client,从其创建连接管理器
func NewMongoConnectionManagerFromURI ¶
func NewMongoConnectionManagerFromURI(uri string) *MongoConnectionManager
NewMongoConnectionManagerFromURI 从Mongo uri创建连接管理器
func (*MongoConnectionManager) Name ¶
func (x *MongoConnectionManager) Name() string
func (*MongoConnectionManager) Shutdown ¶
func (x *MongoConnectionManager) Shutdown(ctx context.Context) error
type MongoLock ¶
type MongoLock struct {
// 锁的ID,这个字段是一个唯一字段,这个字段会作为Mongo中的Collection的主键字段,保证同一个锁同时只会存在一个
ID string `bson:"_id"`
// 锁的当前持有者的ID
OwnerId string `bson:"owner_id"`
// 锁的版本,每次修改都会增加1
Version storage.Version `bson:"version"`
// 锁的json信息,存储着更上层的通用的锁的信息,这里只需要认为它是一个字符串就可以了
LockJsonString string `bson:"lock_json_string"`
}
MongoLock 锁在Mongo中存储的结构
type MongoStorage ¶
type MongoStorage struct {
// contains filtered or unexported fields
}
MongoStorage MongoDB的存储引擎实现
func NewMongoStorage ¶
func NewMongoStorage(ctx context.Context, options *MongoStorageOptions) (*MongoStorage, error)
NewMongoStorage 创建一个基于MongoDB的存储引擎
func (*MongoStorage) Capabilities ¶
func (x *MongoStorage) Capabilities() []storage.StorageCapability
Capabilities 声明 MongoDB 存储支持的能力 MongoDB 通过唯一索引 + 条件更新支持 CAS, 通过 NTP 时间源提供可靠时间,且支持 Change Streams 作为 Watch 能力
func (*MongoStorage) CreateWithVersion ¶
func (x *MongoStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error
func (*MongoStorage) DeleteWithVersion ¶
func (x *MongoStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error
func (*MongoStorage) GetName ¶
func (x *MongoStorage) GetName() string
func (*MongoStorage) List ¶
func (x *MongoStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
func (*MongoStorage) UpdateWithVersion ¶
func (x *MongoStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error
func (*MongoStorage) Watch ¶
func (x *MongoStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
type MongoStorageOptions ¶
type MongoStorageOptions struct {
// 获取连接
ConnectionManager storage.ConnectionManager[*mongo.Client]
// 要存储到的数据库的名称
DatabaseName string
// 集合名称
CollectionName string
// TimeProvider 可选,自定义时间源。MongoDB 本身没有便捷的方式拿到服务器时间,
// 默认使用公共 NTP(见 GetTime),但公共 NTP 依赖外网可达性,延迟也不稳定;
// 内网部署或测试场景可以通过这里注入自定义实现(比如指向内网 NTP,或直接用
// 本地时钟)来避免这个依赖。为空则回退到默认的 NTP 实现。
TimeProvider storage.TimeProvider
}
MongoStorageOptions Mongo的存储选项
func NewMongoStorageOptions ¶
func NewMongoStorageOptions() *MongoStorageOptions
func NewMongoStorageOptionsWithURI ¶
func NewMongoStorageOptionsWithURI(uri string) *MongoStorageOptions
func (*MongoStorageOptions) Check ¶
func (x *MongoStorageOptions) Check() error
func (*MongoStorageOptions) SetCollectionName ¶
func (x *MongoStorageOptions) SetCollectionName(collectionName string) *MongoStorageOptions
func (*MongoStorageOptions) SetConnectionManager ¶
func (x *MongoStorageOptions) SetConnectionManager(connectionProvider storage.ConnectionManager[*mongo.Client]) *MongoStorageOptions
func (*MongoStorageOptions) SetDatabaseName ¶
func (x *MongoStorageOptions) SetDatabaseName(databaseName string) *MongoStorageOptions
func (*MongoStorageOptions) SetTimeProvider ¶
func (x *MongoStorageOptions) SetTimeProvider(timeProvider storage.TimeProvider) *MongoStorageOptions
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
create_mongo_storage_from_client
command
|
|
|
create_mongodb_storage_from_uri
command
|