From 10794b51657066ad7b45ed818dd394c307c1f35b Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Sat, 11 May 2024 10:09:11 +0800 Subject: [PATCH 1/2] fix: divide by zero --- retrievalprovider/httpretrieval/server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/retrievalprovider/httpretrieval/server.go b/retrievalprovider/httpretrieval/server.go index 82343b7a..54ae4cdd 100644 --- a/retrievalprovider/httpretrieval/server.go +++ b/retrievalprovider/httpretrieval/server.go @@ -179,7 +179,11 @@ func serveContent(w http.ResponseWriter, r *http.Request, content io.ReadSeeker, // Write a line to the log end := time.Now() total, count := writeErrWatcher.total, writeErrWatcher.count - avg := total / count + var avg uint64 + if count != 0 { + avg = total / count + } + completeMsg := fmt.Sprintf("GET %s\t%s - %s: %s / %s transferred", r.URL, end.Format(time.RFC3339), start.Format(time.RFC3339), time.Since(start), fmt.Sprintf("total %s (%d B), average write %s ", types.SizeStr(types.NewInt(total)), total, types.SizeStr(types.NewInt(avg)))) From 7931fef3a0c046e194d6958d53a11655c634596f Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Sat, 11 May 2024 11:18:54 +0800 Subject: [PATCH 2/2] fix: avoid getting the wrong readers --- piecestorage/store_wrapper.go | 18 ++++++++++++------ retrievalprovider/httpretrieval/server.go | 4 ++++ retrievalprovider/httpretrieval/server_test.go | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/piecestorage/store_wrapper.go b/piecestorage/store_wrapper.go index 0133c702..5ee8ca43 100644 --- a/piecestorage/store_wrapper.go +++ b/piecestorage/store_wrapper.go @@ -57,9 +57,12 @@ func (sw *storeWrapper) GetReaderCloser(ctx context.Context, s string) (io.ReadC var rc io.ReadCloser var err error for _, name := range extendPiece(s) { - rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name) - if err == nil { - return rc, nil + has, _ := sw.IPieceStorage.Has(ctx, name) + if has { + rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name) + if err == nil { + return rc, nil + } } } @@ -70,9 +73,12 @@ func (sw *storeWrapper) GetMountReader(ctx context.Context, s string) (mount.Rea var reader mount.Reader var err error for _, name := range extendPiece(s) { - reader, err = sw.IPieceStorage.GetMountReader(ctx, name) - if err == nil { - return reader, nil + has, _ := sw.IPieceStorage.Has(ctx, name) + if has { + reader, err = sw.IPieceStorage.GetMountReader(ctx, name) + if err == nil { + return reader, nil + } } } diff --git a/retrievalprovider/httpretrieval/server.go b/retrievalprovider/httpretrieval/server.go index 54ae4cdd..e23df4a6 100644 --- a/retrievalprovider/httpretrieval/server.go +++ b/retrievalprovider/httpretrieval/server.go @@ -106,6 +106,10 @@ func (s *Server) retrievalByPieceCID(w http.ResponseWriter, r *http.Request) { } defer mountReader.Close() // nolint + var buf [32]byte + _, _ = mountReader.Read(buf[:]) + fmt.Println("xxxx", string(buf[:])) + contentReader, err := handleRangeHeader(r.Header.Get("Range"), mountReader, len) if err != nil { log.Warnf("handleRangeHeader failed, Range: %s, error: %v", r.Header.Get("Range"), err) diff --git a/retrievalprovider/httpretrieval/server_test.go b/retrievalprovider/httpretrieval/server_test.go index 7455567c..d02eca63 100644 --- a/retrievalprovider/httpretrieval/server_test.go +++ b/retrievalprovider/httpretrieval/server_test.go @@ -80,8 +80,9 @@ func TestRetrievalByPiece(t *testing.T) { piece, err := cid.Decode(pieceStr) assert.NoError(t, err) buf := &bytes.Buffer{} - f, err := os.Create(filepath.Join(tmpDri, pieceStr)) + f, err := os.Create(filepath.Join(tmpDri, pieceStr+".car")) assert.NoError(t, err) + assert.NoError(t, os.MkdirAll(filepath.Join(tmpDri, pieceStr), os.ModePerm)) for i := 0; i < 100; i++ { buf.WriteString("TEST TEST\n") }