fix: check status code, and switch to more light weight http library

This commit is contained in:
Derrick Hammer 2024-02-29 12:25:21 -05:00
parent 7bd9cf11ae
commit 1584c38641
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 32 additions and 7 deletions

4
go.mod
View File

@ -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
)

View File

@ -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
}