@@ -9,7 +9,7 @@ use std::sync::LazyLock;
99use std:: time:: { SystemTime , UNIX_EPOCH } ;
1010
1111use bytes:: { Bytes , BytesMut } ;
12- use futures:: AsyncRead ;
12+ use futures:: { AsyncRead , FutureExt } ;
1313use nix_types:: { NarFileName , NarInfo , NarInfoFileName } ;
1414use reqwest:: { Method , StatusCode , header} ;
1515use smallvec:: SmallVec ;
@@ -27,6 +27,7 @@ use crate::async_read_ext::AsyncReadExt;
2727use crate :: context;
2828use crate :: protocol:: StoreDir ;
2929use crate :: run:: RunArgs ;
30+ use crate :: tokio:: TokioSpawner ;
3031
3132type AuthToken = String ;
3233
@@ -37,6 +38,7 @@ static SUB60_CACHE_URL: LazyLock<url::Url> =
3738pub ( crate ) struct Sub60Cache {
3839 cache_url : url:: Url ,
3940 client : reqwest:: Client ,
41+ spawner : TokioSpawner ,
4042}
4143
4244#[ derive( Debug , clap:: Args ) ]
@@ -129,6 +131,7 @@ impl Sub60Cache {
129131 /// TODO: docs.
130132 pub ( crate ) async fn connect (
131133 RunArgs { sub60_cache_args : args, .. } : & RunArgs ,
134+ spawner : TokioSpawner ,
132135 ) -> Result < Self , Sub60CacheConnectError > {
133136 let mut cache_url = SUB60_CACHE_URL . clone ( ) ;
134137 cache_url
@@ -141,7 +144,7 @@ impl Sub60Cache {
141144 . build ( )
142145 . map_err ( Sub60CacheConnectError :: BuildHttpClient ) ?;
143146
144- Ok ( Self { cache_url, client } )
147+ Ok ( Self { cache_url, client, spawner } )
145148 }
146149
147150 async fn initiate_multipart_nar_upload (
@@ -432,27 +435,46 @@ impl CacheRequestError {
432435impl context:: Cache for Sub60Cache {
433436 type Error = CacheRequestError ;
434437
435- async fn has_narinfo (
438+ fn has_narinfo (
436439 & self ,
437440 narinfo_filename : & NarInfoFileName ,
438- ) -> Result < bool , Self :: Error > {
441+ ) -> impl Future < Output = Result < bool , Self :: Error > > + Send {
439442 let url = self . narinfo_url ( narinfo_filename) ;
440443
441- let response =
442- self . client . head ( url. clone ( ) ) . send ( ) . await . map_err ( |source| {
443- CacheRequestError :: HttpRequest { method : Method :: HEAD , source }
444- } ) ?;
445-
446- match response. status ( ) {
447- StatusCode :: OK => Ok ( true ) ,
448- StatusCode :: NOT_FOUND => Ok ( false ) ,
449- _ => Err ( CacheRequestError :: unexpected_response (
450- response,
451- Method :: HEAD ,
452- url,
453- )
454- . await ) ,
455- }
444+ #[ expect( clippy:: result_large_err) ]
445+ let future =
446+ self . client . head ( url. clone ( ) ) . send ( ) . map (
447+ move |result| match result. map ( |resp| resp. status ( ) ) {
448+ Ok ( StatusCode :: OK ) => Ok ( true ) ,
449+ Ok ( StatusCode :: NOT_FOUND ) => Ok ( false ) ,
450+ Ok ( status) => Err ( CacheRequestError :: UnexpectedResponse {
451+ method : Method :: HEAD ,
452+ status,
453+ url,
454+ body : String :: new ( ) ,
455+ } ) ,
456+ Err ( source) => Err ( CacheRequestError :: HttpRequest {
457+ method : Method :: HEAD ,
458+ source,
459+ } ) ,
460+ } ,
461+ ) ;
462+
463+ // The caller will race this NARInfo lookup against the upstream caches
464+ // and intentionally drops the losing future. Dropping an in-flight
465+ // HTTP/2 request causes reqwest to terminate the corresponding stream
466+ // by sending an RST_STREAM frame. When handling many paths in rapid
467+ // succession, if the upstream lookups repeatedly win their races, this
468+ // produces a burst of resets on the single multiplexed connection to
469+ // cache.sub60.dev. Cloudflare treats that pattern as an HTTP/2 Rapid
470+ // Reset attack and closes the entire connection, taking unrelated
471+ // NARInfo lookups down with it (see
472+ // <https://blog.cloudflare.com/technical-breakdown-http2-rapid-reset-ddos-attack/>
473+ // for details).
474+ //
475+ // To prevent this, we let the lookup run to completion even if the
476+ // caller drops the future.
477+ self . spawner . run_to_completion ( future)
456478 }
457479
458480 async fn upload_nar (
0 commit comments