Documentation ¶
Index ¶
- Constants
- func GenSessionID() (string, error)
- func GenTxnCableAndSetHeader(header http.Header, opts ...metadata.TxnOption) (*metadata.TxnCapable, error)
- type Transaction
- type TxnManager
- func (t *TxnManager) AutoRunWithTxn(ctx context.Context, cli *mongo.Client, cmd func(ctx context.Context) error) error
- func (t *TxnManager) GenTxnNumber(ctx context.Context, sessionID string, ttl time.Duration) (int64, error)
- func (t *TxnManager) GetTxnContext(ctx context.Context, cli *mongo.Client) (context.Context, mongo.Session, bool, error)
- func (t *TxnManager) GetTxnNumber(ctx context.Context, sessionID string) (int64, error)
- func (t *TxnManager) PrepareTransaction(ctx context.Context, cap *metadata.TxnCapable, cli *mongo.Client) (mongo.Session, error)
- func (t *TxnManager) RemoveSessionKey(ctx context.Context, sessionID string) error
- type TxnOption
Constants ¶
const ( TransactionIdHeader = "mon_transaction_id_string" TransactionTimeoutHeader = "mon_transaction_timeout" // TransactionDefaultTimeout mongodb default transaction timeout is 1 minute. TransactionDefaultTimeout = 1 * time.Minute )
transaction related
Variables ¶
This section is empty.
Functions ¶
func GenSessionID ¶
func GenTxnCableAndSetHeader ¶
func GenTxnCableAndSetHeader(header http.Header, opts ...metadata.TxnOption) (*metadata.TxnCapable, error)
GenTxnCableAndSetHeader generate a session id and set it to header.
Types ¶
type Transaction ¶
type Transaction interface { // PrepareTransaction 事务准备 PrepareTransaction(ctx context.Context) (*metadata.TxnCapable, error) // CommitTransaction 提交事务 CommitTransaction(context.Context, *metadata.TxnCapable) error // AbortTransaction 取消事务 AbortTransaction(context.Context, *metadata.TxnCapable) error }
Transaction 基于Redis的分布式事务
type TxnManager ¶
type TxnManager struct {
// contains filtered or unexported fields
}
TxnManager TXN(Transaction)事务管理器
func NewTxnManager ¶
func NewTxnManager(opts ...TxnOption) *TxnManager
func (*TxnManager) AutoRunWithTxn ¶
func (*TxnManager) GenTxnNumber ¶
func (t *TxnManager) GenTxnNumber(ctx context.Context, sessionID string, ttl time.Duration) (int64, error)
GenTxnNumber 根据SessionID生成一个事务ID 使用Redis的SetNX()分布式锁保证同一时间, 同一个SessionID只有一个事务ID
func (*TxnManager) GetTxnContext ¶
func (t *TxnManager) GetTxnContext(ctx context.Context, cli *mongo.Client) (context.Context, mongo.Session, bool, error)
GetTxnContext create a session if the ctx is a session context, and the bool value is true. so the caller must check the bool, and use session only when the bool is true. otherwise the caller should not use the session, should call the mongodb command directly. Note: this function is always used with mongo.CmdbReleaseSession(ctx, sessCtx) to release the session connection.
func (*TxnManager) GetTxnNumber ¶
GetTxnNumber 获取事务号
func (*TxnManager) PrepareTransaction ¶
func (t *TxnManager) PrepareTransaction(ctx context.Context, cap *metadata.TxnCapable, cli *mongo.Client) (mongo.Session, error)
PrepareTransaction 事务准备阶段
func (*TxnManager) RemoveSessionKey ¶
func (t *TxnManager) RemoveSessionKey(ctx context.Context, sessionID string) error
RemoveSessionKey 删除事务ID 同时也是解锁命令, 通过Redis中的Del(key)解锁对key的锁