Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Nov 27, 2024
1 parent 217f7d5 commit b5dbfb0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions x-pack/metricbeat/module/openai/usage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,39 @@ type RLHTTPClient struct {
// It waits for rate limit token before proceeding with the request.
// Returns the HTTP response and any error encountered.
func (c *RLHTTPClient) Do(req *http.Request) (*http.Response, error) {
c.logger.Debug("Waiting for rate limit token")
start := time.Now()

c.logger.Debug("Waiting for rate limit token")

err := c.Ratelimiter.Wait(req.Context())
waitDuration := time.Since(start)
if err != nil {
return nil, err
}

c.logger.Debug("Rate limit token acquired")

waitDuration := time.Since(start)

if waitDuration > time.Minute {
c.logger.Infof("Rate limit wait exceeded threshold: %v", waitDuration)
}

return c.client.Do(req)
}

// newClient creates a new rate-limited HTTP client with specified rate limiter and timeout.
func newClient(logger *logp.Logger, rl *rate.Limiter, timeout time.Duration) *RLHTTPClient {
var client = http.DefaultClient
client.Timeout = timeout
transport := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
}

client := &http.Client{
Timeout: timeout,
Transport: transport,
}

return &RLHTTPClient{
client: client,
logger: logger,
Expand Down

0 comments on commit b5dbfb0

Please sign in to comment.