Skip to content

Commit

Permalink
netutil: add ipmap method clear
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Oct 12, 2021
1 parent 3307441 commit 9eb5f40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions netutil/ipmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func NewIPMap(hint int) (m *IPMap) {
}
}

// Clear clears the map but retains its allocated storage. Calling Clear on
// a nil *IPMap has no effect, just like calling delete in a loop on an empty
// map doesn't.
func (m *IPMap) Clear() {
if m == nil {
return
}

// This is optimized, see https://github.com/golang/go/issues/20138.
for k := range m.m {
delete(m.m, k)
}
}

// Del deletes ip from the map. Calling Del on a nil *IPMap has no effect, just
// like delete on an empty map doesn't.
func (m *IPMap) Del(ip net.IP) {
Expand Down
4 changes: 4 additions & 0 deletions netutil/ipmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func TestIPMap(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var m *netutil.IPMap

assert.NotPanics(t, func() {
m.Clear()
})

assert.NotPanics(t, func() {
m.Del(ip4)
m.Del(ip6)
Expand Down

0 comments on commit 9eb5f40

Please sign in to comment.