Repository navigation
Commit 9ac8a99
authored
fix(experiments): do not cache a failed npm lookup (#1787)
## Bug
`Guesser.checkNPMPackageWithClient` (`internal/experiments/guesser.go`)
writes its result to the cache manager on every exit path, including the
failure paths: request errors, timeouts, non-200/404 statuses (5xx,
429), and an unparseable body. Those entries use the default 2h TTL, and
the cache is checked before the network on the next call.
A failed lookup tells us nothing about whether the package exists, but
it gets cached as `exists: false`. For two hours, every registry search
that involves that repository loses its npm `install_cmd` /
`repository_info`.
## Trigger
The runtime's registry search (`Runtime.SearchRegistryServers`) builds a
guesser with the shared cache manager. Batch lookups use a 3s client
timeout. One slow npm response, a brief network drop or an npm 5xx
during a search caches the failure. Refreshing the registry
(`RefreshRegistryCache`) does not help, because it only drops the
`registry-servers:` keys, while the guess entries live under
`npm:<package>`.
## Fix
Cache only the definitive answers: 200 (package found) and 404 (package
does not exist). Failed lookups are returned without being cached, so
the next search tries again.
## Test
`guesser_transient_cache_test.go` drives `checkNPMPackageWithClient`
with a scripted `http.RoundTripper`, so it makes no real network calls:
- `TestCheckNPMPackage_TransientFailureNotCached`: the first response is
a network error, a 503 or a 429, and the second is a 200. The second
call must reach the transport and report the package as found.
- `TestCheckNPMPackage_DefinitiveAnswersCached`: a 200 and a 404 are
each still served from the cache on the second call.
## Commands run
- `CGO_ENABLED=0 go test -count=1 -run
'TestCheckNPMPackage_(TransientFailureNotCached|DefinitiveAnswersCached)|TestGuesser_CacheEntriesAreStampedInternal|TestCaching|TestNilCacheManager'
./internal/experiments/`: ok
- `CGO_ENABLED=0 go vet ./internal/experiments/`: ok
- On current `main` (and again with `guesser.go` reverted), all three
`TransientFailureNotCached` subtests fail: `expected: 2, actual: 1`
transport calls, because the second call returned the cached failure.1 parent 4fd87dc commit 9ac8a99
2 files changed
Lines changed: 98 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
278 | 278 | | |
279 | 279 | | |
280 | 280 | | |
281 | | - | |
282 | 281 | | |
283 | 282 | | |
284 | 283 | | |
| |||
288 | 287 | | |
289 | 288 | | |
290 | 289 | | |
291 | | - | |
292 | 290 | | |
293 | 291 | | |
294 | 292 | | |
295 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
296 | 297 | | |
297 | 298 | | |
298 | 299 | | |
| |||
301 | 302 | | |
302 | 303 | | |
303 | 304 | | |
304 | | - | |
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
311 | | - | |
312 | 311 | | |
313 | 312 | | |
314 | 313 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
0 commit comments