Open dougnukem opened 8 years ago
Looking at httpcontrol
Seems like there's a good example of intercepting an HTTP request and redirecting the response or also using the real response (e.g. if Rate limit is exceeded).
// A cache enabled RoundTrip.
func (t *Transport) RoundTrip(req *http.Request) (res *http.Response, err error) {
key := t.Config.Key(req)
var entry cacheEntry
// from cache
if key != "" {
raw, err := t.ByteCache.Get(key)
if err != nil {
return nil, err
}
if raw != nil {
if err = json.Unmarshal(raw, &entry); err != nil {
return nil, err
}
// setup fake http.Response
res = entry.Response
res.Body = ioutil.NopCloser(bytes.NewReader(entry.Body))
res.Request = req
return res, nil
}
}
// real request
res, err = t.Transport.RoundTrip(req)
if err != nil {
return nil, err
}
// no caching required
if key == "" {
return res, nil
}
// fully buffer response for caching purposes
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
// remove properties we want to skip in serialization
res.Body = nil
res.Request = nil
// serialize the cache entry
entry.Response = res
entry.Body = body
raw, err := json.Marshal(&entry)
if err != nil {
return nil, err
}
// put back non serialized properties
res.Body = ioutil.NopCloser(bytes.NewReader(body))
res.Request = req
// determine timeout & put it in cache
timeout := t.Config.MaxAge(res)
if timeout != 0 {
if err = t.ByteCache.Store(key, raw, timeout); err != nil {
return nil, err
}
}
// reset body in case the config.Timeout logic consumed it
res.Body = ioutil.NopCloser(bytes.NewReader(body))
return res, nil
}
Overview
It'd be nice if this library supported a way for
http.Client
outgoing requests to utilizinglimiter.Limit
to rate limit based on configured API rate limits useful for things like:Features
http.Client
requests and map to alimit.Limiter
which could then have some policy callback to let the clientX-RateLimit-*
it'd be useful if there was a way to calllimiter.Limiter.Set(key string, c *Context)
to attempt to keep in sync with server stateI think you'd want to be able to configure a
limit.Limiter
per URL request path e.g.:/1.1/users/user_timeline.json
Similar to how server-side HTTP handlers are setup:
Maybe even use
http.NewServeMux
to client-side proxy rate limit responses?There's some example of it here: