|
|
@ -56,7 +56,6 @@ func (e *EtcdLocker) Start(ctx context.Context) error {
|
|
|
|
|
|
|
|
|
|
|
|
func (e *EtcdLocker) runLockLoop(ctx context.Context) {
|
|
|
|
func (e *EtcdLocker) runLockLoop(ctx context.Context) {
|
|
|
|
defer close(e.stoppedCh)
|
|
|
|
defer close(e.stoppedCh)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-e.stopCh:
|
|
|
|
case <-e.stopCh:
|
|
|
@ -75,7 +74,6 @@ func (e *EtcdLocker) runLockLoop(ctx context.Context) {
|
|
|
|
|
|
|
|
|
|
|
|
if acquired {
|
|
|
|
if acquired {
|
|
|
|
e.runKeepAlive(ctx)
|
|
|
|
e.runKeepAlive(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(e.acquireDelay)
|
|
|
|
time.Sleep(e.acquireDelay)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
e.watchLock(ctx)
|
|
|
|
e.watchLock(ctx)
|
|
|
@ -134,12 +132,10 @@ func (e *EtcdLocker) runKeepAlive(ctx context.Context) {
|
|
|
|
atomic.StoreInt32(&e.isLockOwner, 0) // Set to false atomically
|
|
|
|
atomic.StoreInt32(&e.isLockOwner, 0) // Set to false atomically
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.ZInfo(ctx, "Context canceled, releasing lock", "instanceID", e.instanceID)
|
|
|
|
log.ZInfo(ctx, "Context canceled, releasing lock", "instanceID", e.instanceID)
|
|
|
|
e.releaseLock(ctx)
|
|
|
|
e.releaseLock(ctx)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
case <-e.stopCh:
|
|
|
|
case <-e.stopCh:
|
|
|
|
log.ZInfo(ctx, "Stop signal received, releasing lock", "instanceID", e.instanceID)
|
|
|
|
log.ZInfo(ctx, "Stop signal received, releasing lock", "instanceID", e.instanceID)
|
|
|
|
e.releaseLock(ctx)
|
|
|
|
e.releaseLock(ctx)
|
|
|
@ -151,15 +147,12 @@ func (e *EtcdLocker) runKeepAlive(ctx context.Context) {
|
|
|
|
// Watch lock status directly in etcd
|
|
|
|
// Watch lock status directly in etcd
|
|
|
|
func (e *EtcdLocker) watchLock(ctx context.Context) {
|
|
|
|
func (e *EtcdLocker) watchLock(ctx context.Context) {
|
|
|
|
log.ZInfo(ctx, "Starting to watch lock status", "instanceID", e.instanceID)
|
|
|
|
log.ZInfo(ctx, "Starting to watch lock status", "instanceID", e.instanceID)
|
|
|
|
|
|
|
|
|
|
|
|
watchCtx, cancel := context.WithCancel(ctx)
|
|
|
|
watchCtx, cancel := context.WithCancel(ctx)
|
|
|
|
e.watchCancel = cancel
|
|
|
|
e.watchCancel = cancel
|
|
|
|
|
|
|
|
|
|
|
|
defer e.cancelWatch()
|
|
|
|
defer e.cancelWatch()
|
|
|
|
|
|
|
|
|
|
|
|
// Watch for changes to the lock key
|
|
|
|
// Watch for changes to the lock key
|
|
|
|
e.watchCh = e.client.Watch(watchCtx, lockKey)
|
|
|
|
e.watchCh = e.client.Watch(watchCtx, lockKey)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case resp, ok := <-e.watchCh:
|
|
|
|
case resp, ok := <-e.watchCh:
|
|
|
@ -167,7 +160,6 @@ func (e *EtcdLocker) watchLock(ctx context.Context) {
|
|
|
|
log.ZWarn(ctx, "Watch channel closed", nil, "instanceID", e.instanceID)
|
|
|
|
log.ZWarn(ctx, "Watch channel closed", nil, "instanceID", e.instanceID)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, event := range resp.Events {
|
|
|
|
for _, event := range resp.Events {
|
|
|
|
if event.Type == clientv3.EventTypeDelete {
|
|
|
|
if event.Type == clientv3.EventTypeDelete {
|
|
|
|
log.ZInfo(ctx, "Lock released, attempting to acquire", "instanceID", e.instanceID)
|
|
|
|
log.ZInfo(ctx, "Lock released, attempting to acquire", "instanceID", e.instanceID)
|
|
|
@ -191,7 +183,6 @@ func (e *EtcdLocker) releaseLock(ctx context.Context) {
|
|
|
|
leaseID := e.leaseID
|
|
|
|
leaseID := e.leaseID
|
|
|
|
atomic.StoreInt32(&e.isLockOwner, 0)
|
|
|
|
atomic.StoreInt32(&e.isLockOwner, 0)
|
|
|
|
e.leaseID = 0
|
|
|
|
e.leaseID = 0
|
|
|
|
|
|
|
|
|
|
|
|
if leaseID != 0 {
|
|
|
|
if leaseID != 0 {
|
|
|
|
_, err := e.client.Revoke(context.Background(), leaseID)
|
|
|
|
_, err := e.client.Revoke(context.Background(), leaseID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|