2018-12-21 23:14:41 -05:00
|
|
|
package http
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2018-10-07 21:23:36 -04:00
|
|
|
import (
|
|
|
|
"github.com/sethgrid/pester"
|
|
|
|
)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
type (
|
2018-12-21 23:14:41 -05:00
|
|
|
Option func(opts *Options)
|
|
|
|
|
2018-10-07 21:23:36 -04:00
|
|
|
Options struct {
|
|
|
|
backoff pester.BackoffStrategy
|
|
|
|
maxRetries int
|
|
|
|
concurrency int
|
|
|
|
proxy string
|
2018-10-07 22:18:57 -04:00
|
|
|
userAgent string
|
2018-10-07 21:23:36 -04:00
|
|
|
}
|
2018-09-18 16:42:38 -04:00
|
|
|
)
|
|
|
|
|
2018-11-30 19:30:55 -05:00
|
|
|
func newOptions(setters []Option) *Options {
|
|
|
|
opts := new(Options)
|
|
|
|
opts.backoff = pester.ExponentialBackoff
|
|
|
|
opts.concurrency = 3
|
|
|
|
opts.maxRetries = 5
|
|
|
|
|
|
|
|
for _, setter := range setters {
|
|
|
|
setter(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2018-09-18 16:42:38 -04:00
|
|
|
func WithDefaultBackoff() Option {
|
2018-10-07 21:23:36 -04:00
|
|
|
return func(opts *Options) {
|
|
|
|
opts.backoff = pester.DefaultBackoff
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLinearBackoff() Option {
|
2018-10-07 21:23:36 -04:00
|
|
|
return func(opts *Options) {
|
|
|
|
opts.backoff = pester.LinearBackoff
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExponentialBackoff() Option {
|
2018-10-07 21:23:36 -04:00
|
|
|
return func(opts *Options) {
|
|
|
|
opts.backoff = pester.ExponentialBackoff
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMaxRetries(value int) Option {
|
2018-10-07 21:23:36 -04:00
|
|
|
return func(opts *Options) {
|
|
|
|
opts.maxRetries = value
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithConcurrency(value int) Option {
|
2018-10-07 21:23:36 -04:00
|
|
|
return func(opts *Options) {
|
|
|
|
opts.concurrency = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithProxy(address string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.proxy = address
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-07 22:18:57 -04:00
|
|
|
|
|
|
|
func WithUserAgent(value string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.userAgent = value
|
|
|
|
}
|
|
|
|
}
|