2016-02-22 14:44:12 -05:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gensupport
|
|
|
|
|
2016-10-03 16:49:46 -04:00
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
// BackoffStrategy defines the set of functions that a backoff-er must
|
|
|
|
// implement.
|
2016-02-22 14:44:12 -05:00
|
|
|
type BackoffStrategy interface {
|
2016-10-03 16:49:46 -04:00
|
|
|
// Pause returns the duration of the next pause and true if the operation should be
|
|
|
|
// retried, or false if no further retries should be attempted.
|
|
|
|
Pause() (time.Duration, bool)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
// Reset restores the strategy to its initial state.
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2016-10-03 16:49:46 -04:00
|
|
|
// ExponentialBackoff performs exponential backoff as per https://en.wikipedia.org/wiki/Exponential_backoff.
|
|
|
|
// The initial pause time is given by Base.
|
|
|
|
// Once the total pause time exceeds Max, Pause will indicate no further retries.
|
2016-02-22 14:44:12 -05:00
|
|
|
type ExponentialBackoff struct {
|
2016-10-03 16:49:46 -04:00
|
|
|
Base time.Duration
|
|
|
|
Max time.Duration
|
|
|
|
total time.Duration
|
|
|
|
n uint
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
// Pause returns the amount of time the caller should wait.
|
2016-10-03 16:49:46 -04:00
|
|
|
func (eb *ExponentialBackoff) Pause() (time.Duration, bool) {
|
|
|
|
if eb.total > eb.Max {
|
|
|
|
return 0, false
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 16:49:46 -04:00
|
|
|
// The next pause is selected from randomly from [0, 2^n * Base).
|
|
|
|
d := time.Duration(rand.Int63n((1 << eb.n) * int64(eb.Base)))
|
|
|
|
eb.total += d
|
|
|
|
eb.n++
|
|
|
|
return d, true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
// Reset resets the backoff strategy such that the next Pause call will begin
|
|
|
|
// counting from the start. It is not safe to call concurrently with Pause.
|
2016-02-22 14:44:12 -05:00
|
|
|
func (eb *ExponentialBackoff) Reset() {
|
2016-10-03 16:49:46 -04:00
|
|
|
eb.n = 0
|
|
|
|
eb.total = 0
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|