[Groonga-commit] droonga/droonga-client-ruby at bb2f528 [master] Sleep for each message instead of one large sleep

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 23 22:55:12 JST 2014


Kouhei Sutou	2014-03-23 22:55:12 +0900 (Sun, 23 Mar 2014)

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

  Message:
    Sleep for each message instead of one large sleep

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

  Modified: lib/droonga/client/rate-limiter.rb (+7 -13)
===================================================================
--- lib/droonga/client/rate-limiter.rb    2014-03-23 21:34:26 +0900 (38db3b9)
+++ lib/droonga/client/rate-limiter.rb    2014-03-23 22:55:12 +0900 (943d574)
@@ -21,6 +21,7 @@ module Droonga
       def initialize(client, messages_per_second)
         @client = client
         @messages_per_second = messages_per_second
+        @diff_in_seconds_per_message = 1.0 / @messages_per_second
       end
 
       def send(*args, &block)
@@ -41,23 +42,16 @@ module Droonga
       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
+        @previous_time ||= Time.now
+        diff_in_seconds = Time.now - @previous_time
+        sleep_time = @diff_in_seconds_per_message - diff_in_seconds
+        if sleep_time > 0
+          sleep(sleep_time)
         end
+        @previous_time = Time.now
 
         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