Skip to content

Commit

Permalink
use report API
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Aug 17, 2024
1 parent 29265d0 commit d436d88
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cluster/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ func (cr *Cluster) RequestCert(ctx context.Context) (ckp *CertKeyPair, err error
return
}

func (cr *Cluster) ReportDownload(ctx context.Context, request *http.Request, err error) error {
func (cr *Cluster) ReportDownload(ctx context.Context, response *http.Response, err error) error {
type ReportPayload struct {
Urls []string `json:"urls"`
Error utils.EmbedJSON[struct{ Message string }] `json:"error"`
}
var payload ReportPayload
redirects := utils.GetRedirects(request)
redirects := utils.GetRedirects(response.Request)
payload.Urls = make([]string, len(redirects))
for i, u := range redirects {
payload.Urls[i] = u.String()
Expand Down
23 changes: 18 additions & 5 deletions cluster/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ type FileInfo struct {

type RequestPath struct {
*http.Request
Path string
Cluster *Cluster
Path string
}

type StorageFileInfo struct {
Expand Down Expand Up @@ -153,7 +154,11 @@ func (cr *Cluster) GetFileList(ctx context.Context, fileMap map[string]*StorageF
if err != nil {
return err
}
ff.URLs[req.URL.String()] = RequestPath{Request: req, Path: f.Path}
ff.URLs[req.URL.String()] = RequestPath{
Request: req,
Cluster: cr,
Path: f.Path,
}
fileMap[f.Hash] = ff
}
}
Expand Down Expand Up @@ -548,9 +553,9 @@ func (c *HTTPClient) fetchFile(ctx context.Context, stats *syncStats, f *Storage
}

reqInd := 0
reqs := make([]*http.Request, 0, len(f.URLs))
reqs := make([]RequestPath, 0, len(f.URLs))
for _, rq := range f.URLs {
reqs = append(reqs, rq.Request)
reqs = append(reqs, rq)
}

fileRes := make(chan *os.File, 1)
Expand Down Expand Up @@ -610,10 +615,18 @@ func (c *HTTPClient) fetchFile(ctx context.Context, stats *syncStats, f *Storage
if _, err := fd.Seek(io.SeekStart, 0); err != nil {
return err
}
if err := c.fetchFileWithBuf(ctx, reqs[reqInd], f.Size, hashMethod, f.Hash, fd, buf, func(r io.Reader) io.Reader {
rp := reqs[reqInd]
if err := c.fetchFileWithBuf(ctx, rp.Request, f.Size, hashMethod, f.Hash, fd, buf, func(r io.Reader) io.Reader {
return utils.ProxyPBReader(r, bar, stats.totalBar, &stats.lastInc)
}); err != nil {
reqInd = (reqInd + 1) % len(reqs)
if rerr, ok := err.(*utils.RedirectError); ok {
go func() {
if err := rp.Cluster.ReportDownload(context.WithoutCancel(ctx), rerr.GetResponse(), rerr.Unwrap()); err != nil {
log.Warnf("Report API error: %v", err)
}
}()
}
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/build/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"fmt"
)

const ClusterVersion = "1.10.9"
const ClusterVersion = "1.11.0"

var BuildVersion string = "dev"

Expand Down
6 changes: 6 additions & 0 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,14 @@ func GetRedirects(req *http.Request) []*url.URL {

type RedirectError struct {
Redirects []*url.URL
Response *http.Response
Err error
}

func ErrorFromRedirect(err error, resp *http.Response) *RedirectError {
return &RedirectError{
Redirects: GetRedirects(resp.Request),
Response: resp,
Err: err,
}
}
Expand All @@ -551,6 +553,10 @@ func (e *RedirectError) Error() string {
return b.String()
}

func (e *RedirectError) GetResponse() *http.Response {
return e.Response
}

func (e *RedirectError) Unwrap() error {
return e.Err
}
Expand Down

0 comments on commit d436d88

Please sign in to comment.