pkg: simplify code - use time.Until and time.Since

Change-Id: I9c023bd4bb312e05bfbd6b1e33425500bbc5aada
This commit is contained in:
Paul Lindner 2017-12-03 17:13:40 -08:00
parent 4f4da7aa21
commit 680b296c2e
2 changed files with 7 additions and 3 deletions

View File

@ -99,10 +99,14 @@ func WaitForBlob(storage interface{}, deadline time.Time, blobs []blob.Ref) {
if !canLongPoll { if !canLongPoll {
tc = time.After(2 * time.Second) tc = time.After(2 * time.Second)
} }
t := time.NewTimer(time.Until(deadline))
defer t.Stop()
select { select {
case <-ch: case <-ch:
case <-tc: case <-tc:
case <-time.After(deadline.Sub(time.Now())): case <-t.C:
} }
} }

View File

@ -243,7 +243,7 @@ type apiPost struct {
} }
func (r *run) importBatch(authToken string, parent *importer.Object) (keepTrying bool, err error) { func (r *run) importBatch(authToken string, parent *importer.Object) (keepTrying bool, err error) {
sleepDuration := r.nextAfter.Sub(time.Now()) sleepDuration := time.Until(r.nextAfter)
// block until we either get canceled or until it is time to run // block until we either get canceled or until it is time to run
select { select {
case <-r.Context().Done(): case <-r.Context().Done():
@ -307,7 +307,7 @@ func (r *run) importBatch(authToken string, parent *importer.Object) (keepTrying
}) })
} }
log.Printf("pinboard: Imported batch of %d posts in %s.", postCount, time.Now().Sub(start)) log.Printf("pinboard: Imported batch of %d posts in %s.", postCount, time.Since(start))
r.nextCursor = postBatch[postCount-1].Time r.nextCursor = postBatch[postCount-1].Time
r.lastPause = pauseInterval r.lastPause = pauseInterval