(client HTTPRequester, rawURL, referer string)
| 80 | } |
| 81 | |
| 82 | func (zs *Syncer) fetchRemainingBlocksFromURL(client HTTPRequester, rawURL, referer string) (int64, error) { |
| 83 | u, err := url.Parse(rawURL) |
| 84 | if err != nil { |
| 85 | return 0, fmt.Errorf("invalid URL %s: %w", rawURL, err) |
| 86 | } |
| 87 | var absURL *url.URL |
| 88 | if !u.IsAbs() { |
| 89 | if referer == "" { |
| 90 | return 0, fmt.Errorf("URL '%s' from the .zsync file is relative, but no referer URL is known", rawURL) |
| 91 | } |
| 92 | base, err := url.Parse(referer) |
| 93 | if err != nil { |
| 94 | return 0, err |
| 95 | } |
| 96 | absURL = base.ResolveReference(u) |
| 97 | } else { |
| 98 | absURL = u |
| 99 | } |
| 100 | |
| 101 | ranges := zs.NeededByteRanges() |
| 102 | if len(ranges) == 0 { |
| 103 | return 0, nil |
| 104 | } |
| 105 | |
| 106 | g, ctx := errgroup.WithContext(context.Background()) |
| 107 | g.SetLimit(3) |
| 108 | |
| 109 | var ( |
| 110 | httpBytesDownloaded int64 |
| 111 | mu sync.Mutex |
| 112 | ) |
| 113 | |
| 114 | for _, r := range ranges { |
| 115 | g.Go(func() error { |
| 116 | bytesReceived, err := zs.fetchRange(ctx, client, absURL, r) |
| 117 | // Lock protecting httpBytesDownloaded. |
| 118 | mu.Lock() |
| 119 | defer mu.Unlock() |
| 120 | httpBytesDownloaded += bytesReceived |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | return nil |
| 125 | }) |
| 126 | } |
| 127 | return httpBytesDownloaded, g.Wait() |
| 128 | } |
| 129 | |
| 130 | func (zs *Syncer) fetchRange(ctx context.Context, client HTTPRequester, url *url.URL, r ByteRange) (int64, error) { |
| 131 | req, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil) |
no test coverage detected