1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package discourse
- import (
- "bytes"
- "context"
- "net/http"
- "time"
- "golang.org/x/time/rate"
- )
- var rl *rate.Limiter
- func init() {
- rl = rate.NewLimiter(rate.Every(1*time.Second), 1)
- }
- //RLHTTPClient Rate Limited HTTP Client
- type RLHTTPClient struct {
- client *http.Client
- Ratelimiter *rate.Limiter
- }
- //Do dispatches the HTTP request to the network
- func (c *RLHTTPClient) Do(req *http.Request) (*http.Response, error) {
- // Comment out the below 5 lines to turn off ratelimiting
- ctx := context.Background()
- err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
- if err != nil {
- return nil, err
- }
- resp, err := c.client.Do(req)
- if err != nil {
- return nil, err
- }
- return resp, nil
- }
- //NewClient return http client with a ratelimiter
- func getClient(rl *rate.Limiter) *RLHTTPClient {
- c := &RLHTTPClient{
- client: http.DefaultClient,
- Ratelimiter: rl,
- }
- return c
- }
- func newGetRequest(config APIConfig, url string) (*http.Request, error) {
- req, err := http.NewRequest("GET", url, nil)
- req.Header.Set("Api-Key", config.APIKey)
- req.Header.Set("Api-Username", config.APIUsername)
- req.Header.Set("Content-Type", "application/json")
- return req, err
- }
- func newPostRequest(config APIConfig, url string, payload []byte) (*http.Request, error) {
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
- req.Header.Set("Api-Key", config.APIKey)
- req.Header.Set("Api-Username", config.APIUsername)
- req.Header.Set("Content-Type", "application/json")
- return req, err
- }
- // func getClient() *http.Client {
- // client := &http.Client{Timeout: time.Second * 5}
- // return client
- // }
|