Przeglądaj źródła

Use a lock for getting unique ips

gabrielseibel1 2 lat temu
rodzic
commit
c6c861e1fe
1 zmienionych plików z 18 dodań i 1 usunięć
  1. 18 1
      logic/networks.go

+ 18 - 1
logic/networks.go

@@ -7,6 +7,7 @@ import (
 	"net"
 	"sort"
 	"strings"
+	"sync"
 
 	"github.com/c-robinson/iplib"
 	validator "github.com/go-playground/validator/v10"
@@ -147,8 +148,13 @@ func GetNetworkSettings(networkname string) (models.Network, error) {
 	return network, nil
 }
 
-// UniqueAddress - see if address is unique
+// UniqueAddress - get a unique ipv4 address
 func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
+
+	// getting unique addresses procedures are mutually exclusive
+	addressLock.Lock()
+	defer addressLock.Unlock()
+
 	add := net.IP{}
 	var network models.Network
 	network, err := GetParentNetwork(networkName)
@@ -236,6 +242,11 @@ func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
 
 // UniqueAddress6 - see if ipv6 address is unique
 func UniqueAddress6(networkName string, reverse bool) (net.IP, error) {
+
+	// getting unique addresses procedures are mutually exclusive
+	addressLock.Lock()
+	defer addressLock.Unlock()
+
 	add := net.IP{}
 	var network models.Network
 	network, err := GetParentNetwork(networkName)
@@ -424,3 +435,9 @@ func SortNetworks(unsortedNetworks []models.Network) {
 }
 
 // == Private ==
+
+var addressLock sync.Locker
+
+func init() {
+	addressLock = &sync.Mutex{}
+}