Skip to content

Commit 17c8719

Browse files
author
ztzhou
committed
feat(git): refresh cached refs in the background
Keep refresh work bounded and preserve active Git requests during cancellation and eviction. Refs: #66 Assisted-by: Codex:gpt-6.1-terra
1 parent 0b571db commit 17c8719

11 files changed

Lines changed: 1357 additions & 44 deletions

File tree

‎README.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ Every flag has an environment-variable equivalent; the full reference is in
7979
- `--serve-token` / `--auth-provider`: how clients authenticate to the proxy.
8080
- `--cache-max-mb`: cap on-disk cache size (LRU eviction; `0` = unlimited).
8181

82+
With a positive `--max-stale-seconds`, an existing mirror can serve cached refs
83+
while one background fetch updates it. Age is measured from the start of the last
84+
successful clone or fetch. When its age exceeds the limit, a request waits for a
85+
refresh or fails. After restart, a mirror's age is unknown and it must refresh
86+
before serving. Background attempts are limited globally and retried no sooner
87+
than the fetch TTL, or one second, after the previous attempt completes.
88+
89+
Fetch-triggered automatic Git maintenance is disabled in this mode so a refresh
90+
does not delete objects just advertised to a client. Active requests are protected
91+
from proxy eviction until their Git process exits. There is no fixed limit between
92+
advertisement and upload-pack, so eviction or external Git maintenance between
93+
requests can remove an old object. A missing SHA is fetched on demand when the
94+
upstream permits it; this is best effort. In this mode a client with access to a
95+
mirror can request any object already present in that mirror by SHA, including an
96+
object no longer reachable from an advertised ref.
97+
98+
Endpoints: `/healthz`, `/readyz`, `/metrics` (see [Metrics](#metrics)).
99+
82100
## Metrics
83101

84102
`GET /metrics` serves Prometheus text-format metrics - per-repo request and
@@ -129,8 +147,7 @@ Pin a version and try it against your own setup before relying on it.
129147

130148
Not yet implemented:
131149

132-
- A background/scheduled refresh (today every `info/refs` triggers an on-demand,
133-
TTL-coalesced fetch).
150+
- Scheduled refresh. Background refresh is triggered by requests.
134151

135152
**Non-goal: a git-free image.** The `git` binary is a permanent runtime
136153
dependency: the proxy serves clients by running `git upload-pack`, and no

‎chart/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pointing at the Service.
114114
| `clientAuth.signingKey.existingSecret` | `""` | Secret with the HMAC key signing LFS download grants; empty = a random key per pod |
115115
| `clientAuth.signingKey.key` | `signing-key` | Key in that Secret |
116116
| `config.fetchTtlSeconds` | `10` | Skip upstream fetch if refreshed within this window (`0` = always) |
117+
| `config.maxStaleSeconds` | `0` | Maximum age of cached refs served while refreshing (`0` = disabled) |
118+
| `config.maxBackgroundFetches` | `4` | Maximum concurrent background refreshes (at least `1`) |
117119
| `config.cacheMaxMb` | `0` | Cap on on-disk cache, MiB; evicts LRU idle mirrors (`0` = unlimited) |
118120
| `config.maxConcurrentRequests` | `64` | Max concurrent in-flight requests (`0` = unlimited) |
119121
| `config.maxDecodedBodyMb` | `512` | Cap on a decoded upload-pack request body, MiB |

‎chart/templates/deployment.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ spec:
6565
value: /var/cache/git-cache-proxy
6666
- name: GITCACHEPROXY_FETCH_TTL_SECONDS
6767
value: {{ .Values.config.fetchTtlSeconds | quote }}
68+
- name: GITCACHEPROXY_MAX_STALE_SECONDS
69+
value: {{ .Values.config.maxStaleSeconds | quote }}
70+
- name: GITCACHEPROXY_MAX_BACKGROUND_FETCHES
71+
value: {{ .Values.config.maxBackgroundFetches | quote }}
6872
- name: GITCACHEPROXY_CACHE_MAX_MB
6973
value: {{ .Values.config.cacheMaxMb | quote }}
7074
- name: GITCACHEPROXY_MAX_CONCURRENT_REQUESTS

‎chart/values.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ config:
8181
# Skip the upstream fetch on info/refs if the mirror was refreshed within this
8282
# many seconds; coalesces bursts of clones for one repo. 0 = always fetch.
8383
fetchTtlSeconds: 10
84+
# Serve cached refs for at most this age while refreshing. 0 disables it.
85+
maxStaleSeconds: 0
86+
# Maximum simultaneous background refreshes, at least 1.
87+
maxBackgroundFetches: 4
8488
# Cap on total on-disk mirror cache, in MiB; evicts least-recently-used idle
8589
# mirrors when exceeded. 0 = unlimited (no eviction). Set this whenever the
8690
# cache volume is bounded.

‎docs/configuration.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Every flag has an environment-variable equivalent.
2020
| `--auth-max-concurrent` | `GITCACHEPROXY_AUTH_MAX_CONCURRENT` | `32` | Max token-validation requests in flight; a check waits up to `--auth-timeout-seconds` for a slot, then fails with 503 (`0` = unlimited) |
2121
| `--auth-allow-http` | `GITCACHEPROXY_AUTH_ALLOW_HTTP` | `false` | Provider modes: accept a plain `http://` validation endpoint, which sends client credentials in cleartext |
2222
| `--fetch-ttl-seconds` | `GITCACHEPROXY_FETCH_TTL_SECONDS` | `10` | Skip upstream fetch if the last one started within this window (`0` = always fetch) |
23+
| `--max-stale-seconds` | `GITCACHEPROXY_MAX_STALE_SECONDS` | `0` | Maximum age of cached refs served during background refresh (`0` = disabled) |
24+
| `--max-background-fetches` | `GITCACHEPROXY_MAX_BACKGROUND_FETCHES` | `4` | Maximum concurrent background refreshes (at least `1`) |
2325
| `--max-concurrent-requests` | `GITCACHEPROXY_MAX_CONCURRENT_REQUESTS` | `64` | Max concurrent in-flight requests; excess queue (`0` = unlimited) |
2426
| `--max-concurrent-upload-packs` | `GITCACHEPROXY_MAX_CONCURRENT_UPLOAD_PACKS` | `0` | Max concurrent `git upload-pack` processes, held until each exits; bounds `pack-objects` memory. Clones waiting for a slot hold a `--max-concurrent-requests` slot (`0` = unlimited) |
2527
| `--max-decoded-body-mb` | `GITCACHEPROXY_MAX_DECODED_BODY_MB` | `512` | Cap on a decoded upload-pack request body, in MiB (bounds memory / gzip bombs) |

‎src/config.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ use std::path::PathBuf;
77
use anyhow::{Result, bail};
88
use clap::{Parser, ValueEnum};
99

10+
fn positive_usize(value: &str) -> Result<usize, String> {
11+
let value: usize = value.parse().map_err(|_| "expected a positive integer")?;
12+
if value == 0 || value > tokio::sync::Semaphore::MAX_PERMITS {
13+
return Err("value is outside the semaphore range".into());
14+
}
15+
Ok(value)
16+
}
17+
1018
/// Log output format.
1119
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
1220
pub enum LogFormat {
@@ -215,6 +223,14 @@ pub struct Config {
215223
#[arg(long, env = "GITCACHEPROXY_FETCH_TTL_SECONDS", default_value_t = 10)]
216224
pub fetch_ttl_seconds: u64,
217225

226+
/// Maximum age of cached refs served while refreshing; 0 disables this mode.
227+
#[arg(long, env = "GITCACHEPROXY_MAX_STALE_SECONDS", default_value_t = 0)]
228+
pub max_stale_seconds: u64,
229+
230+
/// Maximum number of concurrent background refreshes.
231+
#[arg(long, env = "GITCACHEPROXY_MAX_BACKGROUND_FETCHES", default_value_t = 4, value_parser = positive_usize)]
232+
pub max_background_fetches: usize,
233+
218234
/// Maximum number of requests handled concurrently, shared across all
219235
/// connections. Excess requests queue until a slot frees. This bounds the
220236
/// concurrent upstream clone/fetch work a burst can trigger (the slot is held
@@ -350,4 +366,12 @@ mod tests {
350366
.to_string();
351367
assert!(err.contains("--upstream-auth-header"), "{err}");
352368
}
369+
370+
#[test]
371+
fn background_fetch_limit_must_be_positive_and_fit_a_semaphore() {
372+
assert_eq!(positive_usize("1").unwrap(), 1);
373+
assert!(positive_usize("0").is_err());
374+
assert!(positive_usize("invalid").is_err());
375+
assert!(positive_usize(&usize::MAX.to_string()).is_err());
376+
}
353377
}

0 commit comments

Comments
 (0)