Skip to content

Commit

Permalink
If the client already disconnect, ignore this request to avoid NPE. (#…
Browse files Browse the repository at this point in the history
…6175)

* if the client already disconnect, ignore this request to avoid npe.

* unify code with DistroClientDataProcessor.

* print warn info when client already disconnect.

* transfer client check logic to method.

* if client is not ephemeral, is illegal.
  • Loading branch information
horizonzy authored Jun 30, 2021
1 parent 95892bc commit 3fd77bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public abstract class RpcClient implements Closeable {
protected ClientAbilities clientAbilities;

/**
* default keep alive time 10s.
* default keep alive time 5s.
*/
private long keepAliveTime = 5000L;

Expand Down Expand Up @@ -176,7 +176,6 @@ public RpcClient keepAlive(long keepAliveTime, TimeUnit timeUnit) {
this.keepAliveTime = keepAliveTime * timeUnit.toMillis(keepAliveTime);
LoggerUtils.printIfInfoEnabled(LOGGER, "[{}]RpcClient init keepalive time, keepAliveTimeMillis={}", name,
keepAliveTime);

return this;
}

Expand Down Expand Up @@ -706,7 +705,7 @@ public Response request(Request request, long timeoutMills) throws NacosExceptio
throw (exceptionThrow instanceof NacosException) ? (NacosException) exceptionThrow
: new NacosException(SERVER_ERROR, exceptionThrow);
} else {
throw new NacosException(SERVER_ERROR, "Request fail,Unknown Error");
throw new NacosException(SERVER_ERROR, "Request fail, unknown Error");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public EphemeralClientOperationServiceImpl(ClientManagerDelegate clientManager)
public void registerInstance(Service service, Instance instance, String clientId) {
Service singleton = ServiceManager.getInstance().getSingleton(service);
Client client = clientManager.getClient(clientId);
if (!clientIsLegal(client, clientId)) {
return;
}
InstancePublishInfo instanceInfo = getPublishInfo(instance);
client.addServiceInstance(singleton, instanceInfo);
client.setLastUpdatedTime();
Expand All @@ -65,6 +68,9 @@ public void deregisterInstance(Service service, Instance instance, String client
}
Service singleton = ServiceManager.getInstance().getSingleton(service);
Client client = clientManager.getClient(clientId);
if (!clientIsLegal(client, clientId)) {
return;
}
InstancePublishInfo removedInstance = client.removeServiceInstance(singleton);
client.setLastUpdatedTime();
if (null != removedInstance) {
Expand All @@ -78,6 +84,9 @@ public void deregisterInstance(Service service, Instance instance, String client
public void subscribeService(Service service, Subscriber subscriber, String clientId) {
Service singleton = ServiceManager.getInstance().getSingletonIfExist(service).orElse(service);
Client client = clientManager.getClient(clientId);
if (!clientIsLegal(client, clientId)) {
return;
}
client.addServiceSubscriber(singleton, subscriber);
client.setLastUpdatedTime();
NotifyCenter.publishEvent(new ClientOperationEvent.ClientSubscribeServiceEvent(singleton, clientId));
Expand All @@ -87,9 +96,23 @@ public void subscribeService(Service service, Subscriber subscriber, String clie
public void unsubscribeService(Service service, Subscriber subscriber, String clientId) {
Service singleton = ServiceManager.getInstance().getSingletonIfExist(service).orElse(service);
Client client = clientManager.getClient(clientId);
if (!clientIsLegal(client, clientId)) {
return;
}
client.removeServiceSubscriber(singleton);
client.setLastUpdatedTime();
NotifyCenter.publishEvent(new ClientOperationEvent.ClientUnsubscribeServiceEvent(singleton, clientId));

}

private boolean clientIsLegal(Client client, String clientId) {
if (client == null) {
Loggers.SRV_LOG.warn("Client connection {} already disconnect", clientId);
return false;
}
if (!client.isEphemeral()) {
Loggers.SRV_LOG.warn("Client connection {} type is not ephemeral", clientId);
return false;
}
return true;
}
}

0 comments on commit 3fd77bc

Please sign in to comment.