6 from sfa.util.sfalogging import sfa_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
14 class _call_ids_impl (dict):
17 # 5 minutes sounds amply enough
19 # when trying to get a lock
25 self._lock=threading.Lock()
28 # return True if the callid is unknown, False otherwise
29 def should_handle_call_id (self,call_id):
30 # if not provided in the call...
31 if not call_id: return True
33 for attempt in range(_call_ids_impl.retries):
34 sfa_logger().debug("Waiting for lock (%d)"%attempt)
35 if self._lock.acquire(False):
37 sfa_logger().debug("got lock (%d)"%attempt)
39 time.sleep(float(_call_ids_impl.wait_ms)/1000)
40 # in the unlikely event where we can't get the lock
42 sfa_logger().warning("_call_ids_impl.should_handle_call_id: could not acquire lock")
45 if self.has_key(call_id):
49 self[call_id]=time.time()
52 sfa_logger().debug("released lock")
58 for (k,v) in self.iteritems():
59 if (now-v) >= _call_ids_impl.purge_timeout: o_keys.append(k)
61 sfa_logger().debug("Purging call_id %r (%s)"%(k,time.strftime("%H:%M:%S",time.localtime(self[k]))))
63 sfa_logger().debug("AFTER PURGE")
64 for (k,v) in self.iteritems(): sfa_logger().debug("%s -> %s"%(k,time.strftime("%H:%M:%S",time.localtime(v))))
67 if not _call_ids_impl._instance:
68 _call_ids_impl._instance = _call_ids_impl()
69 return _call_ids_impl._instance