New returns a Pacer with sensible defaults.
(options ...Option)
| 77 | |
| 78 | // New returns a Pacer with sensible defaults. |
| 79 | func New(options ...Option) *Pacer { |
| 80 | opts := pacerOptions{ |
| 81 | maxConnections: 0, |
| 82 | retries: 3, |
| 83 | } |
| 84 | for _, o := range options { |
| 85 | o(&opts) |
| 86 | } |
| 87 | p := &Pacer{ |
| 88 | pacerOptions: opts, |
| 89 | pacer: make(chan struct{}, 1), |
| 90 | } |
| 91 | if p.calculator == nil { |
| 92 | p.SetCalculator(nil) |
| 93 | } |
| 94 | p.state.SleepTime = p.calculator.Calculate(p.state) |
| 95 | if p.invoker == nil { |
| 96 | p.invoker = invoke |
| 97 | } |
| 98 | p.SetMaxConnections(p.maxConnections) |
| 99 | |
| 100 | // Put the first pacing token in |
| 101 | p.pacer <- struct{}{} |
| 102 | |
| 103 | return p |
| 104 | } |
| 105 | |
| 106 | // SetMaxConnections sets the maximum number of concurrent connections. |
| 107 | // Setting the value to 0 will allow unlimited number of connections. |