Add madmin package context support (#9172)

This is to improve responsiveness for all
admin API operations and allowing callers
to cancel any on-going admin operations,
if they happen to be waiting too long.
This commit is contained in:
Harshavardhana
2020-03-20 15:00:44 -07:00
committed by GitHub
parent 1ffa983a9d
commit ae654831aa
47 changed files with 398 additions and 213 deletions

View File

@@ -18,6 +18,7 @@
package madmin
import (
"context"
"math/rand"
"net"
"net/http"
@@ -68,7 +69,7 @@ func (r *lockedRandSource) Seed(seed int64) {
// newRetryTimer creates a timer with exponentially increasing
// delays until the maximum retry attempts are reached.
func (adm AdminClient) newRetryTimer(maxRetry int, unit time.Duration, cap time.Duration, jitter float64, doneCh chan struct{}) <-chan int {
func (adm AdminClient) newRetryTimer(ctx context.Context, maxRetry int, unit time.Duration, cap time.Duration, jitter float64) <-chan int {
attemptCh := make(chan int)
// computes the exponential backoff duration according to
@@ -96,14 +97,15 @@ func (adm AdminClient) newRetryTimer(maxRetry int, unit time.Duration, cap time.
go func() {
defer close(attemptCh)
for i := 0; i < maxRetry; i++ {
select {
// Attempts start from 1.
case attemptCh <- i + 1:
case <-doneCh:
attemptCh <- i + 1
select {
case <-time.After(exponentialBackoffWait(i)):
case <-ctx.Done():
// Stop the routine.
return
}
time.Sleep(exponentialBackoffWait(i))
}
}()
return attemptCh