Skip to content

Commit 2c11bc1

Browse files
committed
Reduce contention in the selector pool
This moves the creation of a new selector outside of the sync section, to avoid blocking threads that are just trying to put a selector back when we are waiting to open a new one. This whole class could use some improvement using nonblocking collections, but this should help reduce contention for now. See #7805.
1 parent 0bfdc4d commit 2c11bc1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

core/src/main/java/org/jruby/util/io/SelectorPool.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public synchronized Selector get() throws IOException{
7474
* @return a java.nio.channels.Selector
7575
* @throws IOException if there's a problem opening a new selector
7676
*/
77-
public synchronized Selector get(SelectorProvider provider) throws IOException{
77+
public Selector get(SelectorProvider provider) throws IOException{
7878
return retrieveFromPool(provider);
7979
}
8080

@@ -125,11 +125,14 @@ public synchronized void cleanup() {
125125
}
126126

127127
private Selector retrieveFromPool(SelectorProvider provider) throws IOException {
128-
List<Selector> providerPool = pool.get(provider);
129-
if (providerPool != null && !providerPool.isEmpty()) {
130-
return providerPool.remove(providerPool.size() - 1);
128+
synchronized (this) {
129+
List<Selector> providerPool = pool.get(provider);
130+
if (providerPool != null && !providerPool.isEmpty()) {
131+
return providerPool.remove(providerPool.size() - 1);
132+
}
131133
}
132134

135+
// otherwise just return a new one
133136
return SelectorFactory.openWithRetryFrom(null, provider);
134137
}
135138

0 commit comments

Comments
 (0)