Skip to content

Instantly share code, notes, and snippets.

@kaspergrubbe
Last active October 31, 2018 14:55
Show Gist options
  • Save kaspergrubbe/cd0873c1fa3bb1695e9c214acf3b83b8 to your computer and use it in GitHub Desktop.
Save kaspergrubbe/cd0873c1fa3bb1695e9c214acf3b83b8 to your computer and use it in GitHub Desktop.
Calculate Redis-rb backoff and print a nice table
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'markdown-tables', '1.0.2'
end
attempts = ARGV[0].to_i
reconnect_delay = ARGV[1].to_f
reconnect_delay_max = ARGV[2].to_f
if [attempts, reconnect_delay, reconnect_delay_max].any?(&:nil?)
puts 'calculate_backoff.rb'
puts
puts 'Usage:'
puts 'ruby calculate_backoff.rb attempts reconnect_delay reconnect_delay_max'
puts
puts 'Example:'
puts 'ruby calculate_backoff.rb 10 1.5 10'
exit 1
end
labels = ['#', 'Attempt wait time', 'Total wait time']
data = [].tap do |it|
total_wait_time = 0
(1..attempts).each do |attempt|
wait_time = [(reconnect_delay * 2**(attempt-1)), reconnect_delay_max].min
total_wait_time += wait_time
it << [attempt, "#{wait_time}s", "#{total_wait_time}s"]
end
end
table = MarkdownTables.make_table(
labels,
data,
is_rows: true,
align: ['l', 'c', 'c'],
)
puts MarkdownTables.plain_text(table)
# puts MarkdownTables.make_table(labels, data, is_rows: true)
|====|===================|======================|
| # | Attempt wait time | Total wait time |
|====|===================|======================|
| 1 | 0.1s | 0.1s |
|----|-------------------|----------------------|
| 2 | 0.2s | 0.30000000000000004s |
|----|-------------------|----------------------|
| 3 | 0.4s | 0.7000000000000001s |
|----|-------------------|----------------------|
| 4 | 0.8s | 1.5s |
|----|-------------------|----------------------|
| 5 | 1.5s | 3.0s |
|----|-------------------|----------------------|
| 6 | 1.5s | 4.5s |
|----|-------------------|----------------------|
| 7 | 1.5s | 6.0s |
|----|-------------------|----------------------|
| 8 | 1.5s | 7.5s |
|----|-------------------|----------------------|
| 9 | 1.5s | 9.0s |
|----|-------------------|----------------------|
| 10 | 1.5s | 10.5s |
|====|===================|======================|
# Attempt wait time Total wait time
1 0.1s 0.1s
2 0.2s 0.30000000000000004s
3 0.4s 0.7000000000000001s
4 0.8s 1.5s
5 1.5s 3.0s
6 1.5s 4.5s
7 1.5s 6.0s
8 1.5s 7.5s
9 1.5s 9.0s
10 1.5s 10.5s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment