INNER CODE UNIT · Python
_open_file_with_retry
lebinh/ngxtop · ngxtop/ngxtop.py:204
def _open_file_with_retry(file_path, max_retries):
"""
Open file with retry logic, handling temporary file absence during rotation.
Returns (file_handle, inode, size) or (None, None, 0) if failed.
"""
for attempt in range(max_retries):
try:
file_stat = os.stat(file_path)
f = open(file_path, 'r')
return f, file_stat.st_ino, file_stat.st_size
except (OSError, IOError) as e:
if attempt < max_retries - 1:
# Wait with exponential backoff: 0.1, 0.2, 0.4, 0.8, 1.6 seconds
wait_time = 0.1 * (2 ** attempt)
logging.warning(f"Failed to open {file_path} (attempt {attempt + 1}/{max_retries}): {e}. Retrying in {wait_time}s...")
time.sleep(wait_time)
else: