Skip to content

Commit

Permalink
ignore unknown and noauth exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 committed Apr 6, 2023
1 parent a3417fc commit 3af2dbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,40 +354,46 @@ private void initializeFromClientConfig(JedisClientConfig config) {
auth(credentialsProvider);
}

try {
List<CommandArguments> fireAndForgetMsg = new ArrayList<>();

int dbIndex = config.getDatabase();
if (dbIndex > 0) {
fireAndForgetMsg.add(new CommandArguments(Command.SELECT).add(Protocol.toByteArray(dbIndex)));
}
List<CommandArguments> fireAndForgetMsg = new ArrayList<>();

String clientName = config.getClientName();
if (clientName != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETNAME).add(clientName));
}
int dbIndex = config.getDatabase();
if (dbIndex > 0) {
fireAndForgetMsg.add(new CommandArguments(Command.SELECT).add(Protocol.toByteArray(dbIndex)));
}

String libName = JedisMetaInfo.getArtifactId();
if (libName != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttrOption.LIB_NAME.getRaw()).add(libName));
}
String clientName = config.getClientName();
if (clientName != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETNAME).add(clientName));
}

String libVersion = JedisMetaInfo.getVersion();
if (libVersion != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttrOption.LIB_VER.getRaw()).add(libVersion));
}
String libName = JedisMetaInfo.getArtifactId();
if (libName != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttrOption.LIB_NAME.getRaw()).add(libName));
}

for (CommandArguments arg : fireAndForgetMsg) {
sendCommand(arg);
}
String libVersion = JedisMetaInfo.getVersion();
if (libVersion != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttrOption.LIB_VER.getRaw()).add(libVersion));
}

// getMany and ignore result
getMany(fireAndForgetMsg.size());
for (CommandArguments arg : fireAndForgetMsg) {
sendCommand(arg);
}

} catch (JedisDataException e) {
// just ignore dataException, such as unknown command
List<Object> objects = getMany(fireAndForgetMsg.size());
for (Object obj : objects) {
if (obj instanceof JedisDataException) {
JedisDataException e = (JedisDataException)obj;
String errorMsg = e.getMessage().toUpperCase();
if (errorMsg.contains("UNKNOWN") ||
errorMsg.contains("NOAUTH")) {
// ignore
} else {
throw e;
}
}
}
} catch (JedisException je) {
try {
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/redis/clients/jedis/JedisPooledTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public void cleanUp() {
assertEquals(1, prepareCount.get());
assertEquals(1, cleanupCount.get());

// clear existing `noauth` links
pool.getPool().clear();

pool.set("foo", "bar");
assertEquals("bar", pool.get("foo"));
assertEquals(2, prepareCount.get());
Expand Down

0 comments on commit 3af2dbd

Please sign in to comment.