Skip to content

Commit

Permalink
Add the handle to overload connection (#10783)
Browse files Browse the repository at this point in the history
* add the handle to overload connection

* fast return
  • Loading branch information
Daydreamer-ia authored Jul 17, 2023
1 parent 3374cf4 commit 318892d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ public void loadCount(int loadClient, String redirectAddress) {
*
* @param connectionId connection id of client.
* @param redirectAddress server address to redirect.
* @return whether remove connection.
*/
public void loadSingle(String connectionId, String redirectAddress) {
public boolean loadSingle(String connectionId, String redirectAddress) {
Connection connection = getConnection(connectionId);

if (connection != null) {
Expand All @@ -281,9 +282,11 @@ public void loadSingle(String connectionId, String redirectAddress) {
unregister(connectionId);
} catch (Exception e) {
LOGGER.error("error occurs when expel connection, connectionId: {} ", connectionId, e);
return false;
}
}
}
return true;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public NacosRuntimeConnectionEjector() {
* eject connections on runtime.
*/
public void doEject() {
// remove out dated connection
ejectOutdatedConnection();
// remove overload connection
ejectOverLimitConnection();
}

/**
* eject the outdated connection.
*/
private void ejectOutdatedConnection() {
try {

Loggers.CONNECTION.info("Connection check task start");
Expand Down Expand Up @@ -137,6 +147,49 @@ public void onException(Throwable e) {
Loggers.CONNECTION.error("Error occurs during connection check... ", e);
}
}

/**
* eject the over limit connection.
*/
private void ejectOverLimitConnection() {
// if not count set, then give up
if (getLoadClient() > 0) {
try {
Loggers.CONNECTION.info("Connection overLimit check task start, loadCount={}, redirectAddress={}",
getLoadClient(), getRedirectAddress());
// check count
int currentConnectionCount = connectionManager.getCurrentConnectionCount();
int ejectingCount = currentConnectionCount - getLoadClient();
// if overload
if (ejectingCount > 0) {
// we may modify the connection map when connection reset
// avoid concurrent modified exception, create new set for ids snapshot
Set<String> ids = new HashSet<>(connectionManager.connections.keySet());
for (String id : ids) {
if (ejectingCount > 0) {
// check sdk
Connection connection = connectionManager.getConnection(id);
if (connection != null && connection.getMetaInfo().isSdkSource()) {
if (connectionManager.loadSingle(id, redirectAddress)) {
ejectingCount--;
}
}
} else {
// reach the count
break;
}
}
}
Loggers.CONNECTION.info("Connection overLimit task end, current loadCount={}, has ejected loadCont={}",
connectionManager.getCurrentConnectionCount(), getLoadClient() - ejectingCount);
} catch (Throwable e) {
Loggers.CONNECTION.error("Error occurs during connection overLimit... ", e);
}
// reset
setRedirectAddress(null);
setLoadClient(-1);
}
}

@Override
public String getName() {
Expand Down

0 comments on commit 318892d

Please sign in to comment.