升级easylock的入参选项

This commit is contained in:
白茶清欢 2021-04-01 16:28:23 +08:00
parent 08a8a8015c
commit 7553846941
4 changed files with 76 additions and 16 deletions

View File

@ -7,9 +7,18 @@
// Date : 2021-02-24 6:29 下午 // Date : 2021-02-24 6:29 下午
package easylock package easylock
// EasyLock ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:18 下午 2021/4/1
type EasyLock interface { type EasyLock interface {
Lock(flag string) error // Lock ...
Unlock(flag string) error Lock(optionFuncList ...OptionFunc) error
RLock(flag string) error // Unlock ...
RUnlock(flag string) error Unlock(optionFuncList ...OptionFunc) error
// RLock ...
RLock(optionFuncList ...OptionFunc) error
// RUnlock ...
RUnlock(optionFuncList ...OptionFunc) error
} }

46
easylock/define.go Normal file
View File

@ -0,0 +1,46 @@
// Package easylock ...
//
// Description : easylock ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-04-01 4:13 下午
package easylock
type option struct {
flag string // 锁的标识
}
// Option 设置option选项
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:15 下午 2021/4/1
type OptionFunc func(o *option)
// WithFlag 设置flag
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:17 下午 2021/4/1
func WithFlag(flag string) OptionFunc {
return func(o *option) {
o.flag = flag
}
}
type base struct {
}
// ParseOption 解析option
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:24 下午 2021/4/1
func (b *base) ParseOption(optionFuncList ...OptionFunc) *option {
o := &option{}
for _, f := range optionFuncList {
f(o)
}
return o
}

View File

@ -24,22 +24,22 @@ type lock struct {
sc *sync.RWMutex sc *sync.RWMutex
} }
func (l *lock) Lock(flag string) error { func (l *lock) Lock(optionFuncList ...OptionFunc) error {
l.sc.Lock() l.sc.Lock()
return nil return nil
} }
func (l *lock) Unlock(flag string) error { func (l *lock) Unlock(optionFuncList ...OptionFunc) error {
l.sc.Unlock() l.sc.Unlock()
return nil return nil
} }
func (l *lock) RLock(flag string) error { func (l *lock) RLock(optionFuncList ...OptionFunc) error {
l.sc.RLock() l.sc.RLock()
return nil return nil
} }
func (l *lock) RUnlock(flag string) error { func (l *lock) RUnlock(optionFuncList ...OptionFunc) error {
l.sc.Unlock() l.sc.Unlock()
return nil return nil
} }

View File

@ -31,20 +31,25 @@ func NewSegment(segmentCnt int) (EasyLock, error) {
type segment struct { type segment struct {
lockTable []EasyLock lockTable []EasyLock
segmentCnt int segmentCnt int
base
} }
func (s *segment) Lock(flag string) error { func (s *segment) Lock(optionFuncList ...OptionFunc) error {
return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Lock(flag) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Lock()
} }
func (s *segment) Unlock(flag string) error { func (s *segment) Unlock(optionFuncList ...OptionFunc) error {
return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Unlock(flag) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].Unlock()
} }
func (s *segment) RLock(flag string) error { func (s *segment) RLock(optionFuncList ...OptionFunc) error {
return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RLock(flag) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RLock()
} }
func (s *segment) RUnlock(flag string) error { func (s *segment) RUnlock(optionFuncList ...OptionFunc) error {
return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RUnlock(flag) o := s.ParseOption(optionFuncList...)
return s.lockTable[util.GetHashIDMod(o.flag, s.segmentCnt)].RUnlock()
} }