What does UDPConn Close really do?
go, networking, udp
Solution
Good Question, let's see the code of `udpconn.Close`
http://golang.org/src/pkg/net/net.go?s=3725:3753#L124
func (c *conn) Close() error {
if !c.ok() {
return syscall.EINVAL
}
return c.fd.Close()
}
Closes `c.fd` but what is c.fd ?
type conn struct {
fd *netFD
}
ok is a `netFD` net File Descriptor. Let's look at the `Close` method.
func (fd *netFD) Close() error {
fd.pd.Lock() // needed for both fd.incref(true) and pollDesc.Evict
if !fd.fdmu.IncrefAndClose() {
fd.pd.Unlock()
return errClosing
}
// Unblock any I/O. Once it all unblocks and returns,
// so that it cannot be referring to fd.sysfd anymore,
// the final decref will close fd.sysfd. This should happen
// fairly quickly, since all the I/O is non-blocking, and any
// attempts to block in the pollDesc will return errClosing.
doWakeup := fd.pd.Evict()
fd.pd.Unlock()
fd.decref()
if doWakeup {
fd.pd.Wakeup()
}
return nil
}
Notice all the `decref` So to answer your question. Yes. Is good practice or you will leave hanging around in memory network file descriptors.
Problem
If UDP is a connectionless protocol, then why does `UDPConn` have a `Close` method? The documentation says "Close closes the connection", but UDP is connectionless. Is it good practice to call `Close` on a `UDPConn` object? Is there any benefit? http://golang.org/pkg/net/#UDPConn.Close