Repository: syncthing/syncthing
File: lib/model/model.go (Go)

Code before:
						cfg.Devices[i].Name = hello.DeviceName
					}
					return
				}
			}
		})
	}

	m.deviceWasSeen(deviceID)
	m.scheduleConnectionPromotion()
}

func (m *model) scheduleConnectionPromotion() {
	// Keeps deferring to prevent multiple executions in quick succession,
	// e.g. if multiple connections to a single device are closed.
	m.promotionTimer.Reset(time.Second)
}

// promoteConnections checks for devices that have connections, but where
// the primary connection hasn't started index handlers etc. yet, and
// promotes the primary connection to be the index handling one. This should
// be called after adding new connections, and after closing a primary
// device connection.
func (m *model) promoteConnections() {
	// Slice of actions to take on connections after releasing the main
	// mutex. We do this so that we don not perform blocking network actions
	// inside the loop, and also to avoid a possible deadlock with calling
	// Start() on connections that are already executing a Close() with a
	// callback into the model...
	var postLockActions []func()

	m.mut.Lock()

	for deviceID, connIDs := range m.deviceConnIDs {
		cm, passwords := m.generateClusterConfigRLocked(deviceID)
		if m.promotedConnID[deviceID] != connIDs[0] {
			// The previously promoted connection is not the current
			// primary; we should promote the primary connection to be the
			// index handling one. We do this by sending a ClusterConfig on
			// it, which will cause the other side to start sending us index
			// messages there. (On our side, we manage index handlers based
			// on where we get ClusterConfigs from the peer.)
			conn := m.connections[connIDs[0]]
			l.Debugf("Promoting connection to %s at %s", deviceID.Short(), conn)
			postLockActions = append(postLockActions, func() {
				if conn.Statistics().StartedAt.IsZero() {
					conn.SetFolderPasswords(passwords)
					conn.Start()
				}
				conn.ClusterConfig(cm)
			})

Review comment (suggestion):
```suggestion
	// mutex. We do this so that we do not perform blocking network actions
```

Code after:
						cfg.Devices[i].Name = hello.DeviceName
					}
					return
				}
			}
		})
	}

	m.deviceWasSeen(deviceID)
	m.scheduleConnectionPromotion()
}

func (m *model) scheduleConnectionPromotion() {
	// Keeps deferring to prevent multiple executions in quick succession,
	// e.g. if multiple connections to a single device are closed.
	m.promotionTimer.Reset(time.Second)
}

// promoteConnections checks for devices that have connections, but where
// the primary connection hasn't started index handlers etc. yet, and
// promotes the primary connection to be the index handling one. This should
// be called after adding new connections, and after closing a primary
// device connection.
func (m *model) promoteConnections() {
	// Slice of actions to take on connections after releasing the main
	// mutex. We do this so that we do not perform blocking network actions
	// inside the loop, and also to avoid a possible deadlock with calling
	// Start() on connections that are already executing a Close() with a
	// callback into the model...
	var postLockActions []func()

	m.mut.Lock()

	for deviceID, connIDs := range m.deviceConnIDs {
		cm, passwords := m.generateClusterConfigRLocked(deviceID)
		if m.promotedConnID[deviceID] != connIDs[0] {
			// The previously promoted connection is not the current
			// primary; we should promote the primary connection to be the
			// index handling one. We do this by sending a ClusterConfig on
			// it, which will cause the other side to start sending us index
			// messages there. (On our side, we manage index handlers based
			// on where we get ClusterConfigs from the peer.)
			conn := m.connections[connIDs[0]]
			l.Debugf("Promoting connection to %s at %s", deviceID.Short(), conn)
			postLockActions = append(postLockActions, func() {
				if conn.Statistics().StartedAt.IsZero() {
					conn.SetFolderPasswords(passwords)
					conn.Start()
				}
				conn.ClusterConfig(cm)
			})