download: Default to a 10s connection establishment timeout.

* guix/build/download.scm (ftp-fetch): Add #:timeout and pass it to
'ftp-open'.
(http-fetch): Add #:timeout and pass it to 'open-connection-for-uri' and
in recursive calls.
(url-fetch): Add #:timeout and pass it to 'http-fetch' and 'ftp-fetch'.
This commit is contained in:
Ludovic Courtès 2016-05-27 10:33:23 +02:00
parent dab2472c6a
commit b18ede2704
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
1 changed files with 13 additions and 9 deletions

View File

@ -234,9 +234,10 @@ and 'guix publish', something like
(string-drop path 33) (string-drop path 33)
path))) path)))
(define (ftp-fetch uri file) (define* (ftp-fetch uri file #:key timeout)
"Fetch data from URI and write it to FILE. Return FILE on success." "Fetch data from URI and write it to FILE. Return FILE on success. Bail
(let* ((conn (ftp-open (uri-host uri))) out if the connection could not be established in less than TIMEOUT seconds."
(let* ((conn (ftp-open (uri-host uri) #:timeout timeout))
(size (false-if-exception (ftp-size conn (uri-path uri)))) (size (false-if-exception (ftp-size conn (uri-path uri))))
(in (ftp-retr conn (basename (uri-path uri)) (in (ftp-retr conn (basename (uri-path uri))
(dirname (uri-path uri))))) (dirname (uri-path uri)))))
@ -585,8 +586,10 @@ Return the resulting target URI."
#:query (uri-query ref) #:query (uri-query ref)
#:fragment (uri-fragment ref))))) #:fragment (uri-fragment ref)))))
(define (http-fetch uri file) (define* (http-fetch uri file #:key timeout)
"Fetch data from URI and write it to FILE. Return FILE on success." "Fetch data from URI and write it to FILE; when TIMEOUT is true, bail out if
the connection could not be established in less than TIMEOUT seconds. Return
FILE on success."
(define post-2.0.7? (define post-2.0.7?
(or (> (string->number (major-version)) 2) (or (> (string->number (major-version)) 2)
@ -605,7 +608,7 @@ Return the resulting target URI."
(Accept . "*/*"))) (Accept . "*/*")))
(let*-values (((connection) (let*-values (((connection)
(open-connection-for-uri uri)) (open-connection-for-uri uri #:timeout timeout))
((resp bv-or-port) ((resp bv-or-port)
;; XXX: `http-get*' was introduced in 2.0.7, and replaced by ;; XXX: `http-get*' was introduced in 2.0.7, and replaced by
;; #:streaming? in 2.0.8. We know we're using it within the ;; #:streaming? in 2.0.8. We know we're using it within the
@ -646,7 +649,7 @@ Return the resulting target URI."
(format #t "following redirection to `~a'...~%" (format #t "following redirection to `~a'...~%"
(uri->string uri)) (uri->string uri))
(close connection) (close connection)
(http-fetch uri file))) (http-fetch uri file #:timeout timeout)))
(else (else
(error "download failed" (uri->string uri) (error "download failed" (uri->string uri)
code (response-reason-phrase resp)))))) code (response-reason-phrase resp))))))
@ -686,6 +689,7 @@ Return a list of URIs."
(define* (url-fetch url file (define* (url-fetch url file
#:key #:key
(timeout 10)
(mirrors '()) (content-addressed-mirrors '()) (mirrors '()) (content-addressed-mirrors '())
(hashes '())) (hashes '()))
"Fetch FILE from URL; URL may be either a single string, or a list of "Fetch FILE from URL; URL may be either a single string, or a list of
@ -711,9 +715,9 @@ or #f."
file (uri->string uri)) file (uri->string uri))
(case (uri-scheme uri) (case (uri-scheme uri)
((http https) ((http https)
(false-if-exception* (http-fetch uri file))) (false-if-exception* (http-fetch uri file #:timeout timeout)))
((ftp) ((ftp)
(false-if-exception* (ftp-fetch uri file))) (false-if-exception* (ftp-fetch uri file #:timeout timeout)))
(else (else
(format #t "skipping URI with unsupported scheme: ~s~%" (format #t "skipping URI with unsupported scheme: ~s~%"
uri) uri)