add backend
parent
46efefa31d
commit
0fff77ab9b
|
@ -20,7 +20,7 @@ var (
|
|||
)
|
||||
|
||||
type etcdLock struct {
|
||||
conf *config.Config
|
||||
ctx context.Context
|
||||
client *clientv3.Client
|
||||
retries int
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ func New(ctx context.Context, conf *config.Config, retries int) (iface.Lock, err
|
|||
}
|
||||
|
||||
lock := etcdLock{
|
||||
conf: conf,
|
||||
ctx: ctx,
|
||||
client: client,
|
||||
retries: retries,
|
||||
}
|
||||
|
@ -48,10 +48,10 @@ func New(ctx context.Context, conf *config.Config, retries int) (iface.Lock, err
|
|||
}
|
||||
|
||||
// LockWithRetries ..
|
||||
func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
|
||||
func (l *etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
|
||||
i := 0
|
||||
for ; i < r.retries; i++ {
|
||||
err := r.Lock(key, unixTsToExpireNs)
|
||||
for ; i < l.retries; i++ {
|
||||
err := l.Lock(key, unixTsToExpireNs)
|
||||
if err == nil {
|
||||
// 成功拿到锁,返回
|
||||
return nil
|
||||
|
@ -66,12 +66,12 @@ func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
|
|||
}
|
||||
|
||||
// Lock ..
|
||||
func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error {
|
||||
func (l *etcdLock) Lock(key string, unixTsToExpireNs int64) error {
|
||||
now := time.Now().UnixNano()
|
||||
ttl := time.Duration(unixTsToExpireNs + 1 - now)
|
||||
|
||||
// 创建一个新的session
|
||||
s, err := concurrency.NewSession(r.client, concurrency.WithTTL(int(ttl.Seconds())))
|
||||
s, err := concurrency.NewSession(l.client, concurrency.WithTTL(int(ttl.Seconds())))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error {
|
|||
lockKey := fmt.Sprintf("/machinery/v2/lock/%s", strings.TrimRight(key, "/"))
|
||||
m := concurrency.NewMutex(s, lockKey)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
|
||||
ctx, cancel := context.WithTimeout(l.ctx, time.Second*2)
|
||||
defer cancel()
|
||||
|
||||
if err := m.Lock(ctx); err != nil {
|
||||
|
|
Loading…
Reference in New Issue