request.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package nodestats
  2. import (
  3. "fmt"
  4. "io"
  5. "math"
  6. "net/http"
  7. "strconv"
  8. "time"
  9. "github.com/opencost/opencost/core/pkg/log"
  10. )
  11. // HttpClient is an interface that captures the Do method of the http.Client. We use this interface to allow
  12. // mocking in tests.
  13. type HttpClient interface {
  14. Do(req *http.Request) (*http.Response, error)
  15. }
  16. type NodeHttpClient struct {
  17. client HttpClient
  18. }
  19. // NewNodeHttpClient creates a new NodeHttpClient with the provided HttpClient.
  20. func NewNodeHttpClient(client HttpClient) *NodeHttpClient {
  21. return &NodeHttpClient{
  22. client: client,
  23. }
  24. }
  25. // AttemptEndPoint will hit a specified endpoint with as many retries as it is allotted.
  26. func (c *NodeHttpClient) AttemptEndPoint(method string, URL string, bearerToken string) (*http.Response, error) {
  27. attempts := uint(1)
  28. for i := uint(0); i < attempts; i++ {
  29. if i > 0 {
  30. time.Sleep(time.Duration(int64(math.Pow(2, float64(i)))) * time.Second)
  31. }
  32. data, err := c.makeRequest(method, URL, bearerToken)
  33. if err == nil {
  34. return data, nil
  35. }
  36. log.Warnf("Error making request to %s: %s", URL, err)
  37. }
  38. return nil, fmt.Errorf("requests to %v failed", URL)
  39. }
  40. // makeRequest will call out to an endpoint and attempt to decode the body into an existing
  41. // data type.
  42. func (c *NodeHttpClient) makeRequest(method string, URL string, bearerToken string) (*http.Response, error) {
  43. request, err := http.NewRequest(method, URL, nil)
  44. if err != nil {
  45. return nil, err
  46. }
  47. if bearerToken != "" {
  48. request.Header.Add("Authorization", "Bearer "+bearerToken)
  49. }
  50. resp, err := c.client.Do(request)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
  55. if resp.Body != nil {
  56. io.Copy(io.Discard, resp.Body)
  57. resp.Body.Close()
  58. }
  59. return nil, fmt.Errorf("invalid response %s", strconv.Itoa(resp.StatusCode))
  60. }
  61. return resp, nil
  62. }
  63. // NodeHttpConnect is a struct that represents a connection to a node using an http client and endpoint formatter.
  64. type NodeHttpConnection struct {
  65. formatter NodeEndpointFormatter
  66. client *NodeHttpClient
  67. }
  68. // NewNodeHttpConnection creates a new HttpConnection with the provided NodeHttpClient and NodeEndpointFormatter.
  69. func NewNodeHttpConnection(client *NodeHttpClient, formatter NodeEndpointFormatter) *NodeHttpConnection {
  70. return &NodeHttpConnection{
  71. formatter: formatter,
  72. client: client,
  73. }
  74. }
  75. // AttemptEndPoint will hit a specified endpoint leveraging the internal http client and formatter for the endpoint.
  76. func (nhc *NodeHttpConnection) AttemptEndPoint(method string, url string, bearerToken string) (*http.Response, error) {
  77. return nhc.client.AttemptEndPoint(method, nhc.formatter.FormatEndpoint(url), bearerToken)
  78. }