Browse Source

handle host port mutex

abhishek9686 2 weeks ago
parent
commit
9028dc21f6
1 changed files with 6 additions and 4 deletions
  1. 6 4
      logic/hosts.go

+ 6 - 4
logic/hosts.go

@@ -621,15 +621,12 @@ func CheckHostPorts(h *models.Host) (changed bool) {
 	if h.IsStaticPort {
 		return false
 	}
-	hostPortMutex.Lock()
-	defer hostPortMutex.Unlock()
-	utils.TraceCaller()
-
 	if h.EndpointIP == nil {
 		return
 	}
 
 	// Get the current host from database to check if it already has a valid port assigned
+	// This check happens before the mutex to avoid unnecessary locking
 	currentHost, err := GetHost(h.ID.String())
 	if err == nil && currentHost.ListenPort > 0 {
 		// If the host already has a port in the database, use that instead of the incoming port
@@ -639,6 +636,11 @@ func CheckHostPorts(h *models.Host) (changed bool) {
 		}
 	}
 
+	// Only acquire mutex when we need to check for port conflicts
+	// This reduces contention for the common case where ports are already valid
+	hostPortMutex.Lock()
+	defer hostPortMutex.Unlock()
+
 	originalPort := h.ListenPort
 	defer func() {
 		if originalPort != h.ListenPort {