storage

package
v0.0.0-...-36f27ca Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
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 GenSessionID() (string, error)

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 (t *TxnManager) AutoRunWithTxn(ctx context.Context, cli *mongo.Client, cmd func(ctx context.Context) error) error

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

func (t *TxnManager) GetTxnNumber(ctx context.Context, sessionID string) (int64, error)

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的锁

type TxnOption

type TxnOption func(*TxnManager)

func RedisClient

func RedisClient(client redis.Client) TxnOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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