升级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 下午
package easylock
// EasyLock ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:18 下午 2021/4/1
type EasyLock interface {
Lock(flag string) error
Unlock(flag string) error
RLock(flag string) error
RUnlock(flag string) error
// Lock ...
Lock(optionFuncList ...OptionFunc) error
// Unlock ...
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
}
func (l *lock) Lock(flag string) error {
func (l *lock) Lock(optionFuncList ...OptionFunc) error {
l.sc.Lock()
return nil
}
func (l *lock) Unlock(flag string) error {
func (l *lock) Unlock(optionFuncList ...OptionFunc) error {
l.sc.Unlock()
return nil
}
func (l *lock) RLock(flag string) error {
func (l *lock) RLock(optionFuncList ...OptionFunc) error {
l.sc.RLock()
return nil
}
func (l *lock) RUnlock(flag string) error {
func (l *lock) RUnlock(optionFuncList ...OptionFunc) error {
l.sc.Unlock()
return nil
}

View File

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