Coder Social home page Coder Social logo

Comments (21)

jkonecki avatar jkonecki commented on May 18, 2024 1

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024 1

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

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.

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

damianh avatar damianh commented on May 18, 2024

Separate NuGet package maybe?

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

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

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

from proxykit.

damianh avatar damianh commented on May 18, 2024

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.

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

from proxykit.

damianh avatar damianh commented on May 18, 2024

Yes, singleton CacheStore

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

from proxykit.

damianh avatar damianh commented on May 18, 2024

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

from proxykit.

jkonecki avatar jkonecki commented on May 18, 2024

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

from proxykit.

damianh avatar damianh commented on May 18, 2024

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!

from proxykit.

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.