Skip to content

Commit

Permalink
style: rename ha controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Jul 7, 2023
1 parent 5e8b4c5 commit 443a376
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
17 changes: 9 additions & 8 deletions ha/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()

Expand Down
30 changes: 15 additions & 15 deletions ha/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit 443a376

Please sign in to comment.