module Bio::TogoWS::AccessWait

Internal Use Only.

Bio::TogoWS::AccessWait is a module to implement a private method for access.

Constants

TOGOWS_ACCESS_WAIT

common default access wait for TogoWS services

TOGOWS_ACCESS_WAIT_MAX

Maximum waiting time to avoid dead lock. When exceeding this value, (max/2) + rand(max) is used, to randomize access. This means real maximum waiting time is (max * 1.5).

Private Instance Methods

reset_togows_access_wait() click to toggle source

(private) resets last access. Should be used only for debug purpose.

# File lib/bio/io/togows.rb, line 92
def reset_togows_access_wait
  @@togows_last_access = nil
end
togows_access_wait() click to toggle source

Sleeping if needed. It sleeps about TOGOWS_ACCESS_WAIT * (number of waiting processes).


Returns

(Numeric) sleeped time

# File lib/bio/io/togows.rb, line 46
def togows_access_wait
  w_min = TOGOWS_ACCESS_WAIT
  debug = defined?(@debug) && @debug

  # initializing class variable
  @@togows_last_access ||= nil

  # determines waiting time
  wait = 0
  if last = @@togows_last_access then
    elapsed = Time.now - last
    if elapsed < w_min then
      wait = w_min - elapsed
    end
  end

  # If wait is too long, truncated to TOGOWS_ACCESS_WAIT_MAX.
  if wait > TOGOWS_ACCESS_WAIT_MAX then
    orig_wait = wait
    wait = TOGOWS_ACCESS_WAIT_MAX
    wait = wait / 2 + rand(wait)
    if debug then
      $stderr.puts "TogoWS: sleeping time #{orig_wait} is too long and set to #{wait} to avoid dead lock."
    end
    newlast = Time.now + TOGOWS_ACCESS_WAIT_MAX
  else
    newlast = Time.now + wait
  end

  # put expected end time of sleeping
  if !@@togows_last_access or @@togows_last_access < newlast then
    @@togows_last_access = newlast
  end

  # sleeping if needed
  if wait > 0 then
    $stderr.puts "TogoWS: sleeping #{wait} second" if debug
    sleep(wait)
  end
  # returns waited time
  wait
end