diff --git a/p2p/host/resource-manager/limit_defaults.go b/p2p/host/resource-manager/limit_defaults.go index c111943438..9ef442e44b 100644 --- a/p2p/host/resource-manager/limit_defaults.go +++ b/p2p/host/resource-manager/limit_defaults.go @@ -152,6 +152,7 @@ func (l *LimitVal) UnmarshalJSON(b []byte) error { if val == 0 { // If there is an explicit 0 in the JSON we should interpret this as block all. *l = BlockAllLimit + return nil } *l = LimitVal(val) @@ -215,6 +216,12 @@ func (l *LimitVal64) UnmarshalJSON(b []byte) error { return fmt.Errorf("failed to unmarshal limit value: %w", err) } + if val == 0 { + // If there is an explicit 0 in the JSON we should interpret this as block all. + *l = BlockAllLimit64 + return nil + } + *l = LimitVal64(val) return nil } @@ -223,8 +230,14 @@ func (l *LimitVal64) UnmarshalJSON(b []byte) error { if err != nil { return err } - *l = LimitVal64(i) + if i == 0 { + // If there is an explicit 0 in the JSON we should interpret this as block all. + *l = BlockAllLimit64 + return nil + } + + *l = LimitVal64(i) return nil } diff --git a/p2p/host/resource-manager/limit_test.go b/p2p/host/resource-manager/limit_test.go index 1bf5103c59..a719f845d4 100644 --- a/p2p/host/resource-manager/limit_test.go +++ b/p2p/host/resource-manager/limit_test.go @@ -227,3 +227,24 @@ func TestSerializeJSON(t *testing.T) { require.NoError(t, err) require.Equal(t, "{\"Streams\":10}", string(out)) } + +func TestWhatIsZeroInResourceLimits(t *testing.T) { + l := ResourceLimits{ + Streams: BlockAllLimit, + Memory: BlockAllLimit64, + } + + out, err := json.Marshal(l) + require.NoError(t, err) + require.Equal(t, `{"Streams":"blockAll","Memory":"blockAll"}`, string(out)) + + l2 := ResourceLimits{} + err = json.Unmarshal([]byte(`{"Streams":0,"Memory":0}`), &l2) + require.NoError(t, err) + require.Equal(t, l, l2) + + l3 := ResourceLimits{} + err = json.Unmarshal([]byte(`{"Streams":0,"Memory":"0"}`), &l3) + require.NoError(t, err) + require.Equal(t, l, l3) +}