http.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package discourse
  2. import (
  3. "bytes"
  4. "context"
  5. "net/http"
  6. "time"
  7. "golang.org/x/time/rate"
  8. )
  9. var rl *rate.Limiter
  10. func init() {
  11. rl = rate.NewLimiter(rate.Every(1*time.Second), 1)
  12. }
  13. //RLHTTPClient Rate Limited HTTP Client
  14. type RLHTTPClient struct {
  15. client *http.Client
  16. Ratelimiter *rate.Limiter
  17. }
  18. //Do dispatches the HTTP request to the network
  19. func (c *RLHTTPClient) Do(req *http.Request) (*http.Response, error) {
  20. // Comment out the below 5 lines to turn off ratelimiting
  21. ctx := context.Background()
  22. err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
  23. if err != nil {
  24. return nil, err
  25. }
  26. resp, err := c.client.Do(req)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return resp, nil
  31. }
  32. //NewClient return http client with a ratelimiter
  33. func getClient(rl *rate.Limiter) *RLHTTPClient {
  34. c := &RLHTTPClient{
  35. client: http.DefaultClient,
  36. Ratelimiter: rl,
  37. }
  38. return c
  39. }
  40. func newGetRequest(config APIConfig, url string) (*http.Request, error) {
  41. req, err := http.NewRequest("GET", url, nil)
  42. req.Header.Set("Api-Key", config.APIKey)
  43. req.Header.Set("Api-Username", config.APIUsername)
  44. req.Header.Set("Content-Type", "application/json")
  45. return req, err
  46. }
  47. func newPostRequest(config APIConfig, url string, payload []byte) (*http.Request, error) {
  48. req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
  49. req.Header.Set("Api-Key", config.APIKey)
  50. req.Header.Set("Api-Username", config.APIUsername)
  51. req.Header.Set("Content-Type", "application/json")
  52. return req, err
  53. }
  54. // func getClient() *http.Client {
  55. // client := &http.Client{Timeout: time.Second * 5}
  56. // return client
  57. // }