add backend

Browse Source
main
git 2024-05-25 01:07:18 +08:00
parent 46efefa31d
commit 0fff77ab9b
Signed by: git
GPG Key ID: 3F65EFFA44207ADD
1 changed files with 8 additions and 8 deletions

View File

@ -20,7 +20,7 @@ var (
) )
type etcdLock struct { type etcdLock struct {
conf *config.Config ctx context.Context
client *clientv3.Client client *clientv3.Client
retries int retries int
} }
@ -39,7 +39,7 @@ func New(ctx context.Context, conf *config.Config, retries int) (iface.Lock, err
} }
lock := etcdLock{ lock := etcdLock{
conf: conf, ctx: ctx,
client: client, client: client,
retries: retries, retries: retries,
} }
@ -48,10 +48,10 @@ func New(ctx context.Context, conf *config.Config, retries int) (iface.Lock, err
} }
// LockWithRetries .. // LockWithRetries ..
func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error { func (l *etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
i := 0 i := 0
for ; i < r.retries; i++ { for ; i < l.retries; i++ {
err := r.Lock(key, unixTsToExpireNs) err := l.Lock(key, unixTsToExpireNs)
if err == nil { if err == nil {
// 成功拿到锁,返回 // 成功拿到锁,返回
return nil return nil
@ -66,12 +66,12 @@ func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
} }
// Lock .. // Lock ..
func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error { func (l *etcdLock) Lock(key string, unixTsToExpireNs int64) error {
now := time.Now().UnixNano() now := time.Now().UnixNano()
ttl := time.Duration(unixTsToExpireNs + 1 - now) ttl := time.Duration(unixTsToExpireNs + 1 - now)
// 创建一个新的session // 创建一个新的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 { if err != nil {
return err 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, "/")) lockKey := fmt.Sprintf("/machinery/v2/lock/%s", strings.TrimRight(key, "/"))
m := concurrency.NewMutex(s, lockKey) 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() defer cancel()
if err := m.Lock(ctx); err != nil { if err := m.Lock(ctx); err != nil {