feat: add lock
							parent
							
								
									9e0756f984
								
							
						
					
					
						commit
						a095e14a71
					
				|  | @ -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") | 	lock, err := etcdlock.New(cnf, "http://127.0.0.1:2379", 3) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -206,7 +206,7 @@ func send() error { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	time.Sleep(time.Second * 120) | 	time.Sleep(time.Second * 120000) | ||||||
| 
 | 
 | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,13 +1,17 @@ | ||||||
| package etcd | package etcd | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/RichardKnop/machinery/v2/config" | 	"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" | ||||||
|  | 	"go.etcd.io/etcd/client/v3/concurrency" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | @ -19,10 +23,9 @@ type etcdLock struct { | ||||||
| 	conf       clientv3.Config | 	conf       clientv3.Config | ||||||
| 	cli        *clientv3.Client | 	cli        *clientv3.Client | ||||||
| 	retries    int | 	retries    int | ||||||
| 	interval   time.Duration |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func New(cnf *config.Config, endpoint string) (iface.Lock, error) { | func New(cnf *config.Config, endpoint string, retries int) (iface.Lock, error) { | ||||||
| 	etcdConf := clientv3.Config{Endpoints: []string{endpoint}} | 	etcdConf := clientv3.Config{Endpoints: []string{endpoint}} | ||||||
| 	cli, err := clientv3.New(etcdConf) | 	cli, err := clientv3.New(etcdConf) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -33,6 +36,7 @@ func New(cnf *config.Config, endpoint string) (iface.Lock, error) { | ||||||
| 		globalConf: cnf, | 		globalConf: cnf, | ||||||
| 		conf:       etcdConf, | 		conf:       etcdConf, | ||||||
| 		cli:        cli, | 		cli:        cli, | ||||||
|  | 		retries:    retries, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return &lock, nil | 	return &lock, nil | ||||||
|  | @ -40,13 +44,46 @@ func New(cnf *config.Config, endpoint string) (iface.Lock, error) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error { | func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error { | ||||||
| 	err := errors.New("LockWithRetries not implemented") | 	for i := 0; i < r.retries; i++ { | ||||||
| 	log.ERROR.Print(err) | 		err := r.Lock(key, unixTsToExpireNs) | ||||||
| 	return err | 		if err == nil { | ||||||
|  | 			// 成功拿到锁,返回
 | ||||||
|  | 			return nil | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		log.INFO.Printf("acquired lock=%s failed, retries=%d", key, i) | ||||||
|  | 		time.Sleep(time.Millisecond * 100) | ||||||
|  | 	} | ||||||
|  | 	return ErrRedisLockFailed | ||||||
|  | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error { | func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error { | ||||||
| 	err := errors.New("Lock not implemented") | 	now := time.Now().UnixNano() | ||||||
| 	log.ERROR.Print(err) | 	ttl := time.Duration(unixTsToExpireNs + 1 - now) | ||||||
| 	return err | 	lease := clientv3.NewLease(r.cli) | ||||||
|  | 
 | ||||||
|  | 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) | ||||||
|  | 	defer cancel() | ||||||
|  | 
 | ||||||
|  | 	resp, err := lease.Grant(ctx, int64(ttl.Seconds())) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// 创建一个新的session
 | ||||||
|  | 	s, err := concurrency.NewSession(r.cli, concurrency.WithLease(resp.ID)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	key = fmt.Sprintf("/machinery/v2/lock/%s", strings.TrimRight(key, "/")) | ||||||
|  | 	m := concurrency.NewMutex(s, key) | ||||||
|  | 
 | ||||||
|  | 	if err := m.Lock(ctx); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	log.INFO.Printf("acquired lock=%s, duration=%s", key, ttl) | ||||||
|  | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue