module Entrez::QueryLimit

Private Instance Methods

ignore_query_limit?() click to toggle source

Only way to set this should be through requiring entrez/spec_helpers and calling Entrez.ignore_query_limit(&block).

# File lib/entrez/query_limit.rb, line 30
def ignore_query_limit?
  !!@ignore_query_limit
end
request_times() click to toggle source
# File lib/entrez/query_limit.rb, line 24
def request_times
  @request_times ||= []
end
respect_query_limit() click to toggle source

NCBI does not allow more than 3 requests per second. If the last 3 requests happened within the last 1 second, sleep for enough time to let a full 1 second pass before the next request. Add current time to queue.

# File lib/entrez/query_limit.rb, line 10
def respect_query_limit
  now = Time.now.to_f
  three_requests_ago = request_times[-3]
  request_times << now
  return unless three_requests_ago
  time_for_last_3_requeests = now - three_requests_ago
  enough_time_has_passed = time_for_last_3_requeests >= 1.0
  unless enough_time_has_passed
    sleep_time = 1 - time_for_last_3_requeests
    STDERR.puts "sleeping #{sleep_time}"
    sleep(sleep_time)
  end
end