~mendelmaleh/download

ref: d367236e0766f5511a5845f6fd923c6742c0c8e3 download/utils.go -rw-r--r-- 677 bytes
d367236eMendel E Initial commit 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package download

import (
	"bytes"
	"errors"
	"io"
	"net"
	"time"
)

func (dl *DL) try(url string, attempts int) (buf *bytes.Buffer, err error) {
	// if attempts <= 0, return an empty buffer
	if attempts <= 0 {
		buf = &bytes.Buffer{}
		return
	}

	for i := 0; i < attempts; i++ {
		buf, err = dl.dl(url)
		if err == nil {
			return
		}

		var e *net.DNSError
		if errors.As(err, &e) && e.Temporary() {
			time.Sleep(10 * time.Millisecond)
		}
	}

	return
}

func (dl *DL) dl(url string) (buf *bytes.Buffer, err error) {
	resp, err := dl.Client.Get(url)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	buf = &bytes.Buffer{}
	_, err = io.Copy(buf, resp.Body)
	return
}