Skip to content

Commit

Permalink
Unmarshal 0 limits as block all
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jan 31, 2023
1 parent 15f6fa5 commit 56056a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
15 changes: 14 additions & 1 deletion p2p/host/resource-manager/limit_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions p2p/host/resource-manager/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 56056a3

Please sign in to comment.