From patchwork Sun May 16 22:11:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 29380 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id B738F27BC81; Sun, 16 May 2021 23:12:23 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 5861A27BC78 for ; Sun, 16 May 2021 23:12:23 +0100 (BST) Received: from localhost ([::1]:60302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liOzm-00061J-Iy for patchwork@mira.cbaines.net; Sun, 16 May 2021 18:12:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39058) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liOzT-0005dH-9W for guix-patches@gnu.org; Sun, 16 May 2021 18:12:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:40089) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1liOzT-00057n-2b for guix-patches@gnu.org; Sun, 16 May 2021 18:12:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1liOzS-0002HQ-UN for guix-patches@gnu.org; Sun, 16 May 2021 18:12:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#47174] [PATCH v2 1/2] guix: Alter http-fetch to return the response. References: <87y2eodxyy.fsf@cbaines.net> In-Reply-To: <87y2eodxyy.fsf@cbaines.net> Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 16 May 2021 22:12:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47174 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 47174@debbugs.gnu.org Received: via spool by 47174-submit@debbugs.gnu.org id=B47174.16212030848685 (code B ref 47174); Sun, 16 May 2021 22:12:02 +0000 Received: (at 47174) by debbugs.gnu.org; 16 May 2021 22:11:24 +0000 Received: from localhost ([127.0.0.1]:51630 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1liOyq-0002Fu-BD for submit@debbugs.gnu.org; Sun, 16 May 2021 18:11:24 -0400 Received: from mira.cbaines.net ([212.71.252.8]:38894) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1liOyo-0002Ff-Ih for 47174@debbugs.gnu.org; Sun, 16 May 2021 18:11:23 -0400 Received: from localhost (unknown [IPv6:2a02:8010:68c1:0:8ac0:b4c7:f5c8:7caa]) by mira.cbaines.net (Postfix) with ESMTPSA id C0B0627BC78 for <47174@debbugs.gnu.org>; Sun, 16 May 2021 23:11:21 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 309302e4 for <47174@debbugs.gnu.org>; Sun, 16 May 2021 22:11:21 +0000 (UTC) From: Christopher Baines Date: Sun, 16 May 2021 23:11:20 +0100 Message-Id: <20210516221121.16705-1-mail@cbaines.net> X-Mailer: git-send-email 2.30.1 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Rather than just the port and response-content-length. I'm looking at using the response headers within the substitute script to work out when to close the connection. * guix/http-client.scm (http-fetch): Return the response as the second value, rather than the response-content-length. * guix/build/download-nar.scm (download-nar): Adapt accordingly. * guix/build/download.scm (url-fetch): Adapt accordingly. * guix/scripts/substitute.scm (process-substitution): Adapt accordingly. --- guix/build/download-nar.scm | 5 +++-- guix/build/download.scm | 9 ++++++--- guix/http-client.scm | 12 ++++++------ guix/scripts/substitute.scm | 12 ++++++++---- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/guix/build/download-nar.scm b/guix/build/download-nar.scm index 867f3c10bb..fbb5d37c0a 100644 --- a/guix/build/download-nar.scm +++ b/guix/build/download-nar.scm @@ -23,6 +23,7 @@ #:autoload (zlib) (call-with-gzip-input-port) #:use-module (guix progress) #:use-module (web uri) + #:use-module (web response) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (ice-9 format) @@ -101,7 +102,7 @@ success, #f otherwise." ((url rest ...) (format #t "Trying content-addressed mirror at ~a...~%" (uri-host (string->uri url))) - (let-values (((port size) + (let-values (((port resp) (catch #t (lambda () (http-fetch (string->uri url))) @@ -109,7 +110,7 @@ success, #f otherwise." (values #f #f))))) (if (not port) (loop rest) - (begin + (let ((size (response-content-length resp))) (if size (format #t "Downloading from ~a (~,2h MiB)...~%" url (/ size (expt 2 20.))) diff --git a/guix/build/download.scm b/guix/build/download.scm index b14db42352..d2006cc1fd 100644 --- a/guix/build/download.scm +++ b/guix/build/download.scm @@ -22,6 +22,7 @@ (define-module (guix build download) #:use-module (web uri) #:use-module (web http) + #:use-module (web response) #:use-module ((web client) #:hide (open-socket-for-uri)) #:use-module (web response) #:use-module (guix base64) @@ -706,7 +707,7 @@ otherwise simply ignore them." (case (uri-scheme uri) ((http https) (false-if-exception* - (let-values (((port size) + (let-values (((port resp) (http-fetch uri #:verify-certificate? verify-certificate? #:timeout timeout))) @@ -716,9 +717,11 @@ otherwise simply ignore them." #:buffer-size %http-receive-buffer-size #:reporter (if print-build-trace? (progress-reporter/trace - file (uri->string uri) size) + file (uri->string uri) + (response-content-length resp)) (progress-reporter/file - (uri-abbreviation uri) size))) + (uri-abbreviation uri) + (response-content-length resp)))) (newline))) file))) ((ftp) diff --git a/guix/http-client.scm b/guix/http-client.scm index 10bc278023..189535079b 100644 --- a/guix/http-client.scm +++ b/guix/http-client.scm @@ -81,11 +81,11 @@ (headers '((user-agent . "GNU Guile"))) (log-port (current-error-port)) timeout) - "Return an input port containing the data at URI, and the expected number of -bytes available or #f. If TEXT? is true, the data at URI is considered to be -textual. Follow any HTTP redirection. When BUFFERED? is #f, return an -unbuffered port, suitable for use in `filtered-port'. HEADERS is an alist of -extra HTTP headers. + "Return an input port containing the data at URI, and the HTTP response from +the server. If TEXT? is true, the data at URI is considered to be textual. +Follow any HTTP redirection. When BUFFERED? is #f, return an unbuffered port, +suitable for use in `filtered-port'. HEADERS is an alist of extra HTTP +headers. When KEEP-ALIVE? is true, the connection is marked as 'keep-alive' and PORT is not closed upon completion. @@ -123,7 +123,7 @@ Raise an '&http-get-error' condition if downloading fails." (response-code resp))) (case code ((200) - (values data (response-content-length resp))) + (values data resp)) ((301 ; moved permanently 302 ; found (redirection) 303 ; see other diff --git a/guix/scripts/substitute.scm b/guix/scripts/substitute.scm index 8e4eae00b3..96f425eaa0 100755 --- a/guix/scripts/substitute.scm +++ b/guix/scripts/substitute.scm @@ -61,6 +61,7 @@ #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) #:use-module (web uri) + #:use-module (web response) #:use-module (guix http-client) #:export (%allow-unauthenticated-substitutes? %reply-file-descriptor @@ -480,10 +481,13 @@ PORT." (uri->string uri)) (warning (G_ "try `--no-substitutes' if the problem persists~%"))) (with-cached-connection uri port - (http-fetch uri #:text? #f - #:port port - #:keep-alive? #t - #:buffered? #f))))) + (let-values (((raw response) + (http-fetch uri #:text? #f + #:port port + #:keep-alive? #t + #:buffered? #f))) + (values raw + (response-content-length response))))))) (else (leave (G_ "unsupported substitute URI scheme: ~a~%") (uri->string uri))))) From patchwork Sun May 16 22:11:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 29379 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id D1FAE27BC81; Sun, 16 May 2021 23:12:12 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 9C8B527BC78 for ; Sun, 16 May 2021 23:12:12 +0100 (BST) Received: from localhost ([::1]:59784 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liOzb-0005fh-RX for patchwork@mira.cbaines.net; Sun, 16 May 2021 18:12:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39054) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liOzS-0005d9-Sa for guix-patches@gnu.org; Sun, 16 May 2021 18:12:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:40088) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1liOzS-00057O-Lo for guix-patches@gnu.org; Sun, 16 May 2021 18:12:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1liOzS-0002HJ-Hd for guix-patches@gnu.org; Sun, 16 May 2021 18:12:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#47174] [PATCH v2 2/2] substitute: Handle closing connections to substitute servers. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 16 May 2021 22:12:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47174 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 47174@debbugs.gnu.org Received: via spool by 47174-submit@debbugs.gnu.org id=B47174.16212030848677 (code B ref 47174); Sun, 16 May 2021 22:12:02 +0000 Received: (at 47174) by debbugs.gnu.org; 16 May 2021 22:11:24 +0000 Received: from localhost ([127.0.0.1]:51628 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1liOyq-0002Fs-2w for submit@debbugs.gnu.org; Sun, 16 May 2021 18:11:24 -0400 Received: from mira.cbaines.net ([212.71.252.8]:38896) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1liOyo-0002Fe-Ih for 47174@debbugs.gnu.org; Sun, 16 May 2021 18:11:22 -0400 Received: from localhost (unknown [IPv6:2a02:8010:68c1:0:8ac0:b4c7:f5c8:7caa]) by mira.cbaines.net (Postfix) with ESMTPSA id C995427BC81 for <47174@debbugs.gnu.org>; Sun, 16 May 2021 23:11:21 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 29660e40 for <47174@debbugs.gnu.org>; Sun, 16 May 2021 22:11:21 +0000 (UTC) From: Christopher Baines Date: Sun, 16 May 2021 23:11:21 +0100 Message-Id: <20210516221121.16705-2-mail@cbaines.net> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210516221121.16705-1-mail@cbaines.net> References: <20210516221121.16705-1-mail@cbaines.net> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches When reusing a HTTP connection to fetch multiple nars, and the remote server signals that the connection should be closed. * guix/scripts/substitute.scm (process-substitution): Close connections to substitute servers when a Connection: close header is specified in the response. --- guix/scripts/substitute.scm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/guix/scripts/substitute.scm b/guix/scripts/substitute.scm index 96f425eaa0..208b8f1273 100755 --- a/guix/scripts/substitute.scm +++ b/guix/scripts/substitute.scm @@ -464,7 +464,9 @@ PORT." (case (uri-scheme uri) ((file) (let ((port (open-file (uri-path uri) "r0b"))) - (values port (stat:size (stat port))))) + (values port + (stat:size (stat port)) + (const #t)))) ; no cleanup to do ((http https) (guard (c ((http-get-error? c) (leave (G_ "download from '~a' failed: ~a, ~s~%") @@ -487,7 +489,12 @@ PORT." #:keep-alive? #t #:buffered? #f))) (values raw - (response-content-length response))))))) + (response-content-length response) + (match (assq 'connection (response-headers response)) + (('connection 'close) + (lambda () + (close-port port))) + (_ (const #t))))))))) (else (leave (G_ "unsupported substitute URI scheme: ~a~%") (uri->string uri))))) @@ -504,7 +511,7 @@ PORT." (format (current-error-port) (G_ "Downloading ~a...~%") (uri->string uri))) - (let*-values (((raw download-size) + (let*-values (((raw download-size post-fetch-cleanup) ;; 'guix publish' without '--cache' doesn't specify a ;; Content-Length, so DOWNLOAD-SIZE is #f in this case. (fetch uri)) @@ -565,6 +572,10 @@ PORT." ;; Wait for the reporter to finish. (every (compose zero? cdr waitpid) pids) + ;; Do post-fetch cleanup, maybe closing the HTTP connection if HTTP is + ;; being used, and the connection should be closed + (post-fetch-cleanup) + ;; Skip a line after what 'progress-reporter/file' printed, and another ;; one to visually separate substitutions. When PRINT-BUILD-TRACE? is ;; true, leave it up to (guix status) to prettify things.