MCPcopy Create free account
hub / github.com/cph6/zsync / fetchRemainingBlocksFromURL

Method fetchRemainingBlocksFromURL

target_fetch.go:82–128  ·  view source on GitHub ↗
(client HTTPRequester, rawURL, referer string)

Source from the content-addressed store, hash-verified

80}
81
82func (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
130func (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)

Callers 1

FetchRemainingBlocksMethod · 0.95

Calls 2

NeededByteRangesMethod · 0.95
fetchRangeMethod · 0.95

Tested by

no test coverage detected