Skip to content

Commit

Permalink
rcmgr: fix: Ignore zero values when marshalling Limits. (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro authored Jan 18, 2023
1 parent 638329d commit 9760794
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
32 changes: 16 additions & 16 deletions p2p/host/resource-manager/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func NewFixedLimiter(conf LimitConfig) Limiter {

// BaseLimit is a mixin type for basic resource limits.
type BaseLimit struct {
Streams int
StreamsInbound int
StreamsOutbound int
Conns int
ConnsInbound int
ConnsOutbound int
FD int
Memory int64
Streams int `json:",omitempty"`
StreamsInbound int `json:",omitempty"`
StreamsOutbound int `json:",omitempty"`
Conns int `json:",omitempty"`
ConnsInbound int `json:",omitempty"`
ConnsOutbound int `json:",omitempty"`
FD int `json:",omitempty"`
Memory int64 `json:",omitempty"`
}

// Apply overwrites all zero-valued limits with the values of l2
Expand Down Expand Up @@ -128,16 +128,16 @@ func (l *BaseLimit) Apply(l2 BaseLimit) {

// BaseLimitIncrease is the increase per GiB of allowed memory.
type BaseLimitIncrease struct {
Streams int
StreamsInbound int
StreamsOutbound int
Conns int
ConnsInbound int
ConnsOutbound int
Streams int `json:",omitempty"`
StreamsInbound int `json:",omitempty"`
StreamsOutbound int `json:",omitempty"`
Conns int `json:",omitempty"`
ConnsInbound int `json:",omitempty"`
ConnsOutbound int `json:",omitempty"`
// Memory is in bytes. Values over 1>>30 (1GiB) don't make sense.
Memory int64
Memory int64 `json:",omitempty"`
// FDFraction is expected to be >= 0 and <= 1.
FDFraction float64
FDFraction float64 `json:",omitempty"`
}

// Apply overwrites all zero-valued limits with the values of l2
Expand Down
19 changes: 19 additions & 0 deletions p2p/host/resource-manager/limit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rcmgr

import (
"encoding/json"
"runtime"
"testing"

Expand Down Expand Up @@ -141,3 +142,21 @@ func TestReadmeExample(t *testing.T) {
require.Equal(t, 384, limitConf.System.Conns)
require.Equal(t, 1000, limitConf.System.FD)
}

func TestSerializeJSON(t *testing.T) {
bl := BaseLimit{
Streams: 10,
}

out, err := json.Marshal(bl)
require.NoError(t, err)
require.Equal(t, "{\"Streams\":10}", string(out))

bli := BaseLimitIncrease{
Streams: 10,
}

out, err = json.Marshal(bli)
require.NoError(t, err)
require.Equal(t, "{\"Streams\":10}", string(out))
}

0 comments on commit 9760794

Please sign in to comment.