You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
2.5 KiB

  1. /*
  2. Copyright 2015 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package gax is a snapshot from github.com/googleapis/gax-go/v2 with minor modifications.
  14. package gax
  15. import (
  16. "context"
  17. "log"
  18. "math/rand"
  19. "os"
  20. "time"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/codes"
  23. )
  24. // Logger is a logger that logs to stderr.
  25. var Logger = log.New(os.Stderr, "", log.LstdFlags)
  26. // APICall is a user defined call stub.
  27. type APICall func(context.Context) error
  28. // scaleDuration returns the product of a and mult.
  29. func scaleDuration(a time.Duration, mult float64) time.Duration {
  30. ns := float64(a) * mult
  31. return time.Duration(ns)
  32. }
  33. // invokeWithRetry calls stub using an exponential backoff retry mechanism
  34. // based on the values provided in callSettings.
  35. func invokeWithRetry(ctx context.Context, stub APICall, callSettings CallSettings) error {
  36. retrySettings := callSettings.RetrySettings
  37. backoffSettings := callSettings.RetrySettings.BackoffSettings
  38. delay := backoffSettings.DelayTimeoutSettings.Initial
  39. for {
  40. // If the deadline is exceeded...
  41. if ctx.Err() != nil {
  42. return ctx.Err()
  43. }
  44. err := stub(ctx)
  45. code := grpc.Code(err)
  46. if code == codes.OK {
  47. return nil
  48. }
  49. if !retrySettings.RetryCodes[code] {
  50. return err
  51. }
  52. // Sleep a random amount up to the current delay
  53. d := time.Duration(rand.Int63n(int64(delay)))
  54. delayCtx, _ := context.WithTimeout(ctx, delay)
  55. if Logger != nil {
  56. Logger.Printf("Retryable error: %v, retrying in %v", err, d)
  57. }
  58. <-delayCtx.Done()
  59. delay = scaleDuration(delay, backoffSettings.DelayTimeoutSettings.Multiplier)
  60. if delay > backoffSettings.DelayTimeoutSettings.Max {
  61. delay = backoffSettings.DelayTimeoutSettings.Max
  62. }
  63. }
  64. }
  65. // Invoke calls stub with a child of context modified by the specified options.
  66. func Invoke(ctx context.Context, stub APICall, opts ...CallOption) error {
  67. settings := &CallSettings{}
  68. callOptions(opts).Resolve(settings)
  69. if len(settings.RetrySettings.RetryCodes) > 0 {
  70. return invokeWithRetry(ctx, stub, *settings)
  71. }
  72. return stub(ctx)
  73. }