stefanprodan / WebApiThrottle

ASP.NET Web API rate limiter for IIS and Owin hosting
MIT License
1.28k stars 275 forks source link

UpdatePolicy does not update rate limits on a azure service fabric self owin deployment #108

Open udbach opened 7 years ago

udbach commented 7 years ago

Hi,

We have a cloud service written on Azure Service Fabric. It has self owin controller web apis for which throttling has been enforced using WebApiThrottle.

Cloude service is deployed on azure service fabric which gets deployed on multiple nodes. This allows service fabric to fail over in case of and service down/crashes.

We are seeing issue with updating throttle policy at run time. WebApiThrottling is applied for the riles defined in App.config but when updating the policy at run time, the same is not reflected.

When trying to fetch policy from "PolicyMemoryCacheRepository" at run time, we see older policy
some times and some times new policy updated. We also tried removing the policy and adding it. But are still seeing the same issue.

How are are such things handled in case where cache is replicated ? Do you have any scenario for a service fabric application ? Please comment.

More Information: As per the documentation, in the startup Register.cs, we registered throttling handler as :

//Register throttling handler config.MessageHandlers.Add(new ThrottlingHandler( policy: ThrottlePolicy.FromStore(new PolicyConfigurationProvider()), policyRepository: new PolicyMemoryCacheRepository(), repository: new MemoryCacheRepository(), logger: new WebApiThrottle.TracingThrottleLogger(traceWriter) ));

Helper functions to fetch and update throttle policy: private string GetPolicyConfiguration() { try { string id = "throttle_policy";

            PolicyMemoryCacheRepository policyRepository = new PolicyMemoryCacheRepository();
            ThrottlePolicy policyObject = policyRepository.FirstOrDefault(id);
            if (policyObject == null)
            {
                return "Provisioning Service Policy Configuration doesnot exist.";
            }
            else
            {
                return JsonConvert.SerializeObject(policyObject);
            }                
        }
        catch (Exception e)
        {
            throw;
        }
    }

    private string UpdatePolicyConfig(string policyConfig)
    {
        try
        {
            string id = "throttle_policy";

            if (string.IsNullOrEmpty(policyConfig))
                throw new Exception("Policy configuration passed was either null or empty.");

            //Deserialize policyConfig
            ThrottlePolicy policyObject = JsonConvert.DeserializeObject<ThrottlePolicy>(policyConfig);

           //init policy repo
            var policyRepository = new PolicyMemoryCacheRepository();

            //apply policy updates
            policyRepository.Remove(id);

            if (policyRepository.FirstOrDefault(id) == null)
            {
                //Policy id : successfully removed from repository of type 
            }
            else
            {
                //Could not remove policy id : from repository of type
            }

            //apply policy updates
            ThrottleManager.UpdatePolicy(policyObject, policyRepository);               

            return "Successfully updated policy configuration for type";
        }
        catch (Exception ex)
        {
            throw;
        }
    }
BoAnd commented 6 years ago

@udbach instead of using the PolicyMemoryCacheRepository. You should implement a Redis Cache implementation or Table Storage. That way you can have a global policy over all the fabric nodes.

udbach commented 6 years ago

Thanks for your reply. Yeah I have implemented Azure Table Storage to persist the policyConfig as PolicyMemoryCacheRepository does not work for multiple nodes.

Please resolve the support ticket I raised.

From: BoAnd [mailto:notifications@github.com] Sent: Tuesday, October 3, 2017 12:12 PM To: stefanprodan/WebApiThrottle WebApiThrottle@noreply.github.com Cc: Uday Bachu udbach@microsoft.com; Mention mention@noreply.github.com Subject: Re: [stefanprodan/WebApiThrottle] UpdatePolicy does not update rate limits on a azure service fabric self owin deployment (#108)

@udbachhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fudbach&data=02%7C01%7Cudbach%40microsoft.com%7C8c6d2deff33942cc2c4108d50a29cec4%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636426097030821488&sdata=4683FsntMtjWjpNOEoAZT4N9W0e8Zmd8N1VUJRaelps%3D&reserved=0 instead of using the PolicyMemoryCacheRepository. You should implement a Redis Cache implementation or Table Storage. That way you can have a global policy over all the fabric nodes.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fstefanprodan%2FWebApiThrottle%2Fissues%2F108%23issuecomment-333753943&data=02%7C01%7Cudbach%40microsoft.com%7C8c6d2deff33942cc2c4108d50a29cec4%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636426097030821488&sdata=g9I5aAWadtzR07UA0Ct29Nj9RB0AsGMOUnPIMKBpmqg%3D&reserved=0, or mute the threadhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAeJ1pqYkGNJYSpbNHTGlx3G-W_VwUCpCks5sodclgaJpZM4PMdRO&data=02%7C01%7Cudbach%40microsoft.com%7C8c6d2deff33942cc2c4108d50a29cec4%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636426097030821488&sdata=s3opKQe59OVb85mh%2BSsR%2BDEopLF%2BPAzumb%2F%2BCyE4WTQ%3D&reserved=0.