-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mysql: AFUNIXDatabaseSocketFactoryCJ: Respect timeouts
AFUNIXDatabaseSocketFactoryCJ.connect takes a "loginTimeout" parameter, and the system properties may specify a "connectTimeout". While connecting to AF_UNIX sockets appears to not block in the regular case, let's assume there's a corner case where respecting these values makes sense. Follow standard Mysql connector behavior and use these two timeouts to determine the connect timeout for our unix domain sockets. #154
- Loading branch information
1 parent
535b73a
commit 9524e32
Showing
3 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
junixsocket-mysql/src/main/java/org/newsclub/net/mysql/MysqlHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* junixsocket | ||
* | ||
* Copyright 2009-2023 Christian Kohlschütter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.newsclub.net.mysql; | ||
|
||
/** | ||
* Code sharable between code in junixsocket-mysql. | ||
* | ||
* @author Christian Kohlschütter | ||
*/ | ||
final class MysqlHelper { | ||
private MysqlHelper() { | ||
} | ||
|
||
/** | ||
* Returns the shorter timeout of two given timeouts (assuming 0 means "unlimited"). | ||
* | ||
* @param a The first timeout. | ||
* @param b The second timeout. | ||
* @return The shorter timeout. | ||
*/ | ||
static int shorterTimeout(int a, int b) { | ||
if (a == 0 || (b != 0 && b < a)) { | ||
return b; | ||
} else { | ||
return a; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...xsocket-mysql/src/test/java/org/newsclub/net/mysql/AFUNIXDatabaseSocketFactoryCJTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* junixsocket | ||
* | ||
* Copyright 2009-2023 Christian Kohlschütter | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.newsclub.net.mysql; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.newsclub.net.unix.AFUNIXServerSocket; | ||
import org.newsclub.net.unix.AFUNIXSocketAddress; | ||
|
||
import com.mysql.cj.conf.DefaultPropertySet; | ||
import com.mysql.cj.conf.StringPropertyDefinition; | ||
|
||
public class AFUNIXDatabaseSocketFactoryCJTest { | ||
|
||
@Test | ||
public void testConnectTimeout() throws Exception { | ||
AFUNIXSocketAddress addr = AFUNIXSocketAddress.ofNewTempFile(); | ||
try (AFUNIXServerSocket serverSocket = AFUNIXServerSocket.newInstance()) { | ||
serverSocket.bind(addr, 1); | ||
|
||
AFUNIXDatabaseSocketFactoryCJ sf = new AFUNIXDatabaseSocketFactoryCJ(); | ||
|
||
DefaultPropertySet props = new DefaultPropertySet(); | ||
props.addProperty(new StringPropertyDefinition("junixsocket.file", "junixsocket.file", addr | ||
.getFile().toString(), true, "description", "0", "category", 1).createRuntimeProperty()); | ||
|
||
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { | ||
sf.connect("localhost", 0, props, 1); | ||
}); | ||
} | ||
} | ||
|
||
} |