implementing a basic rate limiter in Java









up vote
0
down vote

favorite












I had a requirement of implementing a basic rate limiting for a simple api, (with max 40- 50 requests per day) in Java
requirement was:
Request from an IP should be limited to N in M seconds (eg: 10 reqeusts in 60 seconds)
Here is what i did:used a filter and the logic was:
Approach 1:
used cache2k for saving the ip address and a pojo with counts and last accessed time



put each ip as as key and pojo as value (pojo has fields such as :count, last accessed.)



once a requests comes, check the ip in cache,



  • if no ip retrieved, insert into cache:
    key:ip
    value:pojo with count 1 and last accessed as current date.



  • if the ip is retrieved, then check last accessed date.



    • if difference between last accessed time and current time is > M seconds that means ,no requests have come from this ip in more than M seconds. create a new Pojo with count 1 and last accessed as current date and insert to cache



    • if difference is < M, then check the count .



      • if the count is < N ,then increase the count in the pojo and re insert the value with last accessed as current time and allow the request.


      • if count is = N, filter the request- don't allow the request




Approach 2:



  • Set the cache expire time as M seconds :



  • Check if the entry is there in cache or not.



    • If not , insert key as ip and value as count(which is 1)

    • if yes, check the count ,

      • if count is = N , then don't allow

      • else, insert the value with updated count and allow the request



I don't want to over complicate things by using Guava for a simple application.



Would be great if some one can give suggestions.



Thanks,










share|improve this question

























    up vote
    0
    down vote

    favorite












    I had a requirement of implementing a basic rate limiting for a simple api, (with max 40- 50 requests per day) in Java
    requirement was:
    Request from an IP should be limited to N in M seconds (eg: 10 reqeusts in 60 seconds)
    Here is what i did:used a filter and the logic was:
    Approach 1:
    used cache2k for saving the ip address and a pojo with counts and last accessed time



    put each ip as as key and pojo as value (pojo has fields such as :count, last accessed.)



    once a requests comes, check the ip in cache,



    • if no ip retrieved, insert into cache:
      key:ip
      value:pojo with count 1 and last accessed as current date.



    • if the ip is retrieved, then check last accessed date.



      • if difference between last accessed time and current time is > M seconds that means ,no requests have come from this ip in more than M seconds. create a new Pojo with count 1 and last accessed as current date and insert to cache



      • if difference is < M, then check the count .



        • if the count is < N ,then increase the count in the pojo and re insert the value with last accessed as current time and allow the request.


        • if count is = N, filter the request- don't allow the request




    Approach 2:



    • Set the cache expire time as M seconds :



    • Check if the entry is there in cache or not.



      • If not , insert key as ip and value as count(which is 1)

      • if yes, check the count ,

        • if count is = N , then don't allow

        • else, insert the value with updated count and allow the request



    I don't want to over complicate things by using Guava for a simple application.



    Would be great if some one can give suggestions.



    Thanks,










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I had a requirement of implementing a basic rate limiting for a simple api, (with max 40- 50 requests per day) in Java
      requirement was:
      Request from an IP should be limited to N in M seconds (eg: 10 reqeusts in 60 seconds)
      Here is what i did:used a filter and the logic was:
      Approach 1:
      used cache2k for saving the ip address and a pojo with counts and last accessed time



      put each ip as as key and pojo as value (pojo has fields such as :count, last accessed.)



      once a requests comes, check the ip in cache,



      • if no ip retrieved, insert into cache:
        key:ip
        value:pojo with count 1 and last accessed as current date.



      • if the ip is retrieved, then check last accessed date.



        • if difference between last accessed time and current time is > M seconds that means ,no requests have come from this ip in more than M seconds. create a new Pojo with count 1 and last accessed as current date and insert to cache



        • if difference is < M, then check the count .



          • if the count is < N ,then increase the count in the pojo and re insert the value with last accessed as current time and allow the request.


          • if count is = N, filter the request- don't allow the request




      Approach 2:



      • Set the cache expire time as M seconds :



      • Check if the entry is there in cache or not.



        • If not , insert key as ip and value as count(which is 1)

        • if yes, check the count ,

          • if count is = N , then don't allow

          • else, insert the value with updated count and allow the request



      I don't want to over complicate things by using Guava for a simple application.



      Would be great if some one can give suggestions.



      Thanks,










      share|improve this question













      I had a requirement of implementing a basic rate limiting for a simple api, (with max 40- 50 requests per day) in Java
      requirement was:
      Request from an IP should be limited to N in M seconds (eg: 10 reqeusts in 60 seconds)
      Here is what i did:used a filter and the logic was:
      Approach 1:
      used cache2k for saving the ip address and a pojo with counts and last accessed time



      put each ip as as key and pojo as value (pojo has fields such as :count, last accessed.)



      once a requests comes, check the ip in cache,



      • if no ip retrieved, insert into cache:
        key:ip
        value:pojo with count 1 and last accessed as current date.



      • if the ip is retrieved, then check last accessed date.



        • if difference between last accessed time and current time is > M seconds that means ,no requests have come from this ip in more than M seconds. create a new Pojo with count 1 and last accessed as current date and insert to cache



        • if difference is < M, then check the count .



          • if the count is < N ,then increase the count in the pojo and re insert the value with last accessed as current time and allow the request.


          • if count is = N, filter the request- don't allow the request




      Approach 2:



      • Set the cache expire time as M seconds :



      • Check if the entry is there in cache or not.



        • If not , insert key as ip and value as count(which is 1)

        • if yes, check the count ,

          • if count is = N , then don't allow

          • else, insert the value with updated count and allow the request



      I don't want to over complicate things by using Guava for a simple application.



      Would be great if some one can give suggestions.



      Thanks,







      java rate-limiting cache2k






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 0:25









      Parameswar

      41261131




      41261131



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244739%2fimplementing-a-basic-rate-limiter-in-java%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244739%2fimplementing-a-basic-rate-limiter-in-java%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3