Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

100 lignes
3.3 KiB

  1. // Copyright 2016, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. package gax
  30. import (
  31. "context"
  32. "strings"
  33. "time"
  34. )
  35. // APICall is a user defined call stub.
  36. type APICall func(context.Context, CallSettings) error
  37. // Invoke calls the given APICall,
  38. // performing retries as specified by opts, if any.
  39. func Invoke(ctx context.Context, call APICall, opts ...CallOption) error {
  40. var settings CallSettings
  41. for _, opt := range opts {
  42. opt.Resolve(&settings)
  43. }
  44. return invoke(ctx, call, settings, Sleep)
  45. }
  46. // Sleep is similar to time.Sleep, but it can be interrupted by ctx.Done() closing.
  47. // If interrupted, Sleep returns ctx.Err().
  48. func Sleep(ctx context.Context, d time.Duration) error {
  49. t := time.NewTimer(d)
  50. select {
  51. case <-ctx.Done():
  52. t.Stop()
  53. return ctx.Err()
  54. case <-t.C:
  55. return nil
  56. }
  57. }
  58. type sleeper func(ctx context.Context, d time.Duration) error
  59. // invoke implements Invoke, taking an additional sleeper argument for testing.
  60. func invoke(ctx context.Context, call APICall, settings CallSettings, sp sleeper) error {
  61. var retryer Retryer
  62. for {
  63. err := call(ctx, settings)
  64. if err == nil {
  65. return nil
  66. }
  67. if settings.Retry == nil {
  68. return err
  69. }
  70. // Never retry permanent certificate errors. (e.x. if ca-certificates
  71. // are not installed). We should only make very few, targeted
  72. // exceptions: many (other) status=Unavailable should be retried, such
  73. // as if there's a network hiccup, or the internet goes out for a
  74. // minute. This is also why here we are doing string parsing instead of
  75. // simply making Unavailable a non-retried code elsewhere.
  76. if strings.Contains(err.Error(), "x509: certificate signed by unknown authority") {
  77. return err
  78. }
  79. if retryer == nil {
  80. if r := settings.Retry(); r != nil {
  81. retryer = r
  82. } else {
  83. return err
  84. }
  85. }
  86. if d, ok := retryer.Retry(err); !ok {
  87. return err
  88. } else if err = sp(ctx, d); err != nil {
  89. return err
  90. }
  91. }
  92. }