def generate_new_thread_id(cls, entity_type: str, entity_id: str) -> str: """Generates a new thread ID which is unique. Args: entity_type: str. The type of the entity. entity_id: str. The ID of the entity. Returns: str. A thread ID that is different from the IDs of all the existing threads within the given entity. Raises: Exception. There were too many collisions with existing thread IDs when attempting to generate a new thread ID. """ for _ in python_utils.RANGE(_MAX_RETRIES): thread_id = ( '%s.%s.%s%s' % (entity_type, entity_id, utils.base64_from_int( int(utils.get_current_time_in_millisecs())), utils.base64_from_int(utils.get_random_int(_RAND_RANGE)))) if not cls.get_by_id(thread_id): return thread_id raise Exception( 'New thread id generator is producing too many collisions.')
def generate_new_thread_id(cls, exploration_id): """Generates a new thread id, unique within the exploration. Exploration ID + the generated thread ID is globally unique. """ for _ in range(_MAX_RETRIES): thread_id = ( utils.base64_from_int(utils.get_current_time_in_millisecs()) + utils.base64_from_int(utils.get_random_int(_RAND_RANGE))) if not cls.get_by_exp_and_thread_id(exploration_id, thread_id): return thread_id raise Exception( 'New thread id generator is producing too many collisions.')
def generate_new_thread_id(cls, exploration_id): """Generates a new thread ID which is unique within the exploration. Args: exploration_id: str. The ID of the exploration. Returns: str. A thread ID that is different from the IDs of all the existing threads within the given exploration. Raises: Exception: There were too many collisions with existing thread IDs when attempting to generate a new thread ID. """ for _ in range(_MAX_RETRIES): thread_id = ( utils.base64_from_int(utils.get_current_time_in_millisecs()) + utils.base64_from_int(utils.get_random_int(_RAND_RANGE))) if not cls.get_by_exp_and_thread_id(exploration_id, thread_id): return thread_id raise Exception( 'New thread id generator is producing too many collisions.')
def test_base64_from_int(self): base64_number = utils.base64_from_int(108) self.assertEqual(base64.b64decode(base64_number), '[108]')
def test_base64_from_int(self): # type: () -> None base64_number = utils.base64_from_int(108) self.assertEqual(base64.b64decode(base64_number), b'[108]')