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.4 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. // This is ia snapshot from github.com/googleapis/gax-go with minor modifications.
  14. package gax
  15. import (
  16. "math/rand"
  17. "time"
  18. "log"
  19. "os"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/codes"
  23. )
  24. var Logger *log.Logger = log.New(os.Stderr, "", log.LstdFlags)
  25. // A user defined call stub.
  26. type APICall func(context.Context) error
  27. // scaleDuration returns the product of a and mult.
  28. func scaleDuration(a time.Duration, mult float64) time.Duration {
  29. ns := float64(a) * mult
  30. return time.Duration(ns)
  31. }
  32. // invokeWithRetry calls stub using an exponential backoff retry mechanism
  33. // based on the values provided in callSettings.
  34. func invokeWithRetry(ctx context.Context, stub APICall, callSettings CallSettings) error {
  35. retrySettings := callSettings.RetrySettings
  36. backoffSettings := callSettings.RetrySettings.BackoffSettings
  37. delay := backoffSettings.DelayTimeoutSettings.Initial
  38. for {
  39. // If the deadline is exceeded...
  40. if ctx.Err() != nil {
  41. return ctx.Err()
  42. }
  43. err := stub(ctx)
  44. code := grpc.Code(err)
  45. if code == codes.OK {
  46. return nil
  47. }
  48. if !retrySettings.RetryCodes[code] {
  49. return err
  50. }
  51. // Sleep a random amount up to the current delay
  52. d := time.Duration(rand.Int63n(int64(delay)))
  53. delayCtx, _ := context.WithTimeout(ctx, delay)
  54. if Logger != nil {
  55. Logger.Printf("Retryable error: %v, retrying in %v", err, d)
  56. }
  57. <-delayCtx.Done()
  58. delay = scaleDuration(delay, backoffSettings.DelayTimeoutSettings.Multiplier)
  59. if delay > backoffSettings.DelayTimeoutSettings.Max {
  60. delay = backoffSettings.DelayTimeoutSettings.Max
  61. }
  62. }
  63. }
  64. // Invoke calls stub with a child of context modified by the specified options.
  65. func Invoke(ctx context.Context, stub APICall, opts ...CallOption) error {
  66. settings := &CallSettings{}
  67. callOptions(opts).Resolve(settings)
  68. if len(settings.RetrySettings.RetryCodes) > 0 {
  69. return invokeWithRetry(ctx, stub, *settings)
  70. }
  71. return stub(ctx)
  72. }