Skip to content

Commit

Permalink
cleanup timer a bit (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka committed Oct 5, 2021
1 parent 4bfe399 commit be3f6f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
10 changes: 5 additions & 5 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ func Run(options Options) error {
return url.Hostname()
})

timer := timer.Timer{}
certCacheMiss := 0

timer.Start(5, func() {
timer := timer.NewTimer()
timer.Start(5*time.Second, func() {
endpoint, err := k8s.Endpoint()
if err != nil {
logging.L.Error(err.Error())
Expand Down Expand Up @@ -358,17 +358,17 @@ func Run(options Options) error {

remote, err := urlx.Parse(k8s.Config.Host)
if err != nil {
return err
return fmt.Errorf("parsing host config: %w", err)
}

ca, err := ioutil.ReadFile(k8s.Config.TLSClientConfig.CAFile)
if err != nil {
return err
return fmt.Errorf("reading CA file: %w", err)
}

ph, err := proxyHandler(ca, k8s.Config.BearerToken, remote)
if err != nil {
return err
return fmt.Errorf("setting proxy handler: %w", err)
}

cache := jwkCache{
Expand Down
14 changes: 8 additions & 6 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,23 @@ func Run(options Options) error {
}

// schedule the user and group sync jobs
interval := 30
interval := 30 * time.Second
if options.SyncInterval > 0 {
interval = options.SyncInterval
interval = time.Duration(options.SyncInterval) * time.Second
} else {
envSync := os.Getenv("INFRA_SYNC_INTERVAL_SECONDS")
if envSync != "" {
interval, err = strconv.Atoi(envSync)
envInterval, err := strconv.Atoi(envSync)
if err != nil {
zapLogger.Error("invalid INFRA_SYNC_INTERVAL_SECONDS env: " + err.Error())
} else {
interval = time.Duration(envInterval) * time.Second
}
}
}

// be careful with this sync job, there are Okta rate limits on these requests
syncSourcesTimer := timer.Timer{}
syncSourcesTimer := timer.NewTimer()
defer syncSourcesTimer.Stop()
syncSourcesTimer.Start(interval, func() {
var sources []Source
Expand All @@ -127,9 +129,9 @@ func Run(options Options) error {
})

// schedule destination sync job
syncDestinationsTimer := timer.Timer{}
syncDestinationsTimer := timer.NewTimer()
defer syncDestinationsTimer.Stop()
syncDestinationsTimer.Start(5*60, func() {
syncDestinationsTimer.Start(5*time.Minute, func() {
now := time.Now()

var destinations []Destination
Expand Down
11 changes: 9 additions & 2 deletions internal/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ type Timer struct {
stop chan bool
}

func (t *Timer) Start(interval int, sync func()) {
ticker := time.NewTicker(time.Duration(interval) * time.Second)
func NewTimer() *Timer {
return &Timer{
stop: make(chan bool),
}
}

func (t *Timer) Start(interval time.Duration, sync func()) {
ticker := time.NewTicker(interval)

go sync()

Expand All @@ -26,6 +32,7 @@ func (t *Timer) Start(interval int, sync func()) {
}()
}

// Stop should be called only once. It waits for the sync function to exit before returning
func (t *Timer) Stop() {
t.stop <- true
}

0 comments on commit be3f6f0

Please sign in to comment.