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.
 
 
 

111 lines
3.4 KiB

  1. /*
  2. Copyright 2016 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. "time"
  17. "google.golang.org/grpc/codes"
  18. )
  19. // CallOption is a generic interface for modifying the behavior of outbound calls.
  20. type CallOption interface {
  21. Resolve(*CallSettings)
  22. }
  23. type callOptions []CallOption
  24. // Resolve resolves all call options individually.
  25. func (opts callOptions) Resolve(s *CallSettings) *CallSettings {
  26. for _, opt := range opts {
  27. opt.Resolve(s)
  28. }
  29. return s
  30. }
  31. // CallSettings encapsulates the call settings for a particular API call.
  32. type CallSettings struct {
  33. Timeout time.Duration
  34. RetrySettings RetrySettings
  35. }
  36. // RetrySettings are per-call configurable settings for retrying upon transient failure.
  37. type RetrySettings struct {
  38. RetryCodes map[codes.Code]bool
  39. BackoffSettings BackoffSettings
  40. }
  41. // BackoffSettings are parameters to the exponential backoff algorithm for retrying.
  42. type BackoffSettings struct {
  43. DelayTimeoutSettings MultipliableDuration
  44. RPCTimeoutSettings MultipliableDuration
  45. }
  46. // MultipliableDuration defines parameters for backoff settings.
  47. type MultipliableDuration struct {
  48. Initial time.Duration
  49. Max time.Duration
  50. Multiplier float64
  51. }
  52. // Resolve merges the receiver CallSettings into the given CallSettings.
  53. func (w CallSettings) Resolve(s *CallSettings) {
  54. s.Timeout = w.Timeout
  55. s.RetrySettings = w.RetrySettings
  56. s.RetrySettings.RetryCodes = make(map[codes.Code]bool, len(w.RetrySettings.RetryCodes))
  57. for key, value := range w.RetrySettings.RetryCodes {
  58. s.RetrySettings.RetryCodes[key] = value
  59. }
  60. }
  61. type withRetryCodes []codes.Code
  62. func (w withRetryCodes) Resolve(s *CallSettings) {
  63. s.RetrySettings.RetryCodes = make(map[codes.Code]bool)
  64. for _, code := range w {
  65. s.RetrySettings.RetryCodes[code] = true
  66. }
  67. }
  68. // WithRetryCodes sets a list of Google API canonical error codes upon which a
  69. // retry should be attempted.
  70. func WithRetryCodes(retryCodes []codes.Code) CallOption {
  71. return withRetryCodes(retryCodes)
  72. }
  73. type withDelayTimeoutSettings MultipliableDuration
  74. func (w withDelayTimeoutSettings) Resolve(s *CallSettings) {
  75. s.RetrySettings.BackoffSettings.DelayTimeoutSettings = MultipliableDuration(w)
  76. }
  77. // WithDelayTimeoutSettings specifies:
  78. // - The initial delay time, in milliseconds, between the completion of
  79. // the first failed request and the initiation of the first retrying
  80. // request.
  81. // - The multiplier by which to increase the delay time between the
  82. // completion of failed requests, and the initiation of the subsequent
  83. // retrying request.
  84. // - The maximum delay time, in milliseconds, between requests. When this
  85. // value is reached, `RetryDelayMultiplier` will no longer be used to
  86. // increase delay time.
  87. func WithDelayTimeoutSettings(initial time.Duration, max time.Duration, multiplier float64) CallOption {
  88. return withDelayTimeoutSettings(MultipliableDuration{initial, max, multiplier})
  89. }