2014-07-14 3 views

Répondre

3

Bonne question, nous allons voir le code de 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() 
    } 

ferme c.fd mais ce qui est c.fd?

type conn struct { 
    fd *netFD 
} 

ok est un descripteur de fichier net netFD. Regardons la méthode Close.

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 

}

Avis tous les decref Pour répondre à votre question. Oui. Est-ce une bonne pratique ou vous laisserez traîner dans les descripteurs de fichiers de réseau de mémoire.