Comments (1)
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
}
from limiter.
Related Issues (20)
- Change limiter rate on the fly HOT 2
- Is there any way to skip rate limit and keep service available even store crashed? HOT 2
- Does this project support redigo HOT 2
- Builds fail when using gomod HOT 2
- work behind load balancer HOT 4
- Should be flexible enough to support advanced use-cases HOT 4
- Per-route limiting HOT 2
- Reintroduce Ping function to the Client interface HOT 3
- Feature Request: Please can you add rqlite as store HOT 2
- Limit for every 5 minute HOT 1
- Please consider adding SkipList option HOT 1
- Rate limiting based on custom identity HOT 2
- ERR bad lua script for redis cluster HOT 1
- Which time windows does this limiter use? HOT 1
- Custom keys? HOT 1
- limit of dynamic route HOT 1
- Should use the rightmost ip in x-forwarded-for, if used at all HOT 7
- another dynamic limit need HOT 9
- Limiters with momery store are not working as expected HOT 3
- A key can be limited forever HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from limiter.