Provide handle Close() and deprecate Delete()

This commit is contained in:
TheDiveO 2021-09-04 16:27:58 +02:00 committed by Alessandro Boch
parent 871f8a156e
commit 626202eca8
7 changed files with 29 additions and 19 deletions

View File

@ -15,7 +15,7 @@ var pkgHandle = &Handle{}
// Handle is an handle for the netlink requests on a
// specific network namespace. All the requests on the
// same netlink family share the same netlink socket,
// which gets released when the handle is deleted.
// which gets released when the handle is Close'd.
type Handle struct {
sockets map[int]*nl.SocketHandle
lookupByDump bool
@ -136,14 +136,22 @@ func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error)
return h, nil
}
// Delete releases the resources allocated to this handle
func (h *Handle) Delete() {
// Close releases the resources allocated to this handle
func (h *Handle) Close() {
for _, sh := range h.sockets {
sh.Close()
}
h.sockets = nil
}
// Delete releases the resources allocated to this handle
//
// Deprecated: use Close instead which is in line with typical resource release
// patterns for files and other resources.
func (h *Handle) Delete() {
h.Close()
}
func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
// Do this so that package API still use nl package variable nextSeqNr
if h.sockets == nil {

View File

@ -12,6 +12,6 @@ func TestSetGetSocketTimeout(t *testing.T) {
}
if val := GetSocketTimeout(); val != timeout {
t.Fatalf("Unexpcted socket timeout value: got=%v, expected=%v", val, timeout)
t.Fatalf("Unexpected socket timeout value: got=%v, expected=%v", val, timeout)
}
}

View File

@ -19,7 +19,7 @@ import (
"golang.org/x/sys/unix"
)
func TestHandleCreateDelete(t *testing.T) {
func TestHandleCreateClose(t *testing.T) {
h, err := NewHandle()
if err != nil {
t.Fatal(err)
@ -34,9 +34,9 @@ func TestHandleCreateDelete(t *testing.T) {
}
}
h.Delete()
h.Close()
if h.sockets != nil {
t.Fatalf("Handle socket(s) were not destroyed")
t.Fatalf("Handle socket(s) were not closed")
}
}
@ -60,7 +60,7 @@ func TestHandleCreateNetns(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer ch.Delete()
defer ch.Close()
// Create an handle on a custom netns
newNs, err := netns.New()
@ -73,7 +73,7 @@ func TestHandleCreateNetns(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
// Create an interface using the current handle
err = ch.LinkAdd(&Dummy{LinkAttrs{Name: ifName}})
@ -119,7 +119,7 @@ func TestHandleTimeout(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer h.Delete()
defer h.Close()
for _, sh := range h.sockets {
verifySockTimeVal(t, sh.Socket.GetFd(), unix.Timeval{Sec: 0, Usec: 0})
@ -137,7 +137,7 @@ func TestHandleReceiveBuffer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer h.Delete()
defer h.Close()
if err := h.SetSocketReceiveBufferSize(65536, false); err != nil {
t.Fatal(err)
}
@ -255,10 +255,10 @@ func parallelDone() {
ns2.Close()
}
if handle1 != nil {
handle1.Delete()
handle1.Close()
}
if handle2 != nil {
handle2.Delete()
handle2.Close()
}
}
}

View File

@ -23,6 +23,8 @@ func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) {
return nil, ErrNotImplemented
}
func (h *Handle) Close() {}
func (h *Handle) Delete() {}
func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {

View File

@ -1830,7 +1830,7 @@ func TestLinkSubscribeAt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
// Subscribe for Link events on the custom netns
ch := make(chan LinkUpdate)
@ -1880,7 +1880,7 @@ func TestLinkSubscribeListExisting(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
link := &Veth{LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
if err := nh.LinkAdd(link); err != nil {

View File

@ -408,7 +408,7 @@ func TestNeighSubscribeAt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
// Subscribe for Neigh events on the custom netns
ch := make(chan NeighUpdate)
@ -462,7 +462,7 @@ func TestNeighSubscribeListExisting(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
dummy := &Dummy{LinkAttrs{Name: "neigh0"}}
if err := nh.LinkAdd(dummy); err != nil {

View File

@ -416,7 +416,7 @@ func TestRouteSubscribeAt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
// Subscribe for Route events on the custom netns
ch := make(chan RouteUpdate)
@ -474,7 +474,7 @@ func TestRouteSubscribeListExisting(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer nh.Delete()
defer nh.Close()
// get loopback interface
link, err := nh.LinkByName("lo")