-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package try_lock | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
type distributedTryLocker struct { | ||
cmd CASCommand | ||
|
||
key string | ||
value string | ||
|
||
option *options | ||
} | ||
|
||
type CASCommand interface { | ||
CAS(key, src, dst string) bool | ||
} | ||
|
||
type Option func(*options) | ||
|
||
func WithScheduleFunc(f func()) Option { | ||
return func(o *options) { | ||
o.schedule = f | ||
} | ||
} | ||
|
||
type options struct { | ||
schedule Schedule | ||
} | ||
|
||
type Schedule func() | ||
|
||
func defaultSchedule() { | ||
time.Sleep(time.Millisecond * time.Duration((rand.Intn(20)+1)*10)) | ||
runtime.Gosched() | ||
} | ||
|
||
func NewDistributedTryLocker(cmd CASCommand, key, value string, opts ...Option) TryMutexLocker { | ||
option := &options{ | ||
schedule: defaultSchedule, | ||
} | ||
for _, opt := range opts { | ||
opt(option) | ||
} | ||
return &distributedTryLocker{cmd: cmd, key: key, value: value, option: option} | ||
|
||
} | ||
|
||
func (r *distributedTryLocker) Unlock() { | ||
r.cmd.CAS(r.key, r.value, "") | ||
} | ||
|
||
func (r *distributedTryLocker) TryLock(duration time.Duration) error { | ||
if r.cmd.CAS(r.key, "", r.value) { | ||
return nil | ||
} | ||
if duration > 0 { | ||
timeoutChan := time.After(duration) | ||
for { | ||
select { | ||
case <-timeoutChan: | ||
return fmt.Errorf("%w: key=%s value=%s", errGetLockTimeOut, r.key, r.value) | ||
default: | ||
if r.cmd.CAS(r.key, "", r.value) { | ||
return nil | ||
} | ||
// 执行一次切换调度 | ||
r.option.schedule() | ||
} | ||
} | ||
} | ||
return fmt.Errorf("%w: key=%s value=%s", errGetLockTimeOut, r.key, r.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package try_lock | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
) | ||
|
||
type TryMutexLocker interface { | ||
Unlock() | ||
TryLock(duration time.Duration) error | ||
} | ||
|
||
type chMutex struct { | ||
ch chan struct{} | ||
} | ||
|
||
func NewChMutex() TryMutexLocker { | ||
return &chMutex{ch: make(chan struct{}, 1)} | ||
} | ||
|
||
var ( | ||
errGetLockTimeOut = errors.New("get lock timeout") | ||
) | ||
|
||
func (c *chMutex) TryLock(duration time.Duration) error { | ||
if duration > 0 { | ||
timeoutChan := time.After(duration) | ||
select { | ||
case <-timeoutChan: | ||
return errGetLockTimeOut | ||
case c.ch <- struct{}{}: | ||
} | ||
} else { | ||
select { | ||
case c.ch <- struct{}{}: | ||
default: | ||
return errGetLockTimeOut | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *chMutex) Unlock() { | ||
<-c.ch | ||
} |