machinery-plugins/locks/etcd/etcd.go

94 lines
1.9 KiB
Go
Raw Normal View History

2024-05-24 14:57:16 +00:00
package etcd
import (
2024-05-24 15:51:49 +00:00
"context"
2024-05-24 14:57:16 +00:00
"errors"
2024-05-24 15:51:49 +00:00
"fmt"
"strings"
2024-05-24 14:57:16 +00:00
"time"
2024-05-24 17:03:13 +00:00
"github.com/RichardKnop/machinery/v2/config"
2024-05-24 14:57:16 +00:00
"github.com/RichardKnop/machinery/v2/locks/iface"
"github.com/RichardKnop/machinery/v2/log"
clientv3 "go.etcd.io/etcd/client/v3"
2024-05-24 15:51:49 +00:00
"go.etcd.io/etcd/client/v3/concurrency"
2024-05-24 14:57:16 +00:00
)
var (
2024-05-24 16:41:28 +00:00
// ErrLockFailed ..
ErrLockFailed = errors.New("etcd lock: failed to acquire lock")
2024-05-24 14:57:16 +00:00
)
type etcdLock struct {
2024-05-24 17:07:18 +00:00
ctx context.Context
2024-05-24 17:03:13 +00:00
client *clientv3.Client
2024-05-24 16:41:28 +00:00
retries int
2024-05-24 14:57:16 +00:00
}
2024-05-24 16:41:28 +00:00
// New ..
2024-05-24 17:03:13 +00:00
func New(ctx context.Context, conf *config.Config, retries int) (iface.Lock, error) {
2024-05-24 16:41:28 +00:00
etcdConf := clientv3.Config{
2024-05-24 17:03:13 +00:00
Endpoints: []string{conf.Lock},
2024-05-24 16:41:28 +00:00
Context: ctx,
DialTimeout: time.Second * 5,
}
2024-05-24 17:03:13 +00:00
client, err := clientv3.New(etcdConf)
2024-05-24 14:57:16 +00:00
if err != nil {
return nil, err
}
lock := etcdLock{
2024-05-24 17:07:18 +00:00
ctx: ctx,
2024-05-24 17:03:13 +00:00
client: client,
2024-05-24 16:41:28 +00:00
retries: retries,
2024-05-24 14:57:16 +00:00
}
return &lock, nil
}
2024-05-24 16:41:28 +00:00
// LockWithRetries ..
2024-05-24 17:07:18 +00:00
func (l *etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
2024-05-24 16:41:28 +00:00
i := 0
2024-05-24 17:07:18 +00:00
for ; i < l.retries; i++ {
err := l.Lock(key, unixTsToExpireNs)
2024-05-24 15:51:49 +00:00
if err == nil {
// 成功拿到锁,返回
return nil
}
2024-05-24 16:41:28 +00:00
log.DEBUG.Printf("acquired lock=%s failed, retries=%d, err=%s", key, i, err)
2024-05-24 15:51:49 +00:00
time.Sleep(time.Millisecond * 100)
}
2024-05-24 16:41:28 +00:00
log.INFO.Printf("acquired lock=%s failed, retries=%d", key, i)
return ErrLockFailed
2024-05-24 14:57:16 +00:00
}
2024-05-24 16:41:28 +00:00
// Lock ..
2024-05-24 17:07:18 +00:00
func (l *etcdLock) Lock(key string, unixTsToExpireNs int64) error {
2024-05-24 15:51:49 +00:00
now := time.Now().UnixNano()
ttl := time.Duration(unixTsToExpireNs + 1 - now)
// 创建一个新的session
2024-05-24 17:07:18 +00:00
s, err := concurrency.NewSession(l.client, concurrency.WithTTL(int(ttl.Seconds())))
2024-05-24 15:51:49 +00:00
if err != nil {
return err
}
2024-05-24 16:32:23 +00:00
defer s.Orphan()
2024-05-24 15:51:49 +00:00
2024-05-24 16:32:23 +00:00
lockKey := fmt.Sprintf("/machinery/v2/lock/%s", strings.TrimRight(key, "/"))
m := concurrency.NewMutex(s, lockKey)
2024-05-24 15:51:49 +00:00
2024-05-24 17:07:18 +00:00
ctx, cancel := context.WithTimeout(l.ctx, time.Second*2)
2024-05-24 16:32:23 +00:00
defer cancel()
2024-05-24 16:32:35 +00:00
2024-05-24 15:51:49 +00:00
if err := m.Lock(ctx); err != nil {
2024-05-24 16:32:23 +00:00
_ = s.Close()
2024-05-24 15:51:49 +00:00
return err
}
log.INFO.Printf("acquired lock=%s, duration=%s", key, ttl)
return nil
2024-05-24 14:57:16 +00:00
}