proxy.py icon indicating copy to clipboard operation
proxy.py copied to clipboard

[CacheResponsesPlugin] Support serving out of cache

Open abhinavsingh opened this issue 6 years ago • 19 comments

Currently cache_responses.py plugin only caches but doesn't serves out of cached data.

Per @trianta2 request here it will be a good idea to support this in future releases.

However, for a production grade usage, this feature will require significant work. If someone is interested in taking a stab at this one, please feel free to reach out. Happy to discuss it further.

abhinavsingh avatar Mar 24 '20 16:03 abhinavsingh

On the outset it appears as a straight forward approach to map the cached content to the saved url.

Is that the only thing that needs to be done? Can you elaborate the production grade requirement?

ja8zyjits avatar Apr 27 '20 16:04 ja8zyjits

Cache invalidation is the key for production grade caching solutions. E.g. Upstream servers will serve a URL with some cache control/expiry headers. An ideal cache server must invalidate cache and fetch/serve fresh response after cache expiry. Over the course of HTML specification upgrades, there are just too many cache headers to take care of e.g. vary, etag, ... etc

abhinavsingh avatar Apr 27 '20 18:04 abhinavsingh

That would mean replicating the entire HTTP cache protocol with active support for specification changes over every new releases?

ja8zyjits avatar Apr 27 '20 20:04 ja8zyjits

Not necessarily. We can start adding subset of features (enabled explicitly via flags) as needed by the community.

Probably also a good idea to add references to section(s) of RFC that cache plugin implements in the documentation.

Happy to discuss if you have any particular scenario in mind.

abhinavsingh avatar Apr 27 '20 21:04 abhinavsingh

If we start small, from where must we begin? By RFC did you mean this https://tools.ietf.org/html/rfc7234 ?

ja8zyjits avatar Apr 28 '20 21:04 ja8zyjits

@ja8zyjits Yep that's the RFC we can follow. Ideally, we must pick a use case that your application currently needs. If you would like to start working towards it generically, I'll recommend following steps:

  1. Start reading the RFC as much as you can :) Especially go through section 2, 3, 4, 5. Section 2 and 3 are rather short introductions. Section 4 describes various scenarios and strategies. Section 5 provide details about various cache headers and how to handle them.

  2. For implementation pick a use case from section 5. E.g. handling header fields like Age, Cache-Control, max-age, no-store, no-cache, must-revalidate, ... etc

Let me know where you would like to start. Thank you!!!

abhinavsingh avatar May 01 '20 07:05 abhinavsingh

@abhinavsingh sure, would look into them. Meanwhile to test these things, I must make the TLS interception work. Would keep you posted.

ja8zyjits avatar May 11 '20 13:05 ja8zyjits

What's the point to store chunks and never deliver them for the same given request ? I guess it's most a logging plugin instead of caching one :)

anthonykgross avatar May 20 '20 04:05 anthonykgross

What's the point to store chunks and never deliver them for the same given request ? I guess it's most a logging plugin instead of caching one :)

Correct, unless we start to dispatch back responses to the client, it's a logging plugin, so yes CacheResponsesPlugin is actually LogResponsesPlugin :)

However, how to start serving response back out of cache is precisely what @ja8zyjits is working towards. We have discussed in some detail why CacheResponsesPlugin cannot start serving responses out of cache. Yes, we can serve cached content blindly for test cases but not yet ideal for real life traffic.

abhinavsingh avatar May 20 '20 08:05 abhinavsingh

Just my 2 cents... By definition caching felt like it should actually cache the results. But feels like instead of caching, it just dumps the data like tcpdump(correct me if caching plugin actually does something...), which has nothing to do with actual caching.

Even tho it feels logical to respect caching policies and headers in HTTP proto to handle caching in proxy server, for my usecase, it would be wonderful to have KV-based caching. This is why I started to use this product in the first place. Attempted to use... and failed. Feels like I'll need to write my own plugin then.

iorlas avatar Jun 22 '20 18:06 iorlas

Hi Denis,

Correct, CacheResponsesPlugin like any other plugin within proxy.py distribution is merely an example.

Existing plugin can be expanded upon to add application specific functionalities. E.g. CacheResponsesPlugin is built on top of BaseStore (abstract layer for caching backends). You can write your own for custom backends.

Let me know how it goes.

Thank you Abhinav

On Tue, Jun 23, 2020, 12:10 AM Denis Tomilin [email protected] wrote:

Just my 2 cents... By definition caching felt like it should actually cache the results. But feels like instead of caching, it just dumps the data like tcpdump(correct me if caching plugin actually does something...), which has nothing to do with actual caching.

Even tho it feels logical to respect caching policies and headers in HTTP proto to handle caching in proxy server, for my usecase, it would be wonderful to have KV-based caching. This is why I started to use this product in the first place. Attempted to use... and failed. Feels like I'll need to write my own plugin then.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/abhinavsingh/proxy.py/issues/319#issuecomment-647703795, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA6Y4MSKZRAFT2ZD3QW4U3RX6QRZANCNFSM4LSZWTAA .

abhinavsingh avatar Jun 23 '20 04:06 abhinavsingh

Hi Denis,

Normal KV store cache breaks the site, specially when their js scripts

expires. I have tried it and it fails. We need something that works like squid. Caches but also follows the protocols. Unfortunately some sites would not allow any caching eg: YouTube. They use all protocols to expire caches or not let them be stored.

On Tue, 23 Jun 2020, 00:10 Denis Tomilin, [email protected] wrote:

Just my 2 cents... By definition caching felt like it should actually cache the results. But feels like instead of caching, it just dumps the data like tcpdump(correct me if caching plugin actually does something...), which has nothing to do with actual caching.

Even tho it feels logical to respect caching policies and headers in HTTP proto to handle caching in proxy server, for my usecase, it would be wonderful to have KV-based caching. This is why I started to use this product in the first place. Attempted to use... and failed. Feels like I'll need to write my own plugin then.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/abhinavsingh/proxy.py/issues/319#issuecomment-647703795, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSGU6IEUBQAM22A4M4HCCLRX6QR3ANCNFSM4LSZWTAA .

ja8zyjits avatar Jun 23 '20 07:06 ja8zyjits

Hello, For automatic testing purposes, I modified the CacheResponsesPlugin so that it can serve response from the cache. However, I did not implement any requirements of the RFC, since I want a fix response, as I do not want my tests to break when upstream modifies the response. The implementation I did is able to answer GET and POST requests over HTTP and HTTPS. It uses a very basic mechanism for request storage (I wanted the requests to be stored as text so that it is simple to read), but POST request bodies are hashed (for compactness). Would you like me to propose my implementation? Note I think it requires a few modifications to the code in proxy\http\proxy\server.py (at least in master branch, I did not test develop yet) since I want to be able to work without upstream server. Best regards, Pascal

pasccom avatar Aug 14 '20 10:08 pasccom

@pasccom For test cases, I previously took a stab at similar feature. Idea was to record and replay responses by enabling CacheResponsesPlugin on the fly during test execution. We can probably revisit it and build on top of it OR start a fresh implementation.

See https://github.com/abhinavsingh/proxy.py/blob/develop/proxy/testing/test_case.py#L80 and https://github.com/abhinavsingh/proxy.py/blob/develop/tests/testing/test_embed.py#L56. It's pretty incomplete and will require more work for e2e correctness. Probably you already implemented some of those feature sets.

Internally I envisioned something like this:

  1. Users can enable VCR functionality during test execution.
  2. When enabled, VCR also enabled CacheResponsesPlugin.
  3. Instead of simply caching, now CacheResponsesPlugin also serves back cached response during execution of tests.
  4. Optionally, when enabling VCR, user can configure it e.g. path to cache directory, cache expiry etc.

Please propose your implementation in a PR. I would like to better understand reasons behind modifications within server.py. I feel, with above mentioned strategy a standalone plugin (clone of CacheResponsesPlugin) can serve all the functionalities.

Best

abhinavsingh avatar Aug 14 '20 12:08 abhinavsingh

Also see related issue tracker for VCR functionality https://github.com/abhinavsingh/proxy.py/issues/184

abhinavsingh avatar Aug 14 '20 12:08 abhinavsingh

Thinking of it, serving out of cache for test cases can also be a clone of MockRestApiPlugin. Whether this plugin serve from cached responses or from developer provided API template, it will still serve same purpose for assertions in the test cases. See https://github.com/abhinavsingh/proxy.py#mockrestapiplugin

Throwing a few options that can inspire implementation of this feature set 👍

abhinavsingh avatar Aug 14 '20 12:08 abhinavsingh

Please propose your implementation in a PR. I would like to better understand reasons behind modifications within server.py. I feel, with above mentioned strategy a standalone plugin (clone of CacheResponsesPlugin) can serve all the functionalities.

For HTTP requests, yes, it does. But, I needed to intercept HTTPS requests, which did not work. I will test with develop version instead of v2.2.0. Maybe the bugs have been fixed. I will also have a deeper look in MockRestApiPlugin as you suggest.

I will test my implementation on the develop branch and then send a few PRs with my implementation. Then we should be able to implement other cache management features.

pasccom avatar Aug 14 '20 12:08 pasccom

Hello, I think I have a first version of the full cache. I pushed the changes on my fork. Any comments are welcome.

I think there is still some work to be done for it to work correctly (even for HTTP requests):

  • I did not implement ENABLED feature
  • I noticed that sometimes the host of a request is not set, which will break the code. I have to check when this occurs.

I will write some more thorough tests tomorrow, implement the missing features and create a PR. By the way, are there examples of tests with pipelined HTTP requests? I would like to reuse the code.

pasccom avatar Aug 17 '20 19:08 pasccom

@pasccom Cool.

  • We got a few pipelined requests test cases, but they were written mostly for HttpParser. https://github.com/abhinavsingh/proxy.py/blob/c884338f428408221ff48725285d992ff4a8d0a5/tests/http/test_http_parser.py#L447-L480
  • ENABLED is ok to drop if we have an alternative
  • Regarding missing host headers, per spec, dropping such requests is the right thing to do. See screenshot below:
Screen Shot 2020-08-18 at 9 48 22 AM

abhinavsingh avatar Aug 18 '20 04:08 abhinavsingh