CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/core/exploit/retry.rb
Views: 1904
1
module Msf::Exploit::Retry
2
# Retry the block until it returns a truthy value. Each iteration attempt will
3
# be performed with an exponential backoff. If the timeout period surpasses,
4
# nil is returned.
5
#
6
# @param Integer timeout the number of seconds to wait before the operation times out
7
# @return the truthy value of the block is returned or nil if it timed out
8
def retry_until_truthy(timeout:)
9
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
10
ending_time = start_time + timeout
11
retry_count = 0
12
while Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) < ending_time
13
result = yield
14
return result if result
15
16
retry_count += 1
17
remaining_time_budget = ending_time - Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
18
break if remaining_time_budget <= 0
19
20
delay = 2**retry_count
21
if delay >= remaining_time_budget
22
delay = remaining_time_budget
23
vprint_status("Final attempt. Sleeping for the remaining #{delay} seconds out of total timeout #{timeout}")
24
else
25
vprint_status("Sleeping for #{delay} seconds before attempting again")
26
end
27
28
sleep delay
29
end
30
31
nil
32
end
33
end
34
35