changeset 842:99ed7dbb2e05 monetdbs

Cache the system trust roots between invocations Loading them is expensive, it easily takes 100-200 milliseconds.
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Wed, 03 Jan 2024 11:05:56 +0100 (15 months ago)
parents 15d606f44a26
children 3f288787fd77
files src/main/java/org/monetdb/mcl/net/SecureSocket.java
diffstat 1 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/monetdb/mcl/net/SecureSocket.java
+++ b/src/main/java/org/monetdb/mcl/net/SecureSocket.java
@@ -16,6 +16,18 @@ public class SecureSocket {
 	private static final String[] ENABLED_PROTOCOLS = {"TLSv1.3"};
 	private static final String[] APPLICATION_PROTOCOLS = {"mapi/9"};
 
+	// Cache for the default SSL factory. It must load all trust roots
+	// so it's worthwhile to cache.
+	// Only access this through #getDefaultSocketFactory()
+	private static SSLSocketFactory vanillaFactory = null;
+
+	private static synchronized SSLSocketFactory getDefaultSocketFactory() {
+		if (vanillaFactory == null) {
+			vanillaFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
+		}
+		return vanillaFactory;
+	}
+
 	public static Socket wrap(Target.Validated validated, Socket inner) throws IOException {
 		Target.Verify verify = validated.connectVerify();
 		SSLSocketFactory socketFactory;
@@ -23,7 +35,7 @@ public class SecureSocket {
 		try {
 			switch (verify) {
 				case System:
-					socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
+					socketFactory = getDefaultSocketFactory();
 					break;
 				case Cert:
 					KeyStore keyStore = keyStoreForCert(validated.getCert());