From 97607949208fc95bea759eeaa88db3ad2c86bf38 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Wed, 18 Jan 2023 02:51:31 +0100 Subject: [PATCH] rcmgr: fix: Ignore zero values when marshalling Limits. (#1998) --- p2p/host/resource-manager/limit.go | 32 ++++++++++++------------- p2p/host/resource-manager/limit_test.go | 19 +++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/p2p/host/resource-manager/limit.go b/p2p/host/resource-manager/limit.go index 7d0823b1ea..c3d6dd88c8 100644 --- a/p2p/host/resource-manager/limit.go +++ b/p2p/host/resource-manager/limit.go @@ -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 @@ -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 diff --git a/p2p/host/resource-manager/limit_test.go b/p2p/host/resource-manager/limit_test.go index b063e05c00..9070045bc7 100644 --- a/p2p/host/resource-manager/limit_test.go +++ b/p2p/host/resource-manager/limit_test.go @@ -1,6 +1,7 @@ package rcmgr import ( + "encoding/json" "runtime" "testing" @@ -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)) +}