Additional comments

This commit is contained in:
neilotoole 2024-01-29 07:11:33 -07:00
parent a5d4e885b2
commit ed7b16d74a

View File

@ -6,6 +6,7 @@ import (
"context" "context"
"errors" "errors"
"io" "io"
"log/slog"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"os" "os"
@ -255,7 +256,7 @@ func (c *cache) writeHeader(ctx context.Context, resp *http.Response) (err error
return err return err
} }
lg.FromContext(ctx).Info("Updated download cache (header only)", lga.Dir, c.dir, lga.Resp, resp) lg.FromContext(ctx).Info("Updated download main cache (header only)", lga.Dir, mainDir, lga.Resp, resp)
return nil return nil
} }
@ -282,6 +283,9 @@ func (c *cache) newResponseCacher(ctx context.Context, resp *http.Response) (*re
return nil, err return nil, err
} }
log := lg.FromContext(ctx)
log.Debug("Wrote response header to staging cache", lga.Dir, c.dir, lga.Resp, resp)
var f *os.File var f *os.File
if f, err = os.Create(filepath.Join(stagingDir, "body")); err != nil { if f, err = os.Create(filepath.Join(stagingDir, "body")); err != nil {
_ = resp.Body.Close() _ = resp.Body.Close()
@ -289,6 +293,7 @@ func (c *cache) newResponseCacher(ctx context.Context, resp *http.Response) (*re
} }
r := &responseCacher{ r := &responseCacher{
log: log,
stagingDir: stagingDir, stagingDir: stagingDir,
mainDir: filepath.Join(c.dir, "main"), mainDir: filepath.Join(c.dir, "main"),
body: resp.Body, body: resp.Body,
@ -310,6 +315,7 @@ var _ io.ReadCloser = (*responseCacher)(nil)
// consumer of responseCacher will not receive [io.EOF] unless the cache is // consumer of responseCacher will not receive [io.EOF] unless the cache is
// successfully promoted. // successfully promoted.
type responseCacher struct { type responseCacher struct {
log *slog.Logger
body io.ReadCloser body io.ReadCloser
closeErr *error closeErr *error
f *os.File f *os.File
@ -404,8 +410,13 @@ func (r *responseCacher) cachePromote() error {
} }
}() }()
err := r.f.Close() fi, err := r.f.Stat()
if err != nil {
return errz.Wrap(err, "failed to stat staging cache body file")
}
fpBody := r.f.Name() fpBody := r.f.Name()
err = r.f.Close()
r.f = nil r.f = nil
if err != nil { if err != nil {
return errz.Wrap(err, "failed to close cache body file") return errz.Wrap(err, "failed to close cache body file")
@ -432,6 +443,7 @@ func (r *responseCacher) cachePromote() error {
} }
r.stagingDir = "" r.stagingDir = ""
r.log.Info("Promoted download staging cache to main", lga.Size, fi.Size(), lga.Dir, r.mainDir)
return nil return nil
} }