Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
expose BaseLimitIncrease.Apply
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 27, 2022
1 parent d20ed77 commit 58fa159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 34 additions & 4 deletions limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func readLimiterConfigFromJSON(in io.Reader, defaults LimitConfig) (LimitConfig,
if err := json.NewDecoder(in).Decode(&cfg); err != nil {
return LimitConfig{}, err
}
cfg.apply(defaults)
cfg.Apply(defaults)
return cfg, nil
}

Expand Down Expand Up @@ -86,6 +86,35 @@ type BaseLimit struct {
Memory int64
}

// Apply overwrites all zero-valued limits with the values of l2
// Must not use a pointer receiver.
func (l *BaseLimit) Apply(l2 BaseLimit) {
if l.Streams == 0 {
l.Streams = l2.Streams
}
if l.StreamsInbound == 0 {
l.StreamsInbound = l2.StreamsInbound
}
if l.StreamsOutbound == 0 {
l.StreamsOutbound = l2.StreamsOutbound
}
if l.Conns == 0 {
l.Conns = l2.Conns
}
if l.ConnsInbound == 0 {
l.ConnsInbound = l2.ConnsInbound
}
if l.ConnsOutbound == 0 {
l.ConnsOutbound = l2.ConnsOutbound
}
if l.Memory == 0 {
l.Memory = l2.Memory
}
if l.FD == 0 {
l.FD = l2.FD
}
}

// BaseLimitIncrease is the increase per GB of system memory.
type BaseLimitIncrease struct {
Streams int
Expand All @@ -99,7 +128,8 @@ type BaseLimitIncrease struct {
}

// Apply overwrites all zero-valued limits with the values of l2
func (l *BaseLimit) Apply(l2 BaseLimit) {
// Must not use a pointer receiver.
func (l *BaseLimitIncrease) Apply(l2 BaseLimitIncrease) {
if l.Streams == 0 {
l.Streams = l2.Streams
}
Expand All @@ -121,8 +151,8 @@ func (l *BaseLimit) Apply(l2 BaseLimit) {
if l.Memory == 0 {
l.Memory = l2.Memory
}
if l.FD == 0 {
l.FD = l2.FD
if l.FDFraction == 0 {
l.FDFraction = l2.FDFraction
}
}

Expand Down
2 changes: 1 addition & 1 deletion limit_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type LimitConfig struct {
Stream BaseLimit `json:",omitempty"`
}

func (cfg *LimitConfig) apply(c LimitConfig) {
func (cfg *LimitConfig) Apply(c LimitConfig) {
cfg.System.Apply(c.System)
cfg.Transient.Apply(c.Transient)
cfg.ServiceDefault.Apply(c.ServiceDefault)
Expand Down

0 comments on commit 58fa159

Please sign in to comment.