1
0
mirror of https://github.com/google/uuid.git synced 2025-07-03 00:56:49 +02:00

Fix govet issues and make node.go thread safe.

Includes changes from mischief and shawnps.
This commit is contained in:
Paul Borman
2016-02-09 09:58:31 -08:00
parent ae80070655
commit 6cc520cd8b
4 changed files with 110 additions and 53 deletions

20
node.go
View File

@ -4,9 +4,13 @@
package uuid
import "net"
import (
"net"
"sync"
)
var (
nodeMu sync.Mutex
interfaces []net.Interface // cached list of interfaces
ifname string // name of interface being used
nodeID []byte // hardware for version 1 UUIDs
@ -16,6 +20,8 @@ var (
// derived. The interface "user" is returned if the NodeID was set by
// SetNodeID.
func NodeInterface() string {
defer nodeMu.Unlock()
nodeMu.Lock()
return ifname
}
@ -26,6 +32,12 @@ func NodeInterface() string {
//
// SetNodeInterface never fails when name is "".
func SetNodeInterface(name string) bool {
defer nodeMu.Unlock()
nodeMu.Lock()
return setNodeInterface(name)
}
func setNodeInterface(name string) bool {
if interfaces == nil {
var err error
interfaces, err = net.Interfaces()
@ -59,8 +71,10 @@ func SetNodeInterface(name string) bool {
// NodeID returns a slice of a copy of the current Node ID, setting the Node ID
// if not already set.
func NodeID() []byte {
defer nodeMu.Unlock()
nodeMu.Lock()
if nodeID == nil {
SetNodeInterface("")
setNodeInterface("")
}
nid := make([]byte, 6)
copy(nid, nodeID)
@ -71,6 +85,8 @@ func NodeID() []byte {
// of id are used. If id is less than 6 bytes then false is returned and the
// Node ID is not set.
func SetNodeID(id []byte) bool {
defer nodeMu.Unlock()
nodeMu.Lock()
if setNodeID(id) {
ifname = "user"
return true