go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/services/bqexporter/quota_iterator.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bqexporter
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"time"
    21  
    22  	"google.golang.org/api/googleapi"
    23  
    24  	"go.chromium.org/luci/common/data/rand/mathrand"
    25  	"go.chromium.org/luci/common/retry"
    26  )
    27  
    28  // quotaErrorIterator is an retry.Iterator implementation that only retries
    29  // Google API quota errors.
    30  type quotaErrorIterator struct {
    31  	delay    time.Duration
    32  	maxDelay time.Duration
    33  }
    34  
    35  // Next implements exponential backoff retry.
    36  // Not use retry.ExponentialBackOff because we don't want to limited by
    37  // number of retries, and also add jitter.
    38  func (it *quotaErrorIterator) Next(ctx context.Context, err error) time.Duration {
    39  	if apiErr, ok := err.(*googleapi.Error); !ok || apiErr.Code != http.StatusForbidden || !hasReason(apiErr, "quotaExceeded") {
    40  		return retry.Stop
    41  	}
    42  
    43  	delay := it.delay
    44  	if delay > it.maxDelay {
    45  		delay = it.maxDelay
    46  	} else {
    47  		nextDelay := delay * 2
    48  		// +-10% of next delay.
    49  		nextDelay = nextDelay - nextDelay/10 + time.Duration(mathrand.Intn(ctx, int(nextDelay/5)))
    50  		it.delay = nextDelay
    51  	}
    52  
    53  	return delay
    54  }
    55  
    56  func quotaErrorIteratorFactory() retry.Factory {
    57  	return func() retry.Iterator {
    58  		return &quotaErrorIterator{
    59  			delay:    time.Second,
    60  			maxDelay: 10 * time.Second,
    61  		}
    62  	}
    63  }