29 lines
840 B
Go
29 lines
840 B
Go
package fetcher
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
// Client encapsulates HTTP client behavior for fetching Reddit JSON endpoints.
|
|
type Client struct {
|
|
httpClient *http.Client
|
|
userAgent string
|
|
limiter *rate.Limiter
|
|
retryLimit int
|
|
}
|
|
|
|
// NewClient constructs a fetcher Client.
|
|
// - userAgent: User-Agent header
|
|
// - timeout: HTTP client timeout
|
|
// - rateDelay: minimum duration between requests (rate limiter)
|
|
// - burst: burst size (usually equal to concurrency)
|
|
// - retryLimit: max retries per request
|
|
func NewClient(userAgent string, timeout time.Duration, rateDelay time.Duration, burst int, retryLimit int) *Client {
|
|
hc := &http.Client{Timeout: timeout}
|
|
lim := rate.NewLimiter(rate.Every(rateDelay), burst)
|
|
return &Client{httpClient: hc, userAgent: userAgent, limiter: lim, retryLimit: retryLimit}
|
|
}
|