Merge branch 'pr-46'

* pr-46:
  Add configurable timout for HTTP requests
This commit is contained in:
Jess Frazelle 2018-06-06 12:52:47 -04:00
commit 3198679b1e
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
3 changed files with 14 additions and 3 deletions

View file

@ -27,7 +27,7 @@ func Log(format string, args ...interface{}) {
}
// New creates a new Clair struct with the given URL and credentials.
func New(url string, debug bool) (*Clair, error) {
func New(url string, debug bool, timeout time.Duration) (*Clair, error) {
transport := http.DefaultTransport
errorTransport := &ErrorTransport{
@ -43,7 +43,7 @@ func New(url string, debug bool) (*Clair, error) {
registry := &Clair{
URL: url,
Client: &http.Client{
Timeout: 5 * time.Minute,
Timeout: timeout,
Transport: errorTransport,
},
Logf: logf,

View file

@ -53,6 +53,11 @@ func main() {
Value: repoutils.DefaultDockerRegistry,
EnvVar: "REG_REGISTRY",
},
cli.StringFlag{
Name: "timeout",
Value: "1m",
Usage: "timeout for HTTP requests",
},
cli.BoolFlag{
Name: "skip-ping",
Usage: "skip pinging the registry while establishing connection",

View file

@ -115,9 +115,15 @@ func main() {
logrus.Fatal(err)
}
// parse the timeout
timeout, err := time.ParseDuration(c.GlobalString("timeout"))
if err != nil {
logrus.Fatalf("parsing %s as duration failed: %v", c.GlobalString("timeout"), err)
}
// create a clair instance if needed
if c.GlobalString("clair") != "" {
cl, err = clair.New(c.GlobalString("clair"), c.GlobalBool("debug"))
cl, err = clair.New(c.GlobalString("clair"), c.GlobalBool("debug"), timeout)
if err != nil {
logrus.Warnf("creation of clair failed: %v", err)
}