Eagerly cancel timeout context when response has no body, fixes #253 (#313)

Co-authored-by: Philip Dubé <phdub@microsoft.com>
This commit is contained in:
Mohit Sharma 2021-11-09 10:49:06 +05:30 коммит произвёл GitHub
Родитель deb21f705e
Коммит 3d50a5f3aa
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 7 добавлений и 2 удалений

Просмотреть файл

@ -256,7 +256,7 @@ func NewRetryPolicyFactory(o RetryOptions) pipeline.Factory {
tryCancel() // If we're returning an error, cancel this current/last per-retry timeout context
} else {
// We wrap the last per-try context in a body and overwrite the Response's Body field with our wrapper.
// So, when the user closes the Body, the our per-try context gets closed too.
// So, when the user closes the Body, then our per-try context gets closed too.
// Another option, is that the Last Policy do this wrapping for a per-retry context (not for the user's context)
if response == nil || response.Response() == nil {
// We do panic in the case response or response.Response() is nil,
@ -265,7 +265,12 @@ func NewRetryPolicyFactory(o RetryOptions) pipeline.Factory {
// as in this case, current per-try has nothing to do in future.
return nil, errors.New("invalid state, response should not be nil when the operation is executed successfully")
}
response.Response().Body = &contextCancelReadCloser{cf: tryCancel, body: response.Response().Body}
if response.Response().Body == http.NoBody {
// If the response is empty the caller isn't obligated to call close
tryCancel();
} else {
response.Response().Body = &contextCancelReadCloser{cf: tryCancel, body: response.Response().Body}
}
}
break // Don't retry
}