diff --git a/easylock/abstruct.go b/easylock/abstruct.go index 34ca973..bb48ddf 100644 --- a/easylock/abstruct.go +++ b/easylock/abstruct.go @@ -8,8 +8,8 @@ package easylock type EasyLock interface { - Lock() error - Unlock() error - RLock() error - RUnlock() error + Lock(flag string) error + Unlock(flag string) error + RLock(flag string) error + RUnlock(flag string) error } diff --git a/easylock/error.go b/easylock/error.go new file mode 100644 index 0000000..854c147 --- /dev/null +++ b/easylock/error.go @@ -0,0 +1,19 @@ +// Package easylock... +// +// Description : easylock... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-24 10:55 下午 +package easylock + +import "github.com/pkg/errors" + +// segmentError ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 1:44 下午 2021/2/24 +func segmentError() error { + return errors.New("segment需要大于0") +} diff --git a/easylock/lock.go b/easylock/lock.go index 9d22c0a..2ea0d6c 100644 --- a/easylock/lock.go +++ b/easylock/lock.go @@ -9,6 +9,11 @@ package easylock import "sync" +// NewLock获取普通锁实例,因为只有一把锁,flag没有意义,传空即可 +// +// Author : go_developer@163.com<张德满> +// +// Date : 10:42 下午 2021/2/24 func NewLock() EasyLock { return &lock{ sc: &sync.RWMutex{}, @@ -19,22 +24,22 @@ type lock struct { sc *sync.RWMutex } -func (l *lock) Lock() error { +func (l *lock) Lock(flag string) error { l.sc.Lock() return nil } -func (l *lock) Unlock() error { +func (l *lock) Unlock(flag string) error { l.sc.Unlock() return nil } -func (l *lock) RLock() error { +func (l *lock) RLock(flag string) error { l.sc.RLock() return nil } -func (l *lock) RUnlock() error { +func (l *lock) RUnlock(flag string) error { l.sc.Unlock() return nil } diff --git a/easylock/lock_segment.go b/easylock/lock_segment.go new file mode 100644 index 0000000..afa7f51 --- /dev/null +++ b/easylock/lock_segment.go @@ -0,0 +1,50 @@ +// Package easylock... +// +// Description : 分段的锁 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-24 10:44 下午 +package easylock + +import "github.com/go-developer/gopkg/util" + +// NewSegment 获取分段锁 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:20 下午 2021/2/24 +func NewSegment(segmentCnt int) (EasyLock, error) { + if segmentCnt <= 0 { + return nil, segmentError() + } + s := &segment{ + lockTable: make([]EasyLock, segmentCnt), + segmentCnt: segmentCnt, + } + for i := 0; i < segmentCnt; i++ { + s.lockTable[i] = NewLock() + } + return s, nil +} + +type segment struct { + lockTable []EasyLock + segmentCnt int +} + +func (s *segment) Lock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Lock(flag) +} + +func (s *segment) Unlock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].Unlock(flag) +} + +func (s *segment) RLock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RLock(flag) +} + +func (s *segment) RUnlock(flag string) error { + return s.lockTable[util.GetHashIDMod(flag, s.segmentCnt)].RUnlock(flag) +}