[Groonga-commit] droonga/droonga-client-ruby at 8a28841 [master] Add RateLimiter

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 23 21:09:35 JST 2014


Kouhei Sutou	2014-03-23 21:09:35 +0900 (Sun, 23 Mar 2014)

  New Revision: 8a2884141fd3e7f7b4235febcce0e5250a4845ad
  https://github.com/droonga/droonga-client-ruby/commit/8a2884141fd3e7f7b4235febcce0e5250a4845ad

  Message:
    Add RateLimiter
    
    It limits by messages per second.

  Added files:
    lib/droonga/client/rate-limiter.rb
  Modified files:
    lib/droonga/client.rb

  Modified: lib/droonga/client.rb (+1 -0)
===================================================================
--- lib/droonga/client.rb    2014-03-19 15:43:03 +0900 (8d32519)
+++ lib/droonga/client.rb    2014-03-23 21:09:35 +0900 (a854a16)
@@ -19,6 +19,7 @@ require "droonga/client/version"
 require "droonga/client/error"
 require "droonga/client/connection/http"
 require "droonga/client/connection/droonga-protocol"
+require "droonga/client/rate-limiter"
 
 module Droonga
   class Client

  Added: lib/droonga/client/rate-limiter.rb (+63 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/client/rate-limiter.rb    2014-03-23 21:09:35 +0900 (38db3b9)
@@ -0,0 +1,63 @@
+# Copyright (C) 2014 Droonga Project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+module Droonga
+  class Client
+    class RateLimiter
+      NO_LIMIT = -1
+
+      def initialize(client, messages_per_second)
+        @client = client
+        @messages_per_second = messages_per_second
+      end
+
+      def send(*args, &block)
+        limit do
+          @client.send(*args, &block)
+        end
+      end
+
+      def method_missing(name, *args, &block)
+        if****@clien*****_to?(name)
+          @client.__send__(name, *args, &block)
+        else
+          super
+        end
+      end
+
+      private
+      def limit
+        return yield if @messages_per_second == NO_LIMIT
+
+        if****@curre*****_i != Time.now.to_i
+          reset_counter
+        end
+
+        @n_sent_messages_in_second += 1
+        if @n_sent_messages_in_second > @messages_per_second
+          sleep(Time.at(@current.to_i + 1) - @current)
+          reset_counter
+        end
+
+        yield
+      end
+
+      def reset_counter
+        @current = Time.now
+        @n_sent_messages_in_second = 0
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index