Przeglądaj źródła

Make getting & setting unique ips atomic

gabrielseibel1 2 lat temu
rodzic
commit
3259141102
3 zmienionych plików z 9 dodań i 8 usunięć
  1. 4 0
      logic/extpeers.go
  2. 0 8
      logic/networks.go
  3. 5 0
      logic/nodes.go

+ 4 - 0
logic/extpeers.go

@@ -151,6 +151,10 @@ func GetExtClientByPubKey(publicKey string, network string) (*models.ExtClient,
 // CreateExtClient - creates an extclient
 func CreateExtClient(extclient *models.ExtClient) error {
 
+	// lock because we need unique IPs and having it concurrent makes parallel calls result in same "unique" IPs
+	addressLock.Lock()
+	defer addressLock.Unlock()
+
 	if len(extclient.PublicKey) == 0 {
 		privateKey, err := wgtypes.GeneratePrivateKey()
 		if err != nil {

+ 0 - 8
logic/networks.go

@@ -151,10 +151,6 @@ func GetNetworkSettings(networkname string) (models.Network, error) {
 // 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)
@@ -243,10 +239,6 @@ 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)

+ 5 - 0
logic/nodes.go

@@ -460,6 +460,11 @@ func updateProNodeACLS(node *models.Node) error {
 
 // createNode - creates a node in database
 func createNode(node *models.Node) error {
+
+	// lock because we need unique IPs and having it concurrent makes parallel calls result in same "unique" IPs
+	addressLock.Lock()
+	defer addressLock.Unlock()
+
 	host, err := GetHost(node.HostID.String())
 	if err != nil {
 		return err