Skip to content

Commit

Permalink
updater,pkg/timeutil: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Feb 14, 2019
1 parent 165c397 commit f64bd11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pkg/timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ import (
"math/rand"
"time"

"github.com/coreos/clair/pkg/stopper"
log "github.com/sirupsen/logrus"

"github.com/coreos/clair/pkg/stopper"
)

// ApproxSleep is a stoppable time.Sleep that adds a slight random variation to
// the wakeup time in order to prevent thundering herds.
func ApproxSleep(approxWakeup time.Time, st *stopper.Stopper) (stopped bool) {
waitUntil := approxWakeup.Add(time.Duration(rand.ExpFloat64()/0.5) * time.Second)
log.WithField("wakeup", waitUntil).Debug("updater sleeping")
if !waitUntil.Before(time.Now().UTC()) {
if !st.Sleep(waitUntil.Sub(time.Now())) {
now := time.Now().UTC()
if !waitUntil.Before(now) {
if !st.Sleep(waitUntil.Sub(now)) {
return true
}
}
Expand Down
16 changes: 9 additions & 7 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import (
"sync"
"time"

"github.com/pborman/uuid"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/vulnmdsrc"
"github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/stopper"
"github.com/coreos/clair/pkg/timeutil"
"github.com/pborman/uuid"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -121,7 +122,7 @@ func RunUpdater(config *UpdaterConfig, datastore database.Datastore, st *stopper

// If the next update timer is in the past, then try to update.
if nextUpdate.Before(time.Now().UTC()) {
// Attempt to get a lock on the the update.
// Attempt to get a lock on the update.
log.Debug("attempting to obtain update lock")
acquiredLock, lockExpiration := database.AcquireLock(datastore, updaterLockName, whoAmI, updaterLockDuration, false)
if lockExpiration.IsZero() {
Expand All @@ -134,6 +135,7 @@ func RunUpdater(config *UpdaterConfig, datastore database.Datastore, st *stopper
sleepDuration, err = updateWhileRenewingLock(datastore, whoAmI, isFirstUpdate, st)
if err != nil {
if err == errReceivedStopSignal {
log.Debug("updater received stop signal")
return
}
log.WithError(err).Debug("failed to acquired lock")
Expand Down Expand Up @@ -275,7 +277,7 @@ func setUpdaterDuration(start time.Time) {

// fetchUpdates asynchronously runs all of the enabled Updaters, aggregates
// their results, and appends metadata to the vulnerabilities found.
func fetchUpdates(ctx context.Context, datastore database.Datastore) (status bool, vulns []database.VulnerabilityWithAffected, flags map[string]string, notes []string) {
func fetchUpdates(ctx context.Context, datastore database.Datastore) (success bool, vulns []database.VulnerabilityWithAffected, flags map[string]string, notes []string) {
flags = make(map[string]string)

log.Info("fetching vulnerability updates")
Expand Down Expand Up @@ -316,7 +318,7 @@ func fetchUpdates(ctx context.Context, datastore database.Datastore) (status boo
}

if err := g.Wait(); err == nil {
status = true
success = true
}

vulns = addMetadata(ctx, datastore, vulns)
Expand Down

0 comments on commit f64bd11

Please sign in to comment.