def _allocate_segment(self, session, net_id, source):
with session.begin(subtransactions=True):<EOL><INDENT>hour_lapse = utils.utc_time_lapse(self.seg_timeout)<EOL>count = (session.query(self.model).filter(<EOL>self.model.delete_time < hour_lapse).update(<EOL>{"<STR_LIT>": None}))<EOL>select = (session.query(self.model).filter_by(allocated=False,<EOL>delete_time=None))<EOL>for attempt in range(DB_MAX_RETRIES + <NUM_LIT:1>):<EOL><INDENT>alloc = select.first()<EOL>if not alloc:<EOL><INDENT>LOG.info("<STR_LIT>")<EOL>return<EOL><DEDENT>count = (session.query(self.model).<EOL>filter_by(segmentation_id=alloc.segmentation_id,<EOL>allocated=False).update({"<STR_LIT>": True,<EOL>"<STR_LIT>": net_id,<EOL>"<STR_LIT:source>": source}))<EOL>if count:<EOL><INDENT>return alloc<EOL><DEDENT><DEDENT><DEDENT>LOG.error("<STR_LIT>"<EOL>"<STR_LIT>",<EOL>{'<STR_LIT>': net_id, '<STR_LIT:src>': source})<EOL>
Allocate segment from pool.

        Return allocated db object or None.