mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
fix(httpcache): propagate sidecar write errors from Fetch
saveMeta now returns an error instead of silently swallowing WriteFile/ Rename failures. Fetch wraps and returns it (with updated=true, since the body rename already succeeded). Callers get a loud signal when the sidecar can't be written — the body is still good, but the next conditional GET may redownload.
This commit is contained in:
parent
f75d5c489a
commit
ba64018838
@ -89,17 +89,21 @@ func (c *Cacher) loadMeta() {
|
||||
}
|
||||
|
||||
// saveMeta writes etag/lastMod to the sidecar file atomically.
|
||||
func (c *Cacher) saveMeta() {
|
||||
func (c *Cacher) saveMeta() error {
|
||||
m := cacheMeta{ETag: c.etag, LastMod: c.lastMod}
|
||||
data, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
tmp := c.metaPath() + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
os.Rename(tmp, c.metaPath())
|
||||
if err := os.Rename(tmp, c.metaPath()); err != nil {
|
||||
_ = os.Remove(tmp)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// New creates a Cacher that fetches URL and writes it to path.
|
||||
@ -219,7 +223,9 @@ func (c *Cacher) Fetch() (updated bool, err error) {
|
||||
if lm := resp.Header.Get("Last-Modified"); lm != "" {
|
||||
c.lastMod = lm
|
||||
}
|
||||
c.saveMeta()
|
||||
if err := c.saveMeta(); err != nil {
|
||||
return true, fmt.Errorf("save meta for %s: %w", c.Path, err)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user