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 — update cache read · netty/netty@8fd03cf

File tree

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

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslCachingKeyMaterialProvider.java
+53-123Lines changed: 53 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -16,168 +16,99 @@
1616
package io.netty.handler.ssl;
1717

1818
import io.netty.buffer.ByteBufAllocator;
19+
import io.netty.util.IllegalReferenceCountException;
1920

2021
import javax.net.ssl.X509KeyManager;
2122
import java.util.concurrent.ConcurrentHashMap;
2223

2324
/**
24-
* {@link OpenSslKeyMaterialProvider} that will cache the
25-
* {@link OpenSslKeyMaterial} to reduce the overhead of parsing the chain
26-
* and the key for generation of the material.
25+
* {@link OpenSslKeyMaterialProvider} that will cache the {@link OpenSslKeyMaterial} to reduce the overhead
26+
* of parsing the chain and the key for generation of the material.
2727
* <p>
28-
* All cache operations that access or evict entries perform retain/release
29-
* atomically via {@link ConcurrentHashMap}'s per-bucket locking.
30-
* When the cache is full on a cache miss, stale entries (whose alias is no
31-
* longer owned by the {@link X509KeyManager}) are evicted to make room
32-
* before inserting new material.
28+
* Cache reads are lock-free ({@link ConcurrentHashMap#get} + {@code retain()});
29+
* if a concurrent eviction releases the material in between, {@code retain()}
30+
* detects the dead reference count and the read falls back to a cache miss.
31+
* Mutations (insert, evict, destroy) use {@link ConcurrentHashMap#compute} /
32+
* {@link ConcurrentHashMap#computeIfPresent} for atomicity.
3333
*/
3434
final class OpenSslCachingKeyMaterialProvider extends OpenSslKeyMaterialProvider {
3535

36-
/**
37-
* A cache that performs retain/release atomically with map operations,
38-
* using {@link ConcurrentHashMap}'s per-bucket locking to prevent
39-
* use-after-free races between concurrent reads and evictions.
40-
*/
41-
private static final class KeyMaterialCache {
42-
43-
/**
44-
* Backing store for cached key materials.
45-
*/
46-
private final ConcurrentHashMap<String, OpenSslKeyMaterial> map =
47-
new ConcurrentHashMap<String, OpenSslKeyMaterial>();
48-
49-
/**
50-
* Returns the material for the given alias with its reference count
51-
* incremented, or {@code null} if absent.
52-
*
53-
* @param alias the alias to look up
54-
* @return the retained material, or {@code null} if not cached
55-
*/
56-
OpenSslKeyMaterial getAndRetain(final String alias) {
57-
final OpenSslKeyMaterial[] result = {null};
58-
map.computeIfPresent(alias, (final String k, final OpenSslKeyMaterial v) -> {
59-
v.retain();
60-
result[0] = v;
61-
return v;
62-
});
63-
return result[0];
64-
}
36+
private final int maxCachedEntries;
37+
private final ConcurrentHashMap<String, OpenSslKeyMaterial> cache =
38+
new ConcurrentHashMap<String, OpenSslKeyMaterial>();
6539

66-
/**
67-
* Inserts {@code material} if absent and returns it retained. If a
68-
* concurrent insert won, returns the existing entry retained instead;
69-
* the caller must then release {@code material}.
70-
*
71-
* @param alias the alias to insert under
72-
* @param material the material to insert
73-
* @return the retained material now in the cache
74-
*/
75-
OpenSslKeyMaterial putIfAbsentAndRetain(final String alias, final OpenSslKeyMaterial material) {
76-
final OpenSslKeyMaterial[] result = {null};
77-
map.compute(alias, (final String k, final OpenSslKeyMaterial existing) -> {
78-
if (existing != null) {
79-
existing.retain();
80-
result[0] = existing;
81-
return existing;
82-
}
83-
// Retain for the caller; the map holds the original reference.
84-
material.retain();
85-
result[0] = material;
86-
return material;
87-
});
88-
return result[0];
89-
}
40+
OpenSslCachingKeyMaterialProvider(X509KeyManager keyManager, String password, int maxEntries) {
41+
super(keyManager, password);
42+
maxCachedEntries = maxEntries;
43+
}
9044

91-
/**
92-
* Removes and releases the entry for the given alias, if present.
93-
*
94-
* @param alias the alias whose entry should be removed and released
95-
*/
96-
void removeAndRelease(final String alias) {
97-
map.computeIfPresent(alias, (final String k, final OpenSslKeyMaterial v) -> {
98-
v.release();
45+
/**
46+
* Lock-free cache lookup. If a concurrent eviction releases the material between
47+
* {@code get} and {@code retain}, the dead reference count is detected and treated
48+
* as a cache miss.
49+
*/
50+
private OpenSslKeyMaterial getAndRetain(String alias) {
51+
OpenSslKeyMaterial m = cache.get(alias);
52+
if (m != null) {
53+
try {
54+
return m.retain();
55+
} catch (IllegalReferenceCountException e) {
9956
return null;
100-
});
101-
}
102-
103-
/**
104-
* Returns the number of entries in the cache.
105-
*
106-
* @return the cache size
107-
*/
108-
int size() {
109-
return map.size();
110-
}
111-
112-
/**
113-
* Returns {@code true} if the cache contains no entries.
114-
*
115-
* @return {@code true} if empty
116-
*/
117-
boolean isEmpty() {
118-
return map.isEmpty();
119-
}
120-
121-
/**
122-
* Returns a view of the aliases currently in the cache.
123-
*
124-
* @return the set of aliases
125-
*/
126-
Iterable<String> aliases() {
127-
return map.keySet();
57+
}
12858
}
59+
return null;
12960
}
13061

13162
/**
132-
* Maximum number of entries to hold in the cache.
63+
* Atomically inserts material if absent, or retains the existing entry.
13364
*/
134-
private final int maxCachedEntries;
65+
private OpenSslKeyMaterial putIfAbsentAndRetain(String alias, OpenSslKeyMaterial material) {
66+
return cache.compute(alias, (k, existing) -> {
67+
if (existing != null) {
68+
existing.retain();
69+
return existing;
70+
}
71+
material.retain();
72+
return material;
73+
});
74+
}
13575

13676
/**
137-
* The key material cache.
77+
* Atomically removes and releases the entry for the given alias.
13878
*/
139-
private final KeyMaterialCache cache = new KeyMaterialCache();
140-
141-
OpenSslCachingKeyMaterialProvider(final X509KeyManager keyManager, final String password, final int maxEntries) {
142-
super(keyManager, password);
143-
maxCachedEntries = maxEntries;
79+
private void removeAndRelease(String alias) {
80+
cache.computeIfPresent(alias, (k, v) -> {
81+
v.release();
82+
return null;
83+
});
14484
}
14585

146-
/**
147-
* Removes cached entries whose alias is no longer recognized by the key
148-
* manager. Evicting only when full avoids per-insert overhead, which is
149-
* acceptable for most cases where {@code maxCachedEntries} is reasonably
150-
* sized.
151-
*/
15286
private void evictStaleEntries() {
153-
for (String alias : cache.aliases()) {
87+
for (String alias : cache.keySet()) {
15488
if (keyManager().getCertificateChain(alias) == null) {
155-
cache.removeAndRelease(alias);
89+
removeAndRelease(alias);
15690
}
15791
}
15892
}
15993

16094
@Override
161-
OpenSslKeyMaterial chooseKeyMaterial(final ByteBufAllocator allocator, final String alias) throws Exception {
162-
OpenSslKeyMaterial material = cache.getAndRetain(alias);
95+
OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception {
96+
OpenSslKeyMaterial material = getAndRetain(alias);
16397
if (material == null) {
16498
material = super.chooseKeyMaterial(allocator, alias);
16599
if (material == null) {
166-
// No keymaterial should be used.
167100
return null;
168101
}
169102

170103
if (cache.size() >= maxCachedEntries) {
171-
// Cache is full; try to evict stale entries to make room.
172104
evictStaleEntries();
173105
if (cache.size() >= maxCachedEntries) {
174-
// Still full after eviction, do not cache.
175106
return material;
176107
}
177108
}
178-
OpenSslKeyMaterial old = cache.putIfAbsentAndRetain(alias, material);
109+
// Returns the newly created material, or an existing entry if another thread inserted first.
110+
OpenSslKeyMaterial old = putIfAbsentAndRetain(alias, material);
179111
if (old != material) {
180-
// Another operation inserted first; release our copy.
181112
material.release();
182113
material = old;
183114
}
@@ -191,10 +122,9 @@ int cacheSize() {
191122

192123
@Override
193124
void destroy() {
194-
// Remove and release all entries.
195125
do {
196-
for (String alias : cache.aliases()) {
197-
cache.removeAndRelease(alias);
126+
for (String alias : cache.keySet()) {
127+
removeAndRelease(alias);
198128
}
199129
} while (!cache.isEmpty());
200130
}

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.