@@ -46,6 +46,9 @@ const (
4646 ecosystem = "git"
4747 upstreamHashLen = 8
4848 checkedStamp = "proxy-checked"
49+ // urlFile holds the upstream URL the last successful refresh used, for
50+ // background refreshes.
51+ urlFile = "proxy-url"
4952 usedStamp = "proxy-used"
5053 dirPermissions = 0o750
5154 lockFilePermissions = 0o600
@@ -62,17 +65,21 @@ type Options struct {
6265 // processes the cache runs. Redirects are never followed.
6366 Client * http.Client
6467 Logger * slog.Logger
68+ // WarmMaxIdle, with StaleMaxAge, lets Warm refresh the repositories
69+ // clients used within it before StaleMaxAge runs out. 0 turns that off.
70+ WarmMaxIdle time.Duration
6571 // Upstreams maps each configured upstream's name to its directory in
6672 // CacheDir, as UpstreamDir names it. Only those directories are loaded
6773 // at startup. A directory UpstreamDir would name for one of these names
6874 // at another URL, left by repointing the upstream, is removed. Nothing
6975 // else in CacheDir is touched, so it may hold other data.
7076 Upstreams map [string ]string
7177
72- // refCheckTimeout, lowSpeedTime, maxRequestBody, maxUploadPacks and
73- // maxRefListings override their defaults in tests, and refreshWaitOver,
74- // when set, replaces the RefreshWait timer.
78+ // refCheckTimeout, lowSpeedTime, maxRequestBody, maxUploadPacks,
79+ // maxRefListings and warmTick override their defaults in tests, and
80+ // refreshWaitOver, when set, replaces the RefreshWait timer.
7581 refCheckTimeout time.Duration
82+ warmTick time.Duration
7683 refreshWaitOver <- chan time.Time
7784 lowSpeedTime int
7885 maxRequestBody int64
@@ -158,6 +165,27 @@ type repo struct {
158165 // is current while the count is the one it was read at.
159166 state atomic.Pointer [cachedState ]
160167 refsWritten atomic.Int64
168+ // url is the upstream URL the last successful refresh used.
169+ url atomic.Pointer [string ]
170+ }
171+
172+ // repoURL returns the upstream URL the last successful refresh used, or ""
173+ // if none is recorded.
174+ func (rp * repo ) repoURL () string {
175+ if u := rp .url .Load (); u != nil {
176+ return * u
177+ }
178+ return ""
179+ }
180+
181+ // rememberURL records repoURL as the one a refresh succeeded with, on disk
182+ // too once the repository is cached.
183+ func (rp * repo ) rememberURL (repoURL string ) {
184+ if rp .repoURL () == repoURL || ! rp .exists () {
185+ return
186+ }
187+ rp .url .Store (& repoURL )
188+ _ = os .WriteFile (filepath .Join (rp .dir , urlFile ), []byte (repoURL + "\n " ), lockFilePermissions )
161189}
162190
163191var errLocked = errors .New ("locked by another process" )
@@ -323,6 +351,11 @@ func (c *Cache) load() error {
323351 rp .lastChecked .Store (stampTime (path , checkedStamp ))
324352 rp .lastUsed .Store (stampTime (path , usedStamp ))
325353 rp .usedWritten .Store (rp .lastUsed .Load ())
354+ if data , err := os .ReadFile (filepath .Join (path , urlFile )); err == nil {
355+ if u := strings .TrimSpace (string (data )); u != "" {
356+ rp .url .Store (& u )
357+ }
358+ }
326359 c .repos [rp .key ] = rp
327360 return filepath .SkipDir
328361 })
@@ -439,6 +472,7 @@ func (c *Cache) repoKey(key string) string {
439472 return key
440473}
441474
475+ // release ends a use of the repository by a client, recording it as used.
442476func (c * Cache ) release (rp * repo ) {
443477 now := time .Now ()
444478 rp .lastUsed .Store (now .UnixNano ())
@@ -447,7 +481,12 @@ func (c *Cache) release(rp *repo) {
447481 rp .usedWritten .Store (now .UnixNano ())
448482 touch (rp .dir , usedStamp , now )
449483 }
484+ c .drop (rp , exists )
485+ }
450486
487+ // drop ends a use of the repository without recording it as used. exists
488+ // is whether the repository is cloned.
489+ func (c * Cache ) drop (rp * repo , exists bool ) {
451490 c .mu .Lock ()
452491 rp .users --
453492 idle := rp .users == 0 && c .repos [rp .key ] == rp
0 commit comments