diff --git a/ha/lock.go b/ha/lock.go index 9a6b1075..5aab2619 100644 --- a/ha/lock.go +++ b/ha/lock.go @@ -20,7 +20,7 @@ type lock interface { Renew(ctx context.Context) error } -type LockController struct { +type HALockController struct { ID string renewalPeriod time.Duration waitPeriod time.Duration @@ -30,23 +30,24 @@ type LockController struct { lock lock } -func NewHAController(l lock, logger log.Logger, lease time.Duration) *HAController { - logger = logger.Named("lock_controller") +func NewHALockController(l lock, logger log.Logger, lease time.Duration) *HALockController { + ID := uuid.Generate() + logger = logger.Named("ha_mode").With("id", ID) rn := rand.New(rand.NewSource(time.Now().Unix())).Intn(100) - hac := HAController{ + hac := HALockController{ lock: l, logger: logger, renewalPeriod: time.Duration(float64(lease) * renewalFactor), waitPeriod: time.Duration(float64(lease) * waitFactor), - ID: uuid.Generate(), + ID: ID, randomDelay: time.Duration(rn) * time.Millisecond, } return &hac } -func (hc *HAController) Start(ctx context.Context, protectedFunc func(ctx context.Context)) error { +func (hc *HALockController) Start(ctx context.Context, protectedFunc func(ctx context.Context)) error { hc.logger.Named(hc.ID) // To avoid collisions if all the instances start at the same time, wait @@ -97,7 +98,7 @@ func (hc *HAController) Start(ctx context.Context, protectedFunc func(ctx contex } } -func (hc *HAController) maintainLease(ctx context.Context) error { +func (hc *HALockController) maintainLease(ctx context.Context) error { renewTicker := time.NewTicker(hc.renewalPeriod) defer renewTicker.Stop() for { @@ -116,7 +117,7 @@ func (hc *HAController) maintainLease(ctx context.Context) error { } } -func (hc *HAController) wait(ctx context.Context) { +func (hc *HALockController) wait(ctx context.Context) { t := time.NewTimer(hc.randomDelay) defer t.Stop() diff --git a/ha/lock_test.go b/ha/lock_test.go index a8ab20c7..aeb516ff 100644 --- a/ha/lock_test.go +++ b/ha/lock_test.go @@ -14,7 +14,7 @@ import ( "github.com/shoenig/test/must" ) -var lease = 10 * time.Millisecond +var testLease = 10 * time.Millisecond type mockLock struct { locked bool @@ -63,7 +63,7 @@ func (ml *mockLock) Renew(_ context.Context) error { return errors.New("error") } - if time.Since(ml.leaseStartTime) > lease { + if time.Since(ml.leaseStartTime) > testLease { ml.locked = false return errors.New("lease lost") } @@ -104,21 +104,21 @@ func TestAcquireLock_MultipleInstances(t *testing.T) { defer hac1Cancel() // Wait time on hac1 is 0, it should always get the lock. - hac1 := HAController{ + hac1 := HALockController{ ID: "hac1", lock: &l, logger: testlog.HCLogger(t), - renewalPeriod: time.Duration(float64(lease) * renewalFactor), - waitPeriod: time.Duration(float64(lease) * waitFactor), + renewalPeriod: time.Duration(float64(testLease) * renewalFactor), + waitPeriod: time.Duration(float64(testLease) * waitFactor), randomDelay: 0, } - hac2 := HAController{ + hac2 := HALockController{ ID: "hac2", lock: &l, logger: testlog.HCLogger(t), - renewalPeriod: time.Duration(float64(lease) * renewalFactor), - waitPeriod: time.Duration(float64(lease) * waitFactor), + renewalPeriod: time.Duration(float64(testLease) * renewalFactor), + waitPeriod: time.Duration(float64(testLease) * waitFactor), randomDelay: 6 * time.Millisecond, } @@ -195,12 +195,12 @@ func TestAcquireLock_MultipleInstances(t *testing.T) { must.StrContains(t, hac1.ID, s.starterID) // Start a new instance of the service with ha running, initial delay of 1ms - hac3 := HAController{ + hac3 := HALockController{ ID: "hac3", lock: &l, logger: testlog.HCLogger(t), - renewalPeriod: time.Duration(float64(lease) * renewalFactor), - waitPeriod: time.Duration(float64(lease) * waitFactor), + renewalPeriod: time.Duration(float64(testLease) * renewalFactor), + waitPeriod: time.Duration(float64(testLease) * waitFactor), randomDelay: 1 * time.Millisecond, } @@ -280,13 +280,13 @@ func TestFailedRenewal(t *testing.T) { testCtx, testCancel := context.WithCancel(context.Background()) defer testCancel() - // Set the renewal period to 1.5 * lease (15 ms) to force and error. - hac := HAController{ + // Set the renewal period to 1.5 * testLease (15 ms) to force and error. + hac := HALockController{ ID: "hac1", lock: &l, logger: testlog.HCLogger(t), - renewalPeriod: time.Duration(float64(lease) * 1.5), - waitPeriod: time.Duration(float64(lease) * waitFactor), + renewalPeriod: time.Duration(float64(testLease) * 1.5), + waitPeriod: time.Duration(float64(testLease) * waitFactor), randomDelay: 0, }