func clientExample() error { resp, err := http.Get("http://127.0.0.1:8000/") if err != nil { return err } defer resp.Body.Close() bucket := ratelimit.NewFromRate(10*1024, 10*1024, 0) wrappedReader := ratelimit.NewReader(resp.Body, bucket) if _, err := ioutil.ReadAll(wrappedReader); err != nil { return err } return nil }
func (h *httpDownloader) Download(u *url.URL, dst string) (resp *http.Response, err error) { defer func() { <-h.ch }() if h.client == nil { h.client = http.DefaultClient } req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return } resp, err = h.client.Do(req) if err != nil { return } defer resp.Body.Close() if resp.StatusCode != 200 { return resp, fmt.Errorf("downloader error downloading %s: got http status %s", u, resp.Status) } os.MkdirAll(path.Dir(dst), 0755) f, err := os.Create(dst) if err != nil { return } defer f.Close() var src io.Reader = resp.Body if h.bucket != nil { src = ratelimit.NewReader(resp.Body, h.bucket) } _, err = io.Copy(f, src) return }