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.
 
 
 

58 lines
1.3 KiB

  1. // Copyright 2016 Google LLC
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gensupport
  5. import (
  6. "io"
  7. "time"
  8. )
  9. // errReader reads out of a buffer until it is empty, then returns the specified error.
  10. type errReader struct {
  11. buf []byte
  12. err error
  13. }
  14. func (er *errReader) Read(p []byte) (int, error) {
  15. if len(er.buf) == 0 {
  16. if er.err == nil {
  17. return 0, io.EOF
  18. }
  19. return 0, er.err
  20. }
  21. n := copy(p, er.buf)
  22. er.buf = er.buf[n:]
  23. return n, nil
  24. }
  25. // UniformPauseStrategy implements BackoffStrategy with uniform pause.
  26. type UniformPauseStrategy time.Duration
  27. func (p UniformPauseStrategy) Pause() (time.Duration, bool) { return time.Duration(p), true }
  28. func (p UniformPauseStrategy) Reset() {}
  29. // NoPauseStrategy implements BackoffStrategy with infinite 0-length pauses.
  30. const NoPauseStrategy = UniformPauseStrategy(0)
  31. // LimitRetryStrategy wraps a BackoffStrategy but limits the number of retries.
  32. type LimitRetryStrategy struct {
  33. Max int
  34. Strategy BackoffStrategy
  35. n int
  36. }
  37. func (l *LimitRetryStrategy) Pause() (time.Duration, bool) {
  38. l.n++
  39. if l.n > l.Max {
  40. return 0, false
  41. }
  42. return l.Strategy.Pause()
  43. }
  44. func (l *LimitRetryStrategy) Reset() {
  45. l.n = 0
  46. l.Strategy.Reset()
  47. }