Skip to content

Commit cf69dc7

Browse files
committed
Added exponential backoff to snapshot creation throttling errors
1 parent 52e4fd4 commit cf69dc7

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

libcloudforensics/providers/gcp/forensics.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import random
1919
import re
2020
import subprocess
21+
import time
2122
from typing import List, Tuple, Optional, Dict, Any, Union, Sequence
2223

2324
from google.auth.exceptions import DefaultCredentialsError
@@ -90,7 +91,22 @@ def CreateDiskCopy(
9091
disk_type = 'pd-standard'
9192

9293
logger.info('Disk copy of {0:s} started...'.format(disk_to_copy.name))
93-
snapshot, created = disk_to_copy.Snapshot()
94+
95+
attempts = 0
96+
max_attempts = 5
97+
backoff = 1
98+
while attempts < max_attempts:
99+
try:
100+
attempts += 1
101+
snapshot, created = disk_to_copy.Snapshot()
102+
break
103+
except RuntimeError as exception:
104+
if 'RESOURCE_OPERATION_RATE_EXCEEDED' not in str(exception) or attempts >= max_attempts:
105+
raise exception
106+
logger.debug('Snapshot creation throttled: Pausing {0:d} seconds'.format(backoff))
107+
time.sleep(backoff)
108+
backoff ** 2
109+
94110
logger.debug('Snapshot created: {0:s}'.format(snapshot.name))
95111
new_disk = dst_project.compute.CreateDiskFromSnapshot(
96112
snapshot, disk_name_prefix='evidence', disk_type=disk_type)

0 commit comments

Comments
 (0)