feat(ratelimier): Support disabling rate limiting
This commit is contained in:
9 files changed
+896
-60
No files matched your search
@@ -51,7 +51,6 @@ import (
|
||||
external_plugins "github.com/hashicorp/boundary/sdk/plugins"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-rate"
|
||||
"github.com/hashicorp/go-secure-stdlib/mlock"
|
||||
"github.com/hashicorp/go-secure-stdlib/pluginutil/v2"
|
||||
"github.com/hashicorp/nodeenrollment"
|
||||
@@ -121,7 +120,8 @@ type Controller struct {
|
||||
apiGrpcServerListener grpcServerListener
|
||||
apiGrpcGatewayTicket string
|
||||
|
||||
rateLimiter *atomic.Pointer[rate.Limiter]
|
||||
rateLimiter ratelimit.Limiter
|
||||
rateLimiterMu sync.RWMutex
|
||||
|
||||
// Repo factory methods
|
||||
AuthTokenRepoFn common.AuthTokenRepoFactory
|
||||
@@ -174,7 +174,6 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
pkiConnManager: cluster.NewDownstreamManager(),
|
||||
workerStatusGracePeriod: new(atomic.Int64),
|
||||
livenessTimeToStale: new(atomic.Int64),
|
||||
rateLimiter: new(atomic.Pointer[rate.Limiter]),
|
||||
}
|
||||
|
||||
if downstreamReceiverFactory != nil {
|
||||
@@ -251,19 +250,9 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
}
|
||||
c.clusterListener = clusterListeners[0]
|
||||
|
||||
rateLimits, err := conf.RawConfig.Controller.ApiRateLimits.Limits(c.baseContext)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing rate limit configuration: %w", err)
|
||||
}
|
||||
|
||||
rateLimiter, err := ratelimit.NewLimiter(
|
||||
rateLimits,
|
||||
conf.RawConfig.Controller.ApiRateLimiterMaxEntries,
|
||||
)
|
||||
if err != nil {
|
||||
if err := c.initializeRateLimiter(conf.RawConfig); err != nil {
|
||||
return nil, fmt.Errorf("error initializing rate limiter: %w", err)
|
||||
}
|
||||
c.rateLimiter.Store(rateLimiter)
|
||||
|
||||
var pluginLogger hclog.Logger
|
||||
for _, enabledPlugin := range c.enabledPlugins {
|
||||
@@ -638,37 +627,3 @@ func (c *Controller) Shutdown() error {
|
||||
func (c *Controller) WorkerStatusUpdateTimes() *sync.Map {
|
||||
return c.workerStatusUpdateTimes
|
||||
}
|
||||
|
||||
func (c *Controller) getRateLimiter() *rate.Limiter {
|
||||
return c.rateLimiter.Load()
|
||||
}
|
||||
|
||||
// ReloadRateLimiter replaces the Controller's rate.Limiter with a new rate.Limiter
|
||||
// using the supplied ratelimit.Configs and max entries. If the configs and max
|
||||
// entries match the current values, the rate.Limiter is not replaced and no
|
||||
// error is returned. Otherwise the rate.Limiter is replaced, and the old rate.Limiter
|
||||
// is shutdown. This means that a client's quota is effectively reset if the
|
||||
// configuration changes.
|
||||
func (c *Controller) ReloadRateLimiter(newLimitConfigs ratelimit.Configs, newMaxEntries int) error {
|
||||
// Config has not changed, no need to reload.
|
||||
if c.conf.ApiRateLimits.Equal(newLimitConfigs) && c.conf.ApiRateLimiterMaxEntries == newMaxEntries {
|
||||
return nil
|
||||
}
|
||||
|
||||
ratelimits, err := newLimitConfigs.Limits(c.baseContext)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing rate limit configuration: %w", err)
|
||||
}
|
||||
|
||||
limiter, err := ratelimit.NewLimiter(ratelimits, newMaxEntries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
old := c.rateLimiter.Swap(limiter)
|
||||
|
||||
c.conf.ApiRateLimits = newLimitConfigs
|
||||
c.conf.ApiRateLimiterMaxEntries = newMaxEntries
|
||||
|
||||
return old.Shutdown()
|
||||
}
|
||||
Reference in new issue
Block a user