From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id iD/4DfyBmmDEhQAAgWs5BA (envelope-from ) for ; Tue, 11 May 2021 15:09:16 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id yGqRCfyBmmBQIgAAB5/wlQ (envelope-from ) for ; Tue, 11 May 2021 13:09:16 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 973B22CB57 for ; Tue, 11 May 2021 15:09:15 +0200 (CEST) Received: from localhost ([::1]:59134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS8Q-0004aA-D1 for larch@yhetil.org; Tue, 11 May 2021 09:09:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgS8F-0004QX-3v for guix-patches@gnu.org; Tue, 11 May 2021 09:09:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:52040) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS8E-0006C7-S5 for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lgS8E-0003vp-Mr for guix-patches@gnu.org; Tue, 11 May 2021 09:09:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#47897] [PATCH 1/2] publish: Add '--negative-ttl'. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 11 May 2021 13:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47897 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 47897@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 47897-submit@debbugs.gnu.org id=B47897.162073854015098 (code B ref 47897); Tue, 11 May 2021 13:09:02 +0000 Received: (at 47897) by debbugs.gnu.org; 11 May 2021 13:09:00 +0000 Received: from localhost ([127.0.0.1]:35352 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS8B-0003vK-Kz for submit@debbugs.gnu.org; Tue, 11 May 2021 09:09:00 -0400 Received: from eggs.gnu.org ([209.51.188.92]:42908) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lgS89-0003uy-FN for 47897@debbugs.gnu.org; Tue, 11 May 2021 09:08:58 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:48406) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgS83-000625-Ic; Tue, 11 May 2021 09:08:51 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=57486 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lgS83-0004e7-5o; Tue, 11 May 2021 09:08:51 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Tue, 11 May 2021 15:08:41 +0200 Message-Id: <20210511130842.32381-1-ludo@gnu.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <87wnsthpyn.fsf@cbaines.net> References: <87wnsthpyn.fsf@cbaines.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1620738555; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=z63RaufgdBfwvz4AYkfg/FXF8CyyQ2A69F++a60h0lo=; b=UMaC/43w7mc4wnv4Onh6DXd3oQLY7ke/kr2hq5JcxKDLqVh5/VbV7w4CbmKvzOVEkBNx3q ojNweoCBxvd6LKMfJKwbzjDZ53m1SyXZEKFIXvbcAoHTtXmTNpds52y0yhMTDlwp2NPiqp HKl+d1vTL9alFpfqc9ExhlYh2nfSE2WQyq0keFQVN0Wj4R5x8IEtBJ+ZyJx8FlYOqTLDtB 3XYlm9AJLckhTP+YZydYR4xkPUyRvX6r4sVfFPrRjx1ItMqZ9fkVfbWiBTpl5bkxuzX9Cv Nd+7Prmv11QY9wVDIZcIK1brNQ4V2L5LRWq6D1yyn4hkwwD8zKQ4jq5Iw1PSwg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1620738555; a=rsa-sha256; cv=none; b=jrOlGS+MPk7PBsMGN9IPqcq/UL9+lhj/uDJIWPAvnI/996Daa27J4dpGqWhdsRYgqDGk2x dotvq80ep+HopatpjE2PEjufF1hFoPcnEXUzt51xiA/LhHZPCUE+8OkKsC5Pu6fRqOaz+s QBNYRQIgqBPonlzGjh+nxrGql5QG3Y4Yy2YS6iyQjB46gDGx08OQSkT/cckPVNI2Gm6uz7 1g5M7HSmgi81yd9Wb+WiHwfIMk5h/bbV636YKTygEOk02dAM0WF2nLmTbD0EZIBZ8rz1xw SclyAfBwBsi5oB03LrmBbnWVQS/GvcmciKBCbjFfhw7kd/MQ/4bt0qp7wgHuqA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -1.95 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 973B22CB57 X-Spam-Score: -1.95 X-Migadu-Scanner: scn0.migadu.com X-TUID: ccYtLswyvxdn * guix/scripts/publish.scm (show-help, %options): Add '--negative-ttl'. (render-narinfo, render-narinfo/cached, make-request-handler): Add #:negative-ttl and honor it. (run-publish-server): Add #:narinfo-negative-ttl and honor it. (guix-publish): Honor '--negative-ttl'. * tests/publish.scm ("negative TTL", "no negative TTL"): New tests. --- doc/guix.texi | 10 ++++++++++ guix/scripts/publish.scm | 30 ++++++++++++++++++++++-------- tests/publish.scm | 32 +++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 0947b9f028..a34b2fca1e 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -12727,6 +12727,16 @@ Additionally, when @option{--cache} is used, cached entries that have not been accessed for @var{ttl} and that no longer have a corresponding item in the store, may be deleted. +@item --negative-ttl=@var{ttl} +Similarly produce @code{Cache-Control} HTTP headers to advertise the +time-to-live (TTL) of @emph{negative} lookups---missing store items, for +which the HTTP 404 code is returned. By default, no negative TTL is +advertised. + +This parameter can help adjust server load and substitute latency by +instructing cooperating clients to be more or less patient when a store +item is missing. + @item --cache-bypass-threshold=@var{size} When used in conjunction with @option{--cache}, store items smaller than @var{size} are immediately available, even when they are not yet in diff --git a/guix/scripts/publish.scm b/guix/scripts/publish.scm index 39bb224cad..ef6fa5f074 100644 --- a/guix/scripts/publish.scm +++ b/guix/scripts/publish.scm @@ -1,7 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson ;;; Copyright © 2020 by Amar M. Singh -;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2020 Maxim Cournoyer ;;; Copyright © 2021 Simon Tournier ;;; @@ -101,6 +101,8 @@ Publish ~a over HTTP.\n") %store-directory) --workers=N use N workers to bake items")) (display (G_ " --ttl=TTL announce narinfos can be cached for TTL seconds")) + (display (G_ " + --negative-ttl=TTL announce missing narinfos can be cached for TTL seconds")) (display (G_ " --nar-path=PATH use PATH as the prefix for nar URLs")) (display (G_ " @@ -224,6 +226,13 @@ usage." (leave (G_ "~a: invalid duration~%") arg)) (alist-cons 'narinfo-ttl (time-second duration) result)))) + (option '("negative-ttl") #t #f + (lambda (opt name arg result) + (let ((duration (string->duration arg))) + (unless duration + (leave (G_ "~a: invalid duration~%") arg)) + (alist-cons 'narinfo-negative-ttl (time-second duration) + result)))) (option '("nar-path") #t #f (lambda (opt name arg result) (alist-cons 'nar-path arg result))) @@ -390,14 +399,14 @@ References: ~a~%" (define* (render-narinfo store request hash #:key ttl (compressions (list %no-compression)) - (nar-path "nar")) + (nar-path "nar") negative-ttl) "Render metadata for the store path corresponding to HASH. If TTL is true, advertise it as the maximum validity period (in seconds) via the 'Cache-Control' header. This allows 'guix substitute' to cache it for an appropriate duration. NAR-PATH specifies the prefix for nar URLs." (let ((store-path (hash-part->path store hash))) (if (string-null? store-path) - (not-found request #:phrase "") + (not-found request #:phrase "" #:ttl negative-ttl) (values `((content-type . (application/x-nix-narinfo)) ,@(if ttl `((cache-control (max-age . ,ttl))) @@ -512,7 +521,7 @@ interpreted as the basename of a store item." (define* (render-narinfo/cached store request hash #:key ttl (compressions (list %no-compression)) - (nar-path "nar") + (nar-path "nar") negative-ttl cache pool) "Respond to the narinfo request for REQUEST. If the narinfo is available in CACHE, then send it; otherwise, return 404 and \"bake\" that nar and narinfo @@ -536,7 +545,7 @@ requested using POOL." #:compression (first compressions))))) (cond ((string-null? item) - (not-found request)) + (not-found request #:ttl negative-ttl)) ((file-exists? cached) ;; Narinfo is in cache, send it. (values `((content-type . (application/x-nix-narinfo)) @@ -584,7 +593,7 @@ requested using POOL." #:phrase "We're baking it" #:ttl 300))) ;should be available within 5m (else - (not-found request #:phrase ""))))) + (not-found request #:phrase "" #:ttl negative-ttl))))) (define (compress-nar cache item compression) "Save in directory CACHE the nar for ITEM compressed with COMPRESSION." @@ -974,7 +983,7 @@ methods, return the applicable compression." (define* (make-request-handler store #:key cache pool - narinfo-ttl + narinfo-ttl narinfo-negative-ttl (nar-path "nar") (compressions (list %no-compression))) (define compression-type? @@ -1006,10 +1015,12 @@ methods, return the applicable compression." #:cache cache #:pool pool #:ttl narinfo-ttl + #:negative-ttl narinfo-negative-ttl #:nar-path nar-path #:compressions compressions) (render-narinfo store request hash #:ttl narinfo-ttl + #:negative-ttl narinfo-negative-ttl #:nar-path nar-path #:compressions compressions))) ;; /nar/file/NAME/sha256/HASH @@ -1068,7 +1079,7 @@ methods, return the applicable compression." #:key advertise? port (compressions (list %no-compression)) - (nar-path "nar") narinfo-ttl + (nar-path "nar") narinfo-ttl narinfo-negative-ttl cache pool) (when advertise? (let ((name (service-name))) @@ -1084,6 +1095,7 @@ methods, return the applicable compression." #:pool pool #:nar-path nar-path #:narinfo-ttl narinfo-ttl + #:narinfo-negative-ttl narinfo-negative-ttl #:compressions compressions) concurrent-http-server `(#:socket ,socket))) @@ -1127,6 +1139,7 @@ methods, return the applicable compression." (user (assoc-ref opts 'user)) (port (assoc-ref opts 'port)) (ttl (assoc-ref opts 'narinfo-ttl)) + (negative-ttl (assoc-ref opts 'narinfo-negative-ttl)) (compressions (match (filter-map (match-lambda (('compression . compression) compression) @@ -1192,6 +1205,7 @@ consider using the '--user' option!~%"))) "publish worker")) #:nar-path nar-path #:compressions compressions + #:narinfo-negative-ttl negative-ttl #:narinfo-ttl ttl)))))) ;;; Local Variables: diff --git a/tests/publish.scm b/tests/publish.scm index 3e67c435ac..c3d086995a 100644 --- a/tests/publish.scm +++ b/tests/publish.scm @@ -1,7 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson ;;; Copyright © 2020 by Amar M. Singh -;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -700,6 +700,36 @@ References: ~%" (= (response-content-length response) (stat:size (stat log))) (first (response-content-type response)))))) +(test-equal "negative TTL" + `(404 42) + + (call-with-temporary-directory + (lambda (cache) + (let ((thread (with-separate-output-ports + (call-with-new-thread + (lambda () + (guix-publish "--port=6786" "-C0" + "--negative-ttl=42s")))))) + (wait-until-ready 6786) + + (let* ((base "http://localhost:6786/") + (url (string-append base (make-string 32 #\z) + ".narinfo")) + (response (http-get url))) + (list (response-code response) + (match (assq-ref (response-headers response) 'cache-control) + ((('max-age . ttl)) ttl) + (_ #f)))))))) + +(test-equal "no negative TTL" + `(404 #f) + (let* ((uri (publish-uri + (string-append "/" (make-string 32 #\z) + ".narinfo"))) + (response (http-get uri))) + (list (response-code response) + (assq-ref (response-headers response) 'cache-control)))) + (test-equal "/log/NAME not found" 404 (let ((uri (publish-uri "/log/does-not-exist"))) -- 2.31.1