formatRateReset formats d to look like "[rate reset in 2s]" or "[rate reset in 87m02s]" for the positive durations. And like "[rate limit was reset 87m02s ago]" for the negative cases.
(d time.Duration)
| 2002 | // "[rate reset in 87m02s]" for the positive durations. And like "[rate limit was reset 87m02s ago]" |
| 2003 | // for the negative cases. |
| 2004 | func formatRateReset(d time.Duration) string { |
| 2005 | isNegative := d < 0 |
| 2006 | if isNegative { |
| 2007 | d *= -1 |
| 2008 | } |
| 2009 | secondsTotal := int(0.5 + d.Seconds()) |
| 2010 | minutes := secondsTotal / 60 |
| 2011 | seconds := secondsTotal - minutes*60 |
| 2012 | |
| 2013 | var timeString string |
| 2014 | if minutes > 0 { |
| 2015 | timeString = fmt.Sprintf("%vm%02ds", minutes, seconds) |
| 2016 | } else { |
| 2017 | timeString = fmt.Sprintf("%vs", seconds) |
| 2018 | } |
| 2019 | |
| 2020 | if isNegative { |
| 2021 | return fmt.Sprintf("[rate limit was reset %v ago]", timeString) |
| 2022 | } |
| 2023 | return fmt.Sprintf("[rate reset in %v]", timeString) |
| 2024 | } |
| 2025 | |
| 2026 | func sleepUntilResetWithBuffer(ctx context.Context, reset time.Time) error { |
| 2027 | buffer := time.Second |
no outgoing calls
searching dependent graphs…