mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +00:00
Add a RateLimiter for the gce altTokenSource.
This commit is contained in:
parent
66d2e1cb92
commit
be0d24824d
@ -21,17 +21,28 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
|
|
||||||
"code.google.com/p/google-api-go-client/googleapi"
|
"code.google.com/p/google-api-go-client/googleapi"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Max QPS to allow through to the token URL.
|
||||||
|
tokenURLQPS = 1
|
||||||
|
// Maximum burst of requests to token URL before limiting.
|
||||||
|
tokenURLBurst = 3
|
||||||
|
)
|
||||||
|
|
||||||
type altTokenSource struct {
|
type altTokenSource struct {
|
||||||
oauthClient *http.Client
|
oauthClient *http.Client
|
||||||
tokenURL string
|
tokenURL string
|
||||||
|
throttle util.RateLimiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *altTokenSource) Token() (*oauth2.Token, error) {
|
func (a *altTokenSource) Token() (*oauth2.Token, error) {
|
||||||
|
a.throttle.Accept()
|
||||||
req, err := http.NewRequest("GET", a.tokenURL, nil)
|
req, err := http.NewRequest("GET", a.tokenURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -62,6 +73,7 @@ func newAltTokenSource(tokenURL string) oauth2.TokenSource {
|
|||||||
a := &altTokenSource{
|
a := &altTokenSource{
|
||||||
oauthClient: client,
|
oauthClient: client,
|
||||||
tokenURL: tokenURL,
|
tokenURL: tokenURL,
|
||||||
|
throttle: util.NewTokenBucketRateLimiter(tokenURLQPS, tokenURLBurst),
|
||||||
}
|
}
|
||||||
return oauth2.ReuseTokenSource(nil, a)
|
return oauth2.ReuseTokenSource(nil, a)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user