Polly-Contrib / Polly.Contrib.DuplicateRequestCollapser

A Polly policy to collapse concurrent duplicate requests to a single execution.
Other
17 stars 3 forks source link

Cannot get this to work #7

Open paulprivalgo opened 3 years ago

paulprivalgo commented 3 years ago

Hello

Im really struggling with Polly

My task is to use Polly to "Squash multiple requests into a single request"

For this I am trying to use this package

As a starting point I used

https://nodogmablog.bryanhogan.net/2018/11/caching-in-polly-6-and-the-httpclientfactory/#:~:text=Polly%20allows%20you%20to%20cache,you%20define%20polices%20in%20startup.

This gave me a startup of

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
        services.AddSingleton<IAsyncCacheProvider, MemoryCacheProvider>();

        IPolicyRegistry<string> registry = services.AddPolicyRegistry();

        services.AddHttpClient("RemoteServer", client =>
        {
            client.BaseAddress = new Uri("http://localhost:5000/api/");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
        }).AddPolicyHandlerFromRegistry(PolicySelector);

        //services.AddSingleton<IRequestService, RequestService>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    private IAsyncPolicy<HttpResponseMessage> PolicySelector(IReadOnlyPolicyRegistry<string> policyRegistry,
        HttpRequestMessage httpRequestMessage)
    {
        // you could have some logic to select the right policy
        // see https://nodogmablog.bryanhogan.net/2018/07/polly-httpclientfactory-and-the-policy-registry-choosing-the-right-policy-based-on-the-http-request/
        return policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("CachingPolicy");
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAsyncCacheProvider cacheProvider, IPolicyRegistry<string> registry)
    {
        CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));
        registry.Add("CachingPolicy", cachePolicy);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

The sample works but as soon as I add the DuplicateRequestCollapser to the project

I had a compilation error on

CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

That was easy to fix I changed to

AsyncCachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

Now when I run I get the error

System.TypeLoadException: 'Method 'TryGet' in type 'Polly.Caching.Memory.MemoryCacheProvider' from 
assembly 'Polly.Caching.Memory, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc' 
does not have an implementation.'

Can anyone tell me what I am doing wrong?

This is not working just from adding the package and I havent even started on integrating the duplicate request collapser itself!

Paul

EugeneKrapivin commented 3 years ago

I've just created a short demo for some team members of how this library operates, I gladly shar it with you :)