github.com/wfusion/gofusion@v1.1.14/lock/types.go (about) 1 package lock 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/wfusion/gofusion/common/utils" 8 ) 9 10 const ( 11 ErrDuplicatedName utils.Error = "duplicated lock name" 12 ErrUnsupportedLockType utils.Error = "unsupported lock type" 13 ErrReentrantKeyNotFound utils.Error = "reentrant key for lock not found" 14 ErrTimeout utils.Error = "try to lock timeout" 15 ErrContextDone utils.Error = "try to lock when context done" 16 17 // tolerance Default timeout to prevent deadlock 18 tolerance = 2000 * time.Millisecond 19 ) 20 21 type Lockable interface { 22 Lock(ctx context.Context, key string, opts ...utils.OptionExtender) (err error) 23 Unlock(ctx context.Context, key string, opts ...utils.OptionExtender) (err error) 24 } 25 26 type ReentrantLockable interface { 27 Lockable 28 ReentrantLock(ctx context.Context, key, reentrantKey string, opts ...utils.OptionExtender) (err error) 29 } 30 31 type lockType string 32 33 const ( 34 lockTypeRedisLua lockType = "redis_lua" 35 lockTypeRedisNX lockType = "redis_nx" // not support ReentrantKey 36 lockTypeMySQL lockType = "mysql" // MariaDB versions >= 10.0.2 MySQL versions >= 5.7.5 37 lockTypeMariaDB lockType = "mariadb" // MariaDB versions >= 10.0.2 MySQL versions >= 5.7.5 38 lockTypeMongo lockType = "mongo" // mongo versions >= 3.6 39 ) 40 41 // Conf lock configure 42 type Conf struct { 43 Type lockType `yaml:"type" json:"type" toml:"type"` 44 Instance string `yaml:"instance" json:"instance" toml:"instance"` 45 Scheme string `yaml:"scheme" json:"scheme" toml:"scheme"` 46 } 47 48 type lockOption struct { 49 expired time.Duration // expired After locking, the timeout of the lock 50 reentrantKey string // reentrantKey Reentrant mark 51 } 52 53 func Expire(expired time.Duration) utils.OptionFunc[lockOption] { 54 return func(l *lockOption) { 55 l.expired = expired 56 } 57 } 58 59 func ReentrantKey(key string) utils.OptionFunc[lockOption] { 60 return func(l *lockOption) { 61 l.reentrantKey = key 62 } 63 }