MCPcopy Create free account
hub / github.com/Azure/peerd / downloadSASURL

Function downloadSASURL

tests/random/random.go:189–257  ·  view source on GitHub ↗

downloadSASURL downloads a SAS URL and returns the number of bytes downloaded.

(l *zerolog.Logger, sasURL string, readsPerBlob int)

Source from the content-addressed store, hash-verified

187
188// downloadSASURL downloads a SAS URL and returns the number of bytes downloaded.
189func downloadSASURL(l *zerolog.Logger, sasURL string, readsPerBlob int) ([]float64, int, error) {
190 failures := -1
191 req, err := http.NewRequest("HEAD", sasURL, nil)
192 if err != nil {
193 return nil, failures, err
194 }
195
196 resp, err := client.Do(req)
197 if err != nil {
198 return nil, failures, err
199 } else if resp.StatusCode != http.StatusOK {
200 return nil, failures, fmt.Errorf("unexpected status code %d", resp.StatusCode)
201 }
202 blobSize, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
203
204 l.Info().Int64("size", blobSize).Int("readsPerBlob", readsPerBlob).Msg("downloading blob")
205
206 errs := []error{}
207 speeds := []float64{}
208 failures = 0
209
210 for i := 0; i < readsPerBlob; i++ {
211 req, err = http.NewRequest("GET", sasURL, nil)
212 if err != nil {
213 return speeds, failures, err
214 }
215
216 // Set a random Range header
217 s, _ := rand.Int(rand.Reader, big.NewInt(int64(blobSize)))
218 start := s.Int64()
219 e, _ := rand.Int(rand.Reader, big.NewInt(int64(blobSize-start+1)))
220 end := start + e.Int64()
221 contentRange := fmt.Sprintf("bytes=%d-%d", start, end)
222 req.Header.Set("Range", contentRange)
223
224 l2 := l.With().Int("size", int(blobSize)).Str("url", sasURL).Str("http.request.range", contentRange).Logger()
225
226 st := time.Now()
227 resp, err = client.Do(req)
228 if err != nil {
229 continue
230 }
231 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
232 l2.Error().Err(err).Int("status", resp.StatusCode).Msg("unexpected status code")
233 errs = append(errs, fmt.Errorf("unexpected status code %d", resp.StatusCode))
234 failures++
235 continue
236 }
237
238 defer func() {
239 if err := resp.Body.Close(); err != nil {
240 l2.Error().Err(err).Msg("error closing response body")
241 }
242 }()
243 n, err := io.Copy(io.Discard, resp.Body)
244 if err != nil {
245 l2.Error().Err(err).Msg("error reading response body")
246 errs = append(errs, err)

Callers 1

downloadSASURLsFunction · 0.85

Calls 5

SetMethod · 0.80
CopyMethod · 0.80
sleepFunction · 0.70
CloseMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected