6 from sfa.util.sfalogging import logger
9 Callids: a simple mechanism to remember the call ids served so fas
10 memory-only for now - thread-safe
11 implemented as a (singleton) hash 'callid'->timestamp
17 class _call_ids_impl (dict):
20 # 5 minutes sounds amply enough
21 purge_timeout = 5 * 60
22 # when trying to get a lock
28 self._lock = threading.Lock()
31 # return True if the callid is unknown, False otherwise
32 def already_handled(self, call_id):
33 # if not provided in the call...
37 for attempt in range(_call_ids_impl.retries):
39 logger.debug("Waiting for lock (%d)" % attempt)
40 if self._lock.acquire(False):
43 logger.debug("got lock (%d)" % attempt)
45 time.sleep(float(_call_ids_impl.wait_ms) / 1000)
46 # in the unlikely event where we can't get the lock
49 "_call_ids_impl.should_handle_call_id: could not acquire lock")
56 self[call_id] = time.time()
60 logger.debug("released lock")
66 for (k, v) in self.iteritems():
67 if (now - v) >= _call_ids_impl.purge_timeout:
71 logger.debug("Purging call_id %r (%s)" % (
72 k, time.strftime("%H:%M:%S", time.localtime(self[k]))))
75 logger.debug("AFTER PURGE")
76 for (k, v) in self.iteritems():
77 logger.debug("%s -> %s" %
78 (k, time.strftime("%H:%M:%S", time.localtime(v))))
82 if not _call_ids_impl._instance:
83 _call_ids_impl._instance = _call_ids_impl()
84 return _call_ids_impl._instance