Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoadLimits does not override existing values #1912

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Add error detail to catch-all HTTP error response. {pull}1854[1854]
- Fix issue were errors where being ignored written to elasticsearch. {pull}1896[1896]
- Update apikey.cache_hit log field name to match convention. {pull}1900[1900]
- LoadServerLimits will not overwrite specified limits when loading default/agent number specified values. {issue}1841[1841] {pull}1912[1912]

==== New Features

Expand Down
29 changes: 22 additions & 7 deletions internal/pkg/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,29 @@ func (c *Cache) InitDefaults() {
c.LoadLimits(loadLimits(0))
}

// LoadLimits loads envLimits for any attribute that is not defined in Cache
func (c *Cache) LoadLimits(limits *envLimits) {
l := limits.Cache

c.NumCounters = l.NumCounters
c.MaxCost = l.MaxCost
c.ActionTTL = defaultActionTTL
c.EnrollKeyTTL = defaultEnrollKeyTTL
c.ArtifactTTL = defaultArtifactTTL
c.APIKeyTTL = defaultAPIKeyTTL
c.APIKeyJitter = defaultAPIKeyJitter
if c.NumCounters == 0 {
c.NumCounters = l.NumCounters
}
if c.MaxCost == 0 {
c.MaxCost = l.MaxCost
}
if c.ActionTTL == 0 {
c.ActionTTL = defaultActionTTL
}
if c.EnrollKeyTTL == 0 {
c.EnrollKeyTTL = defaultEnrollKeyTTL
}
if c.ArtifactTTL == 0 {
c.ArtifactTTL = defaultArtifactTTL
}
if c.APIKeyTTL == 0 {
c.APIKeyTTL = defaultAPIKeyTTL
}
if c.APIKeyJitter == 0 {
c.APIKeyJitter = defaultAPIKeyJitter
}
}
32 changes: 32 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,38 @@ func TestConfig(t *testing.T) {
}
}

func TestLoadServerLimits(t *testing.T) {
t.Run("empty loads limits", func(t *testing.T) {
c := &Config{Inputs: []Input{{}}}
err := c.LoadServerLimits()
assert.NoError(t, err)
assert.Equal(t, int64(defaultCheckinMaxBody), c.Inputs[0].Server.Limits.CheckinLimit.MaxBody)
assert.Equal(t, defaultActionTTL, c.Inputs[0].Cache.ActionTTL)
})
t.Run("existing values are not overridden", func(t *testing.T) {
c := &Config{
Inputs: []Input{{
Server: Server{
Limits: ServerLimits{
CheckinLimit: Limit{
MaxBody: 5 * defaultCheckinMaxBody,
},
},
},
Cache: Cache{
ActionTTL: time.Minute,
},
}},
}
err := c.LoadServerLimits()
assert.NoError(t, err)
assert.Equal(t, int64(5*defaultCheckinMaxBody), c.Inputs[0].Server.Limits.CheckinLimit.MaxBody)
assert.Equal(t, defaultCheckinBurst, c.Inputs[0].Server.Limits.CheckinLimit.Burst)
assert.Equal(t, time.Minute, c.Inputs[0].Cache.ActionTTL)
})

}

// Stub out the defaults so that the above is easier to maintain

func defaultCache() Cache {
Expand Down
59 changes: 31 additions & 28 deletions internal/pkg/config/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,41 @@ func (c *ServerLimits) InitDefaults() {
func (c *ServerLimits) LoadLimits(limits *envLimits) {
l := limits.Server

c.MaxHeaderByteSize = 8192 // 8k
c.MaxConnections = l.MaxConnections
c.PolicyThrottle = l.PolicyThrottle
if c.MaxHeaderByteSize == 0 {
c.MaxHeaderByteSize = 8192 // 8k
}
if c.MaxConnections == 0 {
c.MaxConnections = l.MaxConnections
}
if c.PolicyThrottle == 0 {
c.PolicyThrottle = l.PolicyThrottle
}

c.CheckinLimit = mergeEnvLimit(c.CheckinLimit, l.CheckinLimit)
c.ArtifactLimit = mergeEnvLimit(c.ArtifactLimit, l.ArtifactLimit)
c.EnrollLimit = mergeEnvLimit(c.EnrollLimit, l.EnrollLimit)
c.AckLimit = mergeEnvLimit(c.AckLimit, l.AckLimit)
c.StatusLimit = mergeEnvLimit(c.StatusLimit, l.StatusLimit)
}

c.CheckinLimit = Limit{
Interval: l.CheckinLimit.Interval,
Burst: l.CheckinLimit.Burst,
Max: l.CheckinLimit.Max,
MaxBody: l.CheckinLimit.MaxBody,
func mergeEnvLimit(L Limit, l limit) Limit {
result := Limit{
Interval: L.Interval,
Burst: L.Burst,
Max: L.Max,
MaxBody: L.MaxBody,
}
c.ArtifactLimit = Limit{
Interval: l.ArtifactLimit.Interval,
Burst: l.ArtifactLimit.Burst,
Max: l.ArtifactLimit.Max,
MaxBody: l.ArtifactLimit.MaxBody,
if result.Interval == 0 {
result.Interval = l.Interval
}
c.EnrollLimit = Limit{
Interval: l.EnrollLimit.Interval,
Burst: l.EnrollLimit.Burst,
Max: l.EnrollLimit.Max,
MaxBody: l.EnrollLimit.MaxBody,
if result.Burst == 0 {
result.Burst = l.Burst
}
c.AckLimit = Limit{
Interval: l.AckLimit.Interval,
Burst: l.AckLimit.Burst,
Max: l.AckLimit.Max,
MaxBody: l.AckLimit.MaxBody,
if result.Max == 0 {
result.Max = l.Max
}
c.StatusLimit = Limit{
Interval: l.StatusLimit.Interval,
Burst: l.StatusLimit.Burst,
Max: l.StatusLimit.Max,
MaxBody: l.StatusLimit.MaxBody,
if result.MaxBody == 0 {
result.MaxBody = l.MaxBody
}
return result
}