File: ratelimit.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,612 kB
  • sloc: makefile: 28; sh: 13
file content (40 lines) | stat: -rw-r--r-- 1,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package datadog

import (
	"fmt"
	"net/http"
	"net/url"
)

// The list of Rate Limited Endpoints of the Datadog API.
// https://docs.datadoghq.com/api/?lang=bash#rate-limiting
func (client *Client) updateRateLimits(resp *http.Response, api *url.URL) error {
	if resp == nil || resp.Header == nil || api.Path == "" {
		return fmt.Errorf("malformed HTTP content.")
	}
	if resp.Header.Get("X-RateLimit-Remaining") == "" {
		// The endpoint is not Rate Limited.
		return nil
	}
	client.m.Lock()
	defer client.m.Unlock()
	client.rateLimitingStats[api.Path] = RateLimit{
		Limit:     resp.Header.Get("X-RateLimit-Limit"),
		Reset:     resp.Header.Get("X-RateLimit-Reset"),
		Period:    resp.Header.Get("X-RateLimit-Period"),
		Remaining: resp.Header.Get("X-RateLimit-Remaining"),
	}
	return nil
}

// GetRateLimitStats is a threadsafe getter to retrieve the rate limiting stats associated with the Client.
func (client *Client) GetRateLimitStats() map[string]RateLimit {
	client.m.Lock()
	defer client.m.Unlock()
	// Shallow copy to avoid corrupted data
	mapCopy := make(map[string]RateLimit, len(client.rateLimitingStats))
	for k, v := range client.rateLimitingStats {
		mapCopy[k] = v
	}
	return mapCopy
}