dre26973 2015-01-09 21:52
浏览 201
已采纳

限制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?

  • 写回答

2条回答 默认 最新

  • dongshengyin0147 2015-01-10 00:43
    关注

    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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘