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

Fix: Ignore zero values when marshalling Limits. #1998

Merged
merged 1 commit into from
Jan 18, 2023
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
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))
}