From 1584c386419de5a3348df117250fa64511b903fc Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 29 Feb 2024 12:25:21 -0500 Subject: [PATCH] fix: check status code, and switch to more light weight http library --- go.mod | 4 ++-- service/default/storage.go | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index f9e3cc0..323dc9d 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module git.lumeweb.com/LumeWeb/libs5-go go 1.20 require ( + github.com/ddo/rq v0.0.0-20190828174524-b3daa55fcaba github.com/emirpasic/gods v1.18.1 - github.com/go-resty/resty/v2 v2.11.0 github.com/golang/mock v1.6.0 github.com/google/go-cmp v0.6.0 github.com/julienschmidt/httprouter v1.3.0 @@ -30,8 +30,8 @@ require ( go.uber.org/dig v1.17.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect - golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.15.0 // indirect + golang.org/x/time v0.3.0 // indirect golang.org/x/tools v0.6.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/service/default/storage.go b/service/default/storage.go index 55d8380..0cd1b2c 100644 --- a/service/default/storage.go +++ b/service/default/storage.go @@ -12,10 +12,13 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/structs" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" - "github.com/go-resty/resty/v2" + "github.com/ddo/rq" + _ "github.com/ddo/rq" "github.com/vmihailenco/msgpack/v5" "go.etcd.io/bbolt" "go.uber.org/zap" + "io" + "net/http" "time" ) @@ -27,7 +30,6 @@ var ( ) type StorageService struct { - httpClient *resty.Client metadataCache structs.Map providerStore storage.ProviderStore service.ServiceBase @@ -35,7 +37,6 @@ type StorageService struct { func NewStorage(params service.ServiceParams) *StorageService { return &StorageService{ - httpClient: resty.New(), metadataCache: structs.NewMap(), ServiceBase: service.NewServiceBase(params.Logger, params.Config, params.Db), } @@ -223,7 +224,13 @@ func (s *StorageService) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, s.Logger().Debug("Trying to download from", zap.String("url", dlUri.Location().BytesURL())) - res, err := s.httpClient.R().Get(dlUri.Location().BytesURL()) + req := rq.Get(dlUri.Location().BytesURL()) + httpReq, err := req.ParseRequest() + if err != nil { + return nil, err + } + + res, err := http.DefaultClient.Do(httpReq) if err != nil { err := dlUriProvider.Downvote(dlUri) if err != nil { @@ -236,7 +243,25 @@ func (s *StorageService) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, continue } - bodyBytes := res.Body() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + s.Logger().Error("error closing body", zap.Error(err)) + } + }(res.Body) + + if res.StatusCode != 200 { + err := dlUriProvider.Downvote(dlUri) + if err != nil { + return nil, err + } + continue + } + + bodyBytes, err := io.ReadAll(res.Body) + if err != nil { + return nil, err + } return bodyBytes, nil }