Use timer instead of ticker (#477)
Tickers always tick, so if we tick every 5 seconds and the work that we perform at each tick takes 5 seconds, we end up just looping with no sleep period. Instead this is using a timer to ensure we actually get a full 5 second sleep between loops. We should consider an async API instead of polling the provider like this.
This commit is contained in:
@@ -76,24 +76,25 @@ func (s *Server) Run(ctx context.Context) error {
|
|||||||
|
|
||||||
// providerSyncLoop syncronizes pod states from the provider back to kubernetes
|
// providerSyncLoop syncronizes pod states from the provider back to kubernetes
|
||||||
func (s *Server) providerSyncLoop(ctx context.Context) {
|
func (s *Server) providerSyncLoop(ctx context.Context) {
|
||||||
// TODO(@cpuguy83): Ticker does not seem like the right thing to use here. A
|
const sleepTime = 5 * time.Second
|
||||||
// ticker keeps ticking while we are updating state, which can be a long
|
|
||||||
// operation. This would lead to just a constant re-sync rather than sleeping
|
t := time.NewTimer(sleepTime)
|
||||||
// for 5 seconds between each loop.
|
defer t.Stop()
|
||||||
//
|
|
||||||
// Leaving this note here as fixing is out of scope for my current changeset.
|
|
||||||
tick := time.NewTicker(5 * time.Second)
|
|
||||||
defer tick.Stop()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case <-tick.C:
|
case <-t.C:
|
||||||
|
t.Stop()
|
||||||
|
|
||||||
ctx, span := trace.StartSpan(ctx, "syncActualState")
|
ctx, span := trace.StartSpan(ctx, "syncActualState")
|
||||||
s.updateNode(ctx)
|
s.updateNode(ctx)
|
||||||
s.updatePodStatuses(ctx)
|
s.updatePodStatuses(ctx)
|
||||||
span.End()
|
span.End()
|
||||||
|
|
||||||
|
// restart the timer
|
||||||
|
t.Reset(sleepTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user