Code Monkey home page Code Monkey logo

Comments (1)

dougnukem avatar dougnukem commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.