GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow to reuse more then one session per host / port mapping (#14356)… · netty/netty@109567c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src/main/java/io/netty/handler/ssl
    • OpenSslClientSessionCache.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OpenSslClientSessionCache.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslClientSessionCache.java
+53-11Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
import io.netty.internal.tcnative.SSL;
1919
import io.netty.util.AsciiString;
2020

21+
import java.util.ArrayList;
2122
import java.util.HashMap;
23+
import java.util.HashSet;
24+
import java.util.List;
2225
import java.util.Map;
26+
import java.util.Set;
2327

2428
/**
2529
* {@link OpenSslSessionCache} that is used by the client-side.
2630
*/
2731
final class OpenSslClientSessionCache extends OpenSslSessionCache {
28-
// TODO: Should we support to have a List of OpenSslSessions for a Host/Port key and so be able to
29-
// support sessions for different protocols / ciphers to the same remote peer ?
30-
private final Map<HostPort, NativeSslSession> sessions = new HashMap<HostPort, NativeSslSession>();
32+
private final Map<HostPort, Set<NativeSslSession>> sessions = new HashMap<HostPort, Set<NativeSslSession>>();
3133

3234
OpenSslClientSessionCache(OpenSslEngineMap engineMap) {
3335
super(engineMap);
@@ -37,10 +39,17 @@ final class OpenSslClientSessionCache extends OpenSslSessionCache {
3739
protected boolean sessionCreated(NativeSslSession session) {
3840
assert Thread.holdsLock(this);
3941
HostPort hostPort = keyFor(session.getPeerHost(), session.getPeerPort());
40-
if (hostPort == null || sessions.containsKey(hostPort)) {
42+
if (hostPort == null) {
4143
return false;
4244
}
43-
sessions.put(hostPort, session);
45+
Set<NativeSslSession> sessionsForHost = sessions.get(hostPort);
46+
if (sessionsForHost == null) {
47+
// Let's start with something small as usually the server does not provide too many of these per hostPort
48+
// mapping.
49+
sessionsForHost = new HashSet<NativeSslSession>(4);
50+
sessions.put(hostPort, sessionsForHost);
51+
}
52+
sessionsForHost.add(session);
4453
return true;
4554
}
4655

@@ -51,7 +60,13 @@ protected void sessionRemoved(NativeSslSession session) {
5160
if (hostPort == null) {
5261
return;
5362
}
54-
sessions.remove(hostPort);
63+
Set<NativeSslSession> sessionsForHost = sessions.get(hostPort);
64+
if (sessionsForHost != null) {
65+
sessionsForHost.remove(session);
66+
if (sessionsForHost.isEmpty()) {
67+
sessions.remove(hostPort);
68+
}
69+
}
5570
}
5671

5772
@Override
@@ -60,18 +75,45 @@ boolean setSession(long ssl, OpenSslSession session, String host, int port) {
6075
if (hostPort == null) {
6176
return false;
6277
}
63-
final NativeSslSession nativeSslSession;
78+
NativeSslSession nativeSslSession = null;
6479
final boolean reused;
6580
boolean singleUsed = false;
6681
synchronized (this) {
67-
nativeSslSession = sessions.get(hostPort);
68-
if (nativeSslSession == null) {
82+
Set<NativeSslSession> sessionsForHost = sessions.get(hostPort);
83+
if (sessionsForHost == null) {
6984
return false;
7085
}
71-
if (!nativeSslSession.isValid()) {
72-
removeSessionWithId(nativeSslSession.sessionId());
86+
if (sessionsForHost.isEmpty()) {
87+
sessions.remove(hostPort);
88+
// There is no session that we can use.
7389
return false;
7490
}
91+
92+
List<NativeSslSession> toBeRemoved = null;
93+
// Loop through all the sessions that might be usable and check if we can use one of these.
94+
for (NativeSslSession sslSession : sessionsForHost) {
95+
if (sslSession.isValid()) {
96+
nativeSslSession = sslSession;
97+
break;
98+
} else {
99+
if (toBeRemoved == null) {
100+
toBeRemoved = new ArrayList<NativeSslSession>(2);
101+
}
102+
toBeRemoved.add(nativeSslSession);
103+
}
104+
}
105+
106+
// Remove everything that is not valid anymore
107+
if (toBeRemoved != null) {
108+
for (NativeSslSession sslSession : toBeRemoved) {
109+
removeSessionWithId(sslSession.sessionId());
110+
}
111+
}
112+
if (nativeSslSession == null) {
113+
// Couldn't find a valid session that could be used.
114+
return false;
115+
}
116+
75117
// Try to set the session, if true is returned OpenSSL incremented the reference count
76118
// of the underlying SSL_SESSION*.
77119
reused = SSL.setSession(ssl, nativeSslSession.session());

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.