Skip to content

Commit 8b1e58c

Browse files
author
ztzhou
committed
merge: integrate upstream lifetime controls
Keep refresh cancellation independent of its triggering request and retain preparation leases until upstream children are reaped. Assisted-by: OpenAI:Codex
2 parents 626ff98 + 78ce674 commit 8b1e58c

12 files changed

Lines changed: 922 additions & 81 deletions

File tree

‎Cargo.lock‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ tokio = { version = "1", features = [
5858
"sync",
5959
"time",
6060
] }
61-
tokio-util = { version = "0.7", features = ["io"] }
61+
tokio-util = { version = "0.7", features = ["io", "rt"] }
6262
tower = { version = "0.5", features = ["limit"] }
6363
tracing = "0.1"
6464
tracing-appender = "0.2"

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,21 @@ Contributions welcome.
161161
## License
162162

163163
[Apache-2.0](./LICENSE). Self-contained - no dependencies outside crates.io.
164+
165+
### Upstream operation lifetime
166+
167+
`--upstream-timeout-seconds` (`GITCACHEPROXY_UPSTREAM_TIMEOUT_SECONDS`, default
168+
`3600`) bounds each clone, full-ref fetch and SHA backfill across attempts and
169+
retry delays. The value must be positive and fit in a monotonic deadline.
170+
Library users can set the same budget with `GitCache::with_upstream_timeout`.
171+
The budget starts when the operation starts, after the repository lock is acquired.
172+
173+
SIGTERM and Ctrl-C reject new upstream work and cancel active commands, retry
174+
backoff and repository lock waits before HTTP drain. Upstream owners kill process
175+
groups on Unix and wait for the direct child before releasing repository locks.
176+
Dropped requests also cancel their upstream owner. Library hosts call
177+
`GitCache::cancel_upstream` before draining HTTP, `wait_for_shutdown` after
178+
draining, then the async `shutdown` method to drain detached request and refresh tasks.
179+
Active local upload-pack responses continue to drain. This does not impose a hard
180+
shutdown bound on active uploads or blocking filesystem work. On non-Unix targets,
181+
cancellation terminates only the direct child.

‎chart/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pointing at the Service.
113113
| `clientAuth.allowHttp` | `false` | Accept a plain http validation endpoint (credentials in cleartext) |
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 |
116+
| `config.upstreamTimeoutSeconds` | `3600` | Positive total seconds per upstream operation, including retries |
116117
| `config.fetchTtlSeconds` | `10` | Skip upstream fetch if refreshed within this window (`0` = always) |
117118
| `config.maxStaleSeconds` | `0` | Maximum age of cached refs served while refreshing (`0` = disabled) |
118119
| `config.maxBackgroundFetches` | `4` | Maximum concurrent background refreshes (at least `1`) |

‎chart/templates/deployment.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
{{- if or (not (regexMatch "^[1-9][0-9]*$" (toString .Values.config.upstreamTimeoutSeconds))) (le (int64 .Values.config.upstreamTimeoutSeconds) 0) }}
2+
{{- fail "config.upstreamTimeoutSeconds must be a positive integer" }}
3+
{{- end }}
14
apiVersion: apps/v1
25
kind: Deployment
36
metadata:
@@ -63,6 +66,8 @@ spec:
6366
value: {{ .Values.upstream | quote }}
6467
- name: GITCACHEPROXY_CACHE_ROOT
6568
value: /var/cache/git-cache-proxy
69+
- name: GITCACHEPROXY_UPSTREAM_TIMEOUT_SECONDS
70+
value: {{ .Values.config.upstreamTimeoutSeconds | quote }}
6671
- name: GITCACHEPROXY_FETCH_TTL_SECONDS
6772
value: {{ .Values.config.fetchTtlSeconds | quote }}
6873
- name: GITCACHEPROXY_MAX_STALE_SECONDS

‎chart/values.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ config:
8585
maxStaleSeconds: 0
8686
# Maximum simultaneous background refreshes, at least 1.
8787
maxBackgroundFetches: 4
88+
# Total upstream command budget, including retries (positive seconds).
89+
upstreamTimeoutSeconds: 3600
8890
# Cap on total on-disk mirror cache, in MiB; evicts least-recently-used idle
8991
# mirrors when exceeded. 0 = unlimited (no eviction). Set this whenever the
9092
# cache volume is bounded.

‎src/config.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ pub struct Config {
217217
#[arg(long, env = "GITCACHEPROXY_AUTH_TTL_SECONDS", default_value_t = 0)]
218218
pub auth_ttl_seconds: u64,
219219

220+
/// Total seconds per upstream clone, full-ref fetch or SHA backfill,
221+
/// including all attempts and retry delays. Must be positive.
222+
#[arg(long, env = "GITCACHEPROXY_UPSTREAM_TIMEOUT_SECONDS", default_value = "3600",
223+
value_parser = parse_upstream_timeout)]
224+
pub upstream_timeout_seconds: u64,
225+
220226
/// Skip the upstream fetch on `info/refs` if the last fetch started within
221227
/// this many seconds. Coalesces bursts of clones for the same repo. `0` =
222228
/// always fetch (freshest, more upstream load).
@@ -318,10 +324,25 @@ pub struct Config {
318324
pub log_format: LogFormat,
319325
}
320326

327+
fn parse_upstream_timeout(value: &str) -> Result<u64, String> {
328+
let seconds = value.parse::<u64>().map_err(|e| e.to_string())?;
329+
crate::git::validate_upstream_timeout(std::time::Duration::from_secs(seconds))
330+
.map_err(|e| e.to_string())?;
331+
Ok(seconds)
332+
}
333+
321334
#[cfg(test)]
322335
mod tests {
323336
use super::*;
324337

338+
#[test]
339+
fn upstream_timeout_parser_matches_library_validation() {
340+
assert_eq!(parse_upstream_timeout("3600").unwrap(), 3600);
341+
for value in ["0", "-1", "18446744073709551615", "18446744073709551616"] {
342+
assert!(parse_upstream_timeout(value).is_err(), "{value}");
343+
}
344+
}
345+
325346
#[test]
326347
fn static_mode_never_conflicts() {
327348
for provider in [

0 commit comments

Comments
 (0)