Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Support for response caching #36

Closed
jkonecki opened this issue Jan 24, 2019 · 21 comments
Closed

Support for response caching #36

jkonecki opened this issue Jan 24, 2019 · 21 comments
Labels
enhancement New feature or request
Milestone

Comments

@jkonecki
Copy link
Contributor

I would like to cache responses received from upstream servers based on standard http caching headers.

My scenario involves some slow-changing reference data that could be cached by ProxyKit and save a network trip to the upstream server.

It would be nice to be able to enable caching for individual routes, but in my scenario that isn't necessary.

Happy to contribute when given some guidelines.

@damianh
Copy link
Collaborator

damianh commented Jan 25, 2019

Good idea! Wonder if I can leverage something existing to do the work i.e. CacheCow

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 25, 2019 via email

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 25, 2019

I've done a spike and the caching can be simply implemented using:

var client = ClientExtensions.CreateClient();
app.RunProxy(context =>
{
    var forwardContext = context.ForwardTo("upstream-host-url");

    return client.SendAsync(forwardContext.UpstreamRequest);
});

Since CacheCow supports using HttpRequestMessage and HttpResponseMessage, it integrates nicely with Execute().

I would like to live it to you to decide on the best integration design.

@damianh
Copy link
Collaborator

damianh commented Jan 25, 2019

Good start. The small problem with this is that is it not using the HttpClient factory.

@damianh
Copy link
Collaborator

damianh commented Jan 25, 2019

Separate NuGet package maybe?

I think I'll go along with recipe right now as opposed to maintaining integration packages.

@jkonecki
Copy link
Contributor Author

I assume, Damian, that you would like CacheCow to use the HttpClient instance injected into ForwardingContext instead of using own instance?

@jkonecki
Copy link
Contributor Author

I've just noticed that ProxyMiddleware doesn't seem to be using HttpClientFactory provided in the constructor. Is this by design?

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

No, that's dead code that I forgot to remove (I'll remove it now). The code of interest is:

https://github.com/damianh/ProxyKit/blob/3b1f34d072b13e60ed639d1f7edf6b5c9c14160f/src/ProxyKit/ProxyContextExtensions.cs#L31-L33

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 26, 2019 via email

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

SharedProxyOptions has a place to insert a custom HttpMessageHandler that is used when creating the HttpMessageHandler pipeline when creating the HttpClient that is injected into ProxyKitClient (aka "typed http client"). Am going to see if it's easy to inject CachingHandler here.

The way HttpClientFactory works is by disposing and re-creating all the handlers on a timed interval. I have a funny feeling that ProxyKit's API as it currently is might not quite be as simple as desired.

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 26, 2019 via email

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

Can't be static. HttpClientFactory will want to dispose it and recreate it. This can result in ObjectDisposedExceptions a couple of minutes into a running system (which doesn't show up in tests). Related #8

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 26, 2019 via email

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

Yes, singleton CacheStore

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

So this appears to work now:

var cacheStore = new InMemoryCacheStore(TimeSpan.FromMinutes(1));
services.AddProxy(options =>
{
	options.GetMessageHandler = () => new CachingHandler(cacheStore)
	{
		InnerHandler = new HttpClientHandler {AllowAutoRedirect = false, UseCookies = false}
	};
});

Not quite pit-of-success...

  1. options.GetMessageHandler is hooked into the HttpClient Factory as the Primary handler. But here CachingHandler is a DelegatingHandler and I didn't put hook inplace for such. Hence manually setting the InnderHandler...
  2. We're not leveraging the IoC container to activate InMemoryCacheStore nor CachingHandler (with ICacheStore being injected into it).

Need to think about this API a bit.

Example CacheCow test code here: https://github.com/damianh/ProxyKit/blob/cachecow/src/ProxyKit.Tests/CacheCowTests.cs#L19

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 26, 2019 via email

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 26, 2019 via email

@damianh
Copy link
Collaborator

damianh commented Jan 26, 2019

You do need to strongly manage the lifecycles of those, yes.

@jkonecki
Copy link
Contributor Author

Thank you very much, Damian!
All working lovely in production :-)

@damianh
Copy link
Collaborator

damianh commented Jan 27, 2019

Production already? Lovely! 😄

Based on this discussion, I made some improvements to make this easier: #37 . This change was semver breaking so new major version (on nuget.org soon).

Also added recipe for CacheCow and am supporting it via tests

I don't think an integration package is worth it right now but will keep open mind to it if there is more demand and makes things easier.

Thanks for suggestion and the help!

@jkonecki
Copy link
Contributor Author

jkonecki commented Jan 27, 2019 via email

@damianh damianh added the enhancement New feature or request label Jan 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants