fix: check status code, and switch to more light weight http library
This commit is contained in:
parent
7bd9cf11ae
commit
1584c38641
4
go.mod
4
go.mod
|
@ -3,8 +3,8 @@ module git.lumeweb.com/LumeWeb/libs5-go
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/ddo/rq v0.0.0-20190828174524-b3daa55fcaba
|
||||||
github.com/emirpasic/gods v1.18.1
|
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/golang/mock v1.6.0
|
||||||
github.com/google/go-cmp v0.6.0
|
github.com/google/go-cmp v0.6.0
|
||||||
github.com/julienschmidt/httprouter v1.3.0
|
github.com/julienschmidt/httprouter v1.3.0
|
||||||
|
@ -30,8 +30,8 @@ require (
|
||||||
go.uber.org/dig v1.17.0 // indirect
|
go.uber.org/dig v1.17.0 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // 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/sys v0.15.0 // indirect
|
||||||
|
golang.org/x/time v0.3.0 // indirect
|
||||||
golang.org/x/tools v0.6.0 // indirect
|
golang.org/x/tools v0.6.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,10 +12,13 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/structs"
|
"git.lumeweb.com/LumeWeb/libs5-go/structs"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
"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"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,7 +30,6 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type StorageService struct {
|
type StorageService struct {
|
||||||
httpClient *resty.Client
|
|
||||||
metadataCache structs.Map
|
metadataCache structs.Map
|
||||||
providerStore storage.ProviderStore
|
providerStore storage.ProviderStore
|
||||||
service.ServiceBase
|
service.ServiceBase
|
||||||
|
@ -35,7 +37,6 @@ type StorageService struct {
|
||||||
|
|
||||||
func NewStorage(params service.ServiceParams) *StorageService {
|
func NewStorage(params service.ServiceParams) *StorageService {
|
||||||
return &StorageService{
|
return &StorageService{
|
||||||
httpClient: resty.New(),
|
|
||||||
metadataCache: structs.NewMap(),
|
metadataCache: structs.NewMap(),
|
||||||
ServiceBase: service.NewServiceBase(params.Logger, params.Config, params.Db),
|
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()))
|
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 {
|
if err != nil {
|
||||||
err := dlUriProvider.Downvote(dlUri)
|
err := dlUriProvider.Downvote(dlUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -236,7 +243,25 @@ func (s *StorageService) DownloadBytesByHash(hash *encoding.Multihash) ([]byte,
|
||||||
continue
|
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
|
return bodyBytes, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue