Add read-only mode to allow cache reads but skip writes#9
Conversation
This enables use cases where you want to pull from the cache but not populate it, such as running tests on feature branches without polluting the shared cache. - Add -read-only flag and GOBUILDCACHE_READ_ONLY env var - Skip PUT operations when enabled, returning success without writing - Add skippedPuts counter to stats output - Fix division-by-zero in stats when getCount or putCount is zero - Add unit tests for read-only mode behavior
server.go
Outdated
| duplicateGets, float64(duplicateGets)/float64(getCount)*100) | ||
| fmt.Fprintf(os.Stderr, " Deduplicated GETs (singleflight): %d (%.1f%% of GETs)\n", | ||
| deduplicatedGets, float64(deduplicatedGets)/float64(getCount)*100) | ||
| if getCount > 0 { |
There was a problem hiding this comment.
can you remove this conditional so the output is uniform?
There was a problem hiding this comment.
Removed the conditionals entirely. Stats lines are now always printed with inline division, matching the original code style.
server.go
Outdated
| if skippedPuts > 0 { | ||
| fmt.Fprintf(os.Stderr, " Skipped PUTs (read-only mode): %d\n", skippedPuts) | ||
| } | ||
| if putCount > 0 { |
There was a problem hiding this comment.
Same — removed the putCount > 0 conditional as well.
Pre-compute percentage rates with division-by-zero guards instead of conditionally omitting the stats lines when counts are zero.
server.go
Outdated
| resp.ID = req.ID | ||
|
|
||
| // In read-only mode, skip all writes but return success | ||
| if cp.readOnly { |
There was a problem hiding this comment.
I think you might be terminating too early. If you do this, then the program won't even write to the local cache and you'll keep recompiling the same stuff over and over locally. I think what you want is to still write locally, but not write back to the object store right?
There was a problem hiding this comment.
Good catch — moved the read-only check to after the local cache write so local builds still benefit from disk cache. Only the backend PUT is skipped now.
Previously, read-only mode returned early in handlePut, skipping both the local disk cache and backend writes. This meant local builds would recompile the same artifacts repeatedly since nothing was cached locally. Now the read-only check happens after the local cache write but before the backend PUT, so local builds still benefit from the disk cache while the shared remote cache is not polluted with feature branch artifacts.
Summary
-read-onlyflag andGOBUILDCACHE_READ_ONLYenvironment variable to enable read-only modeChanges
readOnlyfield,skippedPutscounter, skip logic inhandlePut, and fixed pre-existing division-by-zero bugs in stats output whengetCountorputCountis zeroTest plan
go test ./...)GOBUILDCACHE_READ_ONLY=trueto verify PUTs are skipped