summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2015-10-28 11:45:27 +0100
committerLudovic Courtès <ludo@gnu.org>2015-10-28 12:04:03 +0100
commitf151298fa00c9532d29cdc9eb4930fb2bfc23c06 (patch)
treed55549408d8b673f37798e02d7f84b1315cf26ef
parent58301666dd3e372ee05aa23e4043781c8d24be97 (diff)
substitute: 'http-multiple-get' follows 'fold' style.
* guix/scripts/substitute.scm (http-multiple-get): Add 'seed' parameter. Call PROC in 'fold' style. (fetch-narinfos)[handle-narinfo-response]: Adjust accordingly. Update 'http-multiple-get' call accordingly.
-rwxr-xr-xguix/scripts/substitute.scm26
1 files changed, 14 insertions, 12 deletions
diff --git a/guix/scripts/substitute.scm b/guix/scripts/substitute.scm
index 8967fa062ed..0377bb6abe5 100755
--- a/guix/scripts/substitute.scm
+++ b/guix/scripts/substitute.scm
@@ -474,12 +474,13 @@ may be #f, in which case it indicates that PATH is unavailable at CACHE-URL."
474 ".narinfo"))) 474 ".narinfo")))
475 (build-request (string->uri url) #:method 'GET))) 475 (build-request (string->uri url) #:method 'GET)))
476 476
477(define (http-multiple-get base-url requests proc) 477(define (http-multiple-get base-url proc seed requests)
478 "Send all of REQUESTS to the server at BASE-URL. Call PROC for each 478 "Send all of REQUESTS to the server at BASE-URL. Call PROC for each
479response, passing it the request object, the response, and a port from which 479response, passing it the request object, the response, a port from which to
480to read the response body. Return the list of results." 480read the response body, and the previous result, starting with SEED, à la
481'fold'. Return the final result."
481 (let connect ((requests requests) 482 (let connect ((requests requests)
482 (result '())) 483 (result seed))
483 ;; (format (current-error-port) "connecting (~a requests left)..." 484 ;; (format (current-error-port) "connecting (~a requests left)..."
484 ;; (length requests)) 485 ;; (length requests))
485 (let ((p (open-socket-for-uri base-url))) 486 (let ((p (open-socket-for-uri base-url)))
@@ -497,7 +498,7 @@ to read the response body. Return the list of results."
497 ((head tail ...) 498 ((head tail ...)
498 (let* ((resp (read-response p)) 499 (let* ((resp (read-response p))
499 (body (response-body-port resp)) 500 (body (response-body-port resp))
500 (result (cons (proc head resp body) result))) 501 (result (proc head resp body result)))
501 ;; The server can choose to stop responding at any time, in which 502 ;; The server can choose to stop responding at any time, in which
502 ;; case we have to try again. Check whether that is the case. 503 ;; case we have to try again. Check whether that is the case.
503 ;; Note that even upon "Connection: close", we can read from BODY. 504 ;; Note that even upon "Connection: close", we can read from BODY.
@@ -536,7 +537,7 @@ if file doesn't exist, and the narinfo otherwise."
536 url (* 100. (/ done (length paths)))) 537 url (* 100. (/ done (length paths))))
537 (set! done (+ 1 done))))) 538 (set! done (+ 1 done)))))
538 539
539 (define (handle-narinfo-response request response port) 540 (define (handle-narinfo-response request response port result)
540 (let ((len (response-content-length response))) 541 (let ((len (response-content-length response)))
541 ;; Make sure to read no more than LEN bytes since subsequent bytes may 542 ;; Make sure to read no more than LEN bytes since subsequent bytes may
542 ;; belong to the next response. 543 ;; belong to the next response.
@@ -545,7 +546,7 @@ if file doesn't exist, and the narinfo otherwise."
545 (let ((narinfo (read-narinfo port url #:size len))) 546 (let ((narinfo (read-narinfo port url #:size len)))
546 (cache-narinfo! url (narinfo-path narinfo) narinfo) 547 (cache-narinfo! url (narinfo-path narinfo) narinfo)
547 (update-progress!) 548 (update-progress!)
548 narinfo)) 549 (cons narinfo result)))
549 ((404) ; failure 550 ((404) ; failure
550 (let* ((path (uri-path (request-uri request))) 551 (let* ((path (uri-path (request-uri request)))
551 (hash-part (string-drop-right path 8))) ; drop ".narinfo" 552 (hash-part (string-drop-right path 8))) ; drop ".narinfo"
@@ -555,13 +556,13 @@ if file doesn't exist, and the narinfo otherwise."
555 (cache-narinfo! url 556 (cache-narinfo! url
556 (find (cut string-contains <> hash-part) paths) 557 (find (cut string-contains <> hash-part) paths)
557 #f) 558 #f)
558 (update-progress!)) 559 (update-progress!)
559 #f) 560 result))
560 (else ; transient failure 561 (else ; transient failure
561 (if len 562 (if len
562 (get-bytevector-n port len) 563 (get-bytevector-n port len)
563 (read-to-eof port)) 564 (read-to-eof port))
564 #f)))) 565 result))))
565 566
566 (define cache-info 567 (define cache-info
567 (download-cache-info url)) 568 (download-cache-info url))
@@ -574,8 +575,9 @@ if file doesn't exist, and the narinfo otherwise."
574 ((http) 575 ((http)
575 (let ((requests (map (cut narinfo-request url <>) paths))) 576 (let ((requests (map (cut narinfo-request url <>) paths)))
576 (update-progress!) 577 (update-progress!)
577 (let ((result (http-multiple-get url requests 578 (let ((result (http-multiple-get url
578 handle-narinfo-response))) 579 handle-narinfo-response '()
580 requests)))
579 (newline (current-error-port)) 581 (newline (current-error-port))
580 result))) 582 result)))
581 ((file #f) 583 ((file #f)