feat: add lock
parent
13675c3e86
commit
0749384c84
|
@ -80,7 +80,7 @@ func startServer() (*machinery.Server, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// lock := redislock.New(cnf, []string{"localhost:6379"}, 3, 2)
|
// lock := redislock.New(cnf, []string{"localhost:6379"}, 3, 2)
|
||||||
lock, err := etcdlock.New(cnf, "http://127.0.0.1:2379", 3)
|
lock, err := etcdlock.New(context.Background(), "http://127.0.0.1:2379", 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/RichardKnop/machinery/v2/config"
|
|
||||||
"github.com/RichardKnop/machinery/v2/locks/iface"
|
"github.com/RichardKnop/machinery/v2/locks/iface"
|
||||||
"github.com/RichardKnop/machinery/v2/log"
|
"github.com/RichardKnop/machinery/v2/log"
|
||||||
clientv3 "go.etcd.io/etcd/client/v3"
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
@ -15,49 +14,55 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrRedisLockFailed = errors.New("redis lock: failed to acquire lock")
|
// ErrLockFailed ..
|
||||||
|
ErrLockFailed = errors.New("etcd lock: failed to acquire lock")
|
||||||
)
|
)
|
||||||
|
|
||||||
type etcdLock struct {
|
type etcdLock struct {
|
||||||
globalConf *config.Config
|
cli *clientv3.Client
|
||||||
conf clientv3.Config
|
retries int
|
||||||
cli *clientv3.Client
|
|
||||||
retries int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cnf *config.Config, endpoint string, retries int) (iface.Lock, error) {
|
// New ..
|
||||||
etcdConf := clientv3.Config{Endpoints: []string{endpoint}}
|
func New(ctx context.Context, endpoint string, retries int) (iface.Lock, error) {
|
||||||
|
etcdConf := clientv3.Config{
|
||||||
|
Endpoints: []string{endpoint},
|
||||||
|
Context: ctx,
|
||||||
|
DialTimeout: time.Second * 5,
|
||||||
|
}
|
||||||
|
|
||||||
cli, err := clientv3.New(etcdConf)
|
cli, err := clientv3.New(etcdConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lock := etcdLock{
|
lock := etcdLock{
|
||||||
globalConf: cnf,
|
cli: cli,
|
||||||
conf: etcdConf,
|
retries: retries,
|
||||||
cli: cli,
|
|
||||||
retries: retries,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &lock, nil
|
return &lock, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LockWithRetries ..
|
||||||
func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
|
func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
|
||||||
for i := 0; i < r.retries; i++ {
|
i := 0
|
||||||
|
for ; i < r.retries; i++ {
|
||||||
err := r.Lock(key, unixTsToExpireNs)
|
err := r.Lock(key, unixTsToExpireNs)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// 成功拿到锁,返回
|
// 成功拿到锁,返回
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.INFO.Printf("acquired lock=%s failed, retries=%d, err=%s", key, i, err)
|
log.DEBUG.Printf("acquired lock=%s failed, retries=%d, err=%s", key, i, err)
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ErrRedisLockFailed
|
log.INFO.Printf("acquired lock=%s failed, retries=%d", key, i)
|
||||||
|
return ErrLockFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lock ..
|
||||||
func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error {
|
func (r 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)
|
||||||
|
|
Loading…
Reference in New Issue