From 21871a387cbf1dc9a8355cc9c7ddf9605cb62321 Mon Sep 17 00:00:00 2001 From: Yonah Date: Thu, 18 Sep 2025 06:06:46 +0900 Subject: [PATCH] generic: return content length alongside body This increases efficiency for bigger response bodies. Signed-off-by: Yonah --- cmd/msrfetch/net.go | 8 ++++---- generic.go | 2 +- song.go | 2 +- song_test.go | 14 +++++++------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/msrfetch/net.go b/cmd/msrfetch/net.go index 47022e8..805f22c 100644 --- a/cmd/msrfetch/net.go +++ b/cmd/msrfetch/net.go @@ -10,12 +10,12 @@ type netDirect struct { c http.Client } -func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, error) { +func (n *netDirect) Get(ctx context.Context, url string) (io.ReadCloser, int64, error) { var resp *http.Response if req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil); err != nil { - return nil, err + return nil, -2, err } else if resp, err = n.c.Do(req); err != nil { - return nil, err + return nil, -2, err } - return resp.Body, nil + return resp.Body, resp.ContentLength, nil } diff --git a/generic.go b/generic.go index 2df3c4c..91dacf2 100644 --- a/generic.go +++ b/generic.go @@ -16,7 +16,7 @@ const ( type Net interface { // Get makes a get request to url and returns the response body. // The caller must close the body after it finishes reading from it. - Get(ctx context.Context, url string) (body io.ReadCloser, err error) + Get(ctx context.Context, url string) (body io.ReadCloser, contentLength int64, err error) } // Response is a generic API response. diff --git a/song.go b/song.go index 1c0ad35..9d3ec94 100644 --- a/song.go +++ b/song.go @@ -47,7 +47,7 @@ func (s *Song) Enrich(ctx context.Context, n Net) error { } var v SongResponse - if body, err := n.Get(ctx, APIPrefix+"/song/"+s.CID.String()); err != nil { + if body, _, err := n.Get(ctx, APIPrefix+"/song/"+s.CID.String()); err != nil { return err } else { if err = json.NewDecoder(body).Decode(&v); err != nil { diff --git a/song_test.go b/song_test.go index 83f1dbb..1909326 100644 --- a/song_test.go +++ b/song_test.go @@ -168,22 +168,22 @@ func TestSongEnrich(t *testing.T) { type stubNetSongEnrichErrorCloser stubNetSongEnrich -func (n stubNetSongEnrichErrorCloser) Get(ctx context.Context, url string) (io.ReadCloser, error) { - r, err := stubNetSongEnrich(n).Get(ctx, url) +func (n stubNetSongEnrichErrorCloser) Get(ctx context.Context, url string) (io.ReadCloser, int64, error) { + r, l, err := stubNetSongEnrich(n).Get(ctx, url) if r != nil { r = errorCloser{r} } - return r, err + return r, l, err } type stubNetSongEnrich map[StringInt][]byte -func (n stubNetSongEnrich) Get(_ context.Context, url string) (io.ReadCloser, error) { +func (n stubNetSongEnrich) Get(_ context.Context, url string) (io.ReadCloser, int64, error) { if i, err := strconv.Atoi(strings.TrimPrefix(url, APIPrefix+"/song/")); err != nil { - return nil, err + return nil, -2, err } else if b, ok := n[StringInt(i)]; !ok { - return nil, fmt.Errorf("song cid %d requested, but is not present", i) + return nil, -2, fmt.Errorf("song cid %d requested, but is not present", i) } else { - return nopCloser{bytes.NewReader(b)}, nil + return nopCloser{bytes.NewReader(b)}, int64(len(b)), nil } }