Skip to content

Commit ad29e5d

Browse files
committed
Only get current time once in errorIfNotReconnecting
Avoid redundant work. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
1 parent 0581201 commit ad29e5d

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

docker/body_reader.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,21 @@ func (br *bodyReader) Read(p []byte) (int, error) {
198198
}
199199
}
200200

201-
// millisecondsSinceOptional is like time.Since(tm).Milliseconds, but it returns a floating-point value.
202-
// If the input time is time.Time{}, it returns math.NaN()
203-
func millisecondsSinceOptional(tm time.Time) float64 {
201+
// millisecondsSinceOptional is like currentTime.Sub(tm).Milliseconds, but it returns a floating-point value.
202+
// If tm is time.Time{}, it returns math.NaN()
203+
func millisecondsSinceOptional(currentTime time.Time, tm time.Time) float64 {
204204
if tm == (time.Time{}) {
205205
return math.NaN()
206206
}
207-
return float64(time.Since(tm).Nanoseconds()) / 1_000_000.0
207+
return float64(currentTime.Sub(tm).Nanoseconds()) / 1_000_000.0
208208
}
209209

210210
// errorIfNotReconnecting makes a heuristic decision whether we should reconnect after err at redactedURL; if so, it returns nil,
211211
// otherwise it returns an appropriate error to return to the caller (possibly augmented with data about the heuristic)
212212
func (br *bodyReader) errorIfNotReconnecting(originalErr error, redactedURL string) error {
213-
totalTime := millisecondsSinceOptional(br.firstConnectionTime)
214-
failureTime := millisecondsSinceOptional(br.lastSuccessTime)
213+
currentTime := time.Now()
214+
totalTime := millisecondsSinceOptional(currentTime, br.firstConnectionTime)
215+
failureTime := millisecondsSinceOptional(currentTime, br.lastSuccessTime)
215216
logrus.Debugf("Reading blob body from %s failed (%#v), decision inputs: lastRetryOffset %d, offset %d, %.3f ms since first connection, %.3f ms since last progress",
216217
redactedURL, originalErr, br.lastRetryOffset, br.offset, totalTime, failureTime)
217218
progress := br.offset - br.lastRetryOffset

0 commit comments

Comments
 (0)