Search code examples
httpgobandwidth-throttling

Limiting bandwidth of http get


I'm a beginner to golang.

Is there any way to limit golang's http.Get() bandwidth usage? I found this: http://godoc.org/code.google.com/p/mxk/go1/flowcontrol, but I'm not sure how to piece the two together. How would I get access to the http Reader?


Solution

  • There is an updated version of the package on github

    You use it by wrapping an io.Reader

    Here is a complete example which will show the homepage of Google veeeery sloooowly.

    This wrapping an interface to make new functionality is very good Go style, and you'll see a lot of it in your journey into Go.

    package main
    
    import (
        "io"
        "log"
        "net/http"
        "os"
    
        "github.com/mxk/go-flowrate/flowrate"
    )
    
    func main() {
        resp, err := http.Get("http://google.com")
        if err != nil {
            log.Fatalf("Get failed: %v", err)
        }
        defer resp.Body.Close()
    
        // Limit to 10 bytes per second
        wrappedIn := flowrate.NewReader(resp.Body, 10)
    
        // Copy to stdout
        _, err = io.Copy(os.Stdout, wrappedIn)
        if err != nil {
            log.Fatalf("Copy failed: %v", err)
        }
    }