Revision a7770f03 lib/mcpu.py

b/lib/mcpu.py
71 71
  return result
72 72

  
73 73

  
74
class _LockAttemptTimeoutStrategy(object):
74
class LockAttemptTimeoutStrategy(object):
75 75
  """Class with lock acquire timeout strategy.
76 76

  
77 77
  """
78 78
  __slots__ = [
79
    "_attempt",
79
    "_timeouts",
80 80
    "_random_fn",
81
    "_start_time",
82 81
    "_time_fn",
83
    "_running_timeout",
84 82
    ]
85 83

  
86 84
  _TIMEOUT_PER_ATTEMPT = _CalculateLockAttemptTimeouts()
87 85

  
88
  def __init__(self, attempt=0, _time_fn=time.time, _random_fn=random.random):
86
  def __init__(self, _time_fn=time.time, _random_fn=random.random):
89 87
    """Initializes this class.
90 88

  
91
    @type attempt: int
92
    @param attempt: Current attempt number
93 89
    @param _time_fn: Time function for unittests
94 90
    @param _random_fn: Random number generator for unittests
95 91

  
96 92
    """
97 93
    object.__init__(self)
98 94

  
99
    if attempt < 0:
100
      raise ValueError("Attempt must be zero or positive")
101

  
102
    self._attempt = attempt
95
    self._timeouts = iter(self._TIMEOUT_PER_ATTEMPT)
103 96
    self._time_fn = _time_fn
104 97
    self._random_fn = _random_fn
105 98

  
106
    try:
107
      timeout = self._TIMEOUT_PER_ATTEMPT[attempt]
108
    except IndexError:
109
      # No more timeouts, do blocking acquire
110
      timeout = None
111

  
112
    self._running_timeout = locking.RunningTimeout(timeout, False,
113
                                                   _time_fn=_time_fn)
114

  
115 99
  def NextAttempt(self):
116
    """Returns the strategy for the next attempt.
100
    """Returns the timeout for the next attempt.
117 101

  
118 102
    """
119
    return _LockAttemptTimeoutStrategy(attempt=self._attempt + 1,
120
                                       _time_fn=self._time_fn,
121
                                       _random_fn=self._random_fn)
122

  
123
  def CalcRemainingTimeout(self):
124
    """Returns the remaining timeout.
125

  
126
    """
127
    timeout = self._running_timeout.Remaining()
103
    try:
104
      timeout = self._timeouts.next()
105
    except StopIteration:
106
      # No more timeouts, do blocking acquire
107
      timeout = None
128 108

  
129 109
    if timeout is not None:
130 110
      # Add a small variation (-/+ 5%) to timeout. This helps in situations

Also available in: Unified diff