duangou1953 2018-02-13 00:48
浏览 60

将自定义运输添加到第三方程序包

I have been using GoRequest as a package in my Go application.

I use it as it helps make all the API calls I need to make a lot cleaner - one thing that it is lacking, that I am able to do with a regular http.Client is implement outbound rate limiting on the Transport.

For example, in one of my applications I use this -

type rateLimitTransport struct {
    limiter *rate.Limiter
    xport   http.RoundTripper
}

var _ http.RoundTripper = &rateLimitTransport{}

func newRateLimitTransport(r float64, xport http.RoundTripper) http.RoundTripper {
    return &rateLimitTransport{
        limiter: rate.NewLimiter(rate.Limit(r), 1),
        xport:   xport,
    }
}

func (t *rateLimitTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    t.limiter.Wait(r.Context())
    return t.xport.RoundTrip(r)
}

var myClient = http.Client{
    // Use a rate-limiting transport which falls back to the default
    Transport: newRateLimitTransport(1, http.DefaultTransport),
}

This allows my to create a client, that is rate limited to 1 request per second; 60 requests per minute.

I have forked the GoRequest package and am trying to add a new method to it called SetRateLimit() which should take the rate per second as an argument and use this to implement a rate limited transport.

My attempt so far looks like this:

import (
    //...current imports
    "golang.org/x/time/rate"
)
//... Rest of the package
type rateLimitTransport struct {
    limiter *rate.Limiter
    xport   http.RoundTripper
}

var _ http.RoundTripper = &rateLimitTransport{}

func newRateLimitTransport(r float64, xport http.RoundTripper) http.RoundTripper {
    return &rateLimitTransport{
        limiter: rate.NewLimiter(rate.Limit(r), 1),
        xport:   xport,
    }
}

func (t *rateLimitTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    t.limiter.Wait(r.Context())
    return t.xport.RoundTrip(r)
}

func (s *SuperAgent) SetRateLimit(limit float64) *SuperAgent {

    s.Transport = newRateLimitTransport(limit, http.DefaultTransport)
    return s
}

However I get an error when trying to build this:

cannot use newRateLimitTransport(limit, http.DefaultTransport) (type http.RoundTripper) as type *http.Transport in assignment: need type assertion

I've been looking at this for hours, and I don't quite understand how this works for a regular http.Client but doesn't work for this package.

Can someone please help me resolve the issue above and add rate limiting to this package?

Update - SuperRequest struct

// A SuperAgent is a object storing all request data for client.
type SuperAgent struct {
    Url               string
    Method            string
    Header            http.Header
    TargetType        string
    ForceType         string
    Data              map[string]interface{}
    SliceData         []interface{}
    FormData          url.Values
    QueryData         url.Values
    FileData          []File
    BounceToRawString bool
    RawString         string
    Client            *http.Client
    Transport         *http.Transport
    Cookies           []*http.Cookie
    Errors            []error
    BasicAuth         struct{ Username, Password string }
    Debug             bool
    CurlCommand       bool
    logger            Logger
    Retryable         struct {
        RetryableStatus []int
        RetryerTime     time.Duration
        RetryerCount    int
        Attempt         int
        Enable          bool
    }
    //If true prevents clearing Superagent data and makes it possible to reuse it for the next requests
    DoNotClearSuperAgent bool
}
  • 写回答

1条回答 默认 最新

  • dpj775835868 2018-02-13 02:40
    关注

    Error you are seeing says the issue.

    cannot use newRateLimitTransport(limit, http.DefaultTransport) (type http.RoundTripper) as type *http.Transport in assignment: need type assertion

    SuperAgent.Transport type is *http.Transport But you are trying to assign http.RoundTripper to that field

    To fix this you could change the SuperaAgent

    type SuperAgent{
    ...
    Transport  http.RoundTripper
    ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像