Skip to content

Commit

Permalink
CON-11819 spread sync of load balancers (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
llDrLove authored Feb 14, 2025
1 parent 904caab commit ff62883
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions cloud-controller-manager/do/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package do
import (
"context"
"fmt"
"math/rand"
"net/http"
"time"

Expand Down Expand Up @@ -71,12 +72,12 @@ func newResources(clusterID, clusterVPCID string, publicAccessFW publicAccessFir
}

type syncer interface {
Sync(name string, period time.Duration, stopCh <-chan struct{}, fn func() error)
Sync(name string, period time.Duration, initialDelay time.Duration, stopCh <-chan struct{}, fn func() error)
}

type tickerSyncer struct{}

func (s *tickerSyncer) Sync(name string, period time.Duration, stopCh <-chan struct{}, fn func() error) {
func (s *tickerSyncer) Sync(name string, period time.Duration, initialDelay time.Duration, stopCh <-chan struct{}, fn func() error) {
ticker := time.NewTicker(period)
defer ticker.Stop()

Expand All @@ -85,6 +86,12 @@ func (s *tickerSyncer) Sync(name string, period time.Duration, stopCh <-chan str
klog.Errorf("%s failed: %s", name, err)
}

select {
case <-time.After(initialDelay):
case <-stopCh:
return
}

for {
select {
case <-ticker.C:
Expand Down Expand Up @@ -126,7 +133,7 @@ func (r *ResourcesController) Run(stopCh <-chan struct{}) {
klog.Info("No cluster ID configured -- skipping cluster dependent syncers.")
return
}
go r.syncer.Sync("tags syncer", controllerSyncTagsPeriod, stopCh, r.syncTags)
go r.syncer.Sync("tags syncer", controllerSyncTagsPeriod, time.Second*time.Duration(rand.Int31n(600)), stopCh, r.syncTags)
}

// syncTags synchronizes tags. Currently, this is only needed to associate
Expand Down
4 changes: 2 additions & 2 deletions cloud-controller-manager/do/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func newRecordingSyncer(stopOn int, stopCh chan struct{}) *recordingSyncer {
}
}

func (s *recordingSyncer) Sync(name string, period time.Duration, stopCh <-chan struct{}, fn func() error) {
func (s *recordingSyncer) Sync(name string, period time.Duration, initialDelay time.Duration, stopCh <-chan struct{}, fn func() error) {
recordingFn := func() error {
s.mutex.Lock()
defer s.mutex.Unlock()
Expand All @@ -139,7 +139,7 @@ func (s *recordingSyncer) Sync(name string, period time.Duration, stopCh <-chan
return fn()
}

s.tickerSyncer.Sync(name, period, stopCh, recordingFn)
s.tickerSyncer.Sync(name, period, initialDelay, stopCh, recordingFn)
}

var (
Expand Down

0 comments on commit ff62883

Please sign in to comment.