Skip to content

Commit

Permalink
NettyIoExecutors: allow passing 0 for N of IoThreads to infer defau…
Browse files Browse the repository at this point in the history
…lt (#2630)

Motivation:

Netty's `MultithreadEventLoopGroup` already has a logic to identify the
default number of EventLoop threads and exposes system properties to
influence these numbers. Users of Netty based frameworks expect these
properties to be taken into account:
- `io.netty.eventLoopThreads`
- `io.netty.availableProcessors`

Modifications:

- Allow users to pass `0` (zero) to `NettyIoExecutors.createIoExecutor`,
which is recognized by Netty as a special value that considers system
properties;

Result:

Users can use Netty's system properties to influence default Netty
behavior for number of EvenLoop threads.
  • Loading branch information
idelpivnitskiy authored Jun 29, 2023
1 parent 0f49071 commit 8492622
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static io.servicetalk.transport.netty.internal.NativeTransportUtils.isEpollAvailable;
import static io.servicetalk.transport.netty.internal.NativeTransportUtils.isIoUringAvailable;
import static io.servicetalk.transport.netty.internal.NativeTransportUtils.isKQueueAvailable;
import static java.lang.Runtime.getRuntime;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ public static EventLoopAwareNettyIoExecutor createIoExecutor(String threadNamePr
/**
* Create a new {@link NettyIoExecutor}.
*
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @return The created {@link IoExecutor}
*/
public static EventLoopAwareNettyIoExecutor createIoExecutor(int ioThreads) {
Expand All @@ -77,7 +76,7 @@ public static EventLoopAwareNettyIoExecutor createIoExecutor(int ioThreads) {
/**
* Create a new {@link NettyIoExecutor}.
*
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @param threadNamePrefix the name prefix used for the created {@link Thread}s.
* @return The created {@link IoExecutor}
*/
Expand All @@ -95,21 +94,20 @@ public static EventLoopAwareNettyIoExecutor createIoExecutor(int ioThreads, Stri
*/
public static <T extends Thread & IoThread> EventLoopAwareNettyIoExecutor createIoExecutor(
IoThreadFactory<T> threadFactory) {
return createIoExecutor(getRuntime().availableProcessors() * 2, threadFactory);
return createIoExecutor(0, threadFactory);
}

/**
* Create a new {@link NettyIoExecutor}.
*
* @param <T> Type of the IO thread instances created by factory.
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @param threadFactory the {@link IoThreadFactory} to use. If possible you should use an instance of
* {@link NettyIoThreadFactory} as it allows internal optimizations.
* @return The created {@link IoExecutor}
*/
public static <T extends Thread & IoThread> EventLoopAwareNettyIoExecutor createIoExecutor(
int ioThreads, IoThreadFactory<T> threadFactory) {
validateIoThreads(ioThreads);
return new EventLoopGroupIoExecutor(createEventLoopGroup(ioThreads, threadFactory), true, true);
}

Expand Down Expand Up @@ -193,7 +191,7 @@ public static NettyIoExecutor fromNettyEventLoopGroup(EventLoopGroup eventLoopGr
}

private static void validateIoThreads(final int ioThreads) {
if (ioThreads <= 0) {
if (ioThreads < 0) {
throw new IllegalArgumentException("ioThreads: " + ioThreads + " (expected >0)");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private NettyIoExecutors() {
* Creates a new {@link IoExecutor} with the specified number of {@code ioThreads}.
*
* @param <T> Type of the IO thread instances created by factory.
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @param threadFactory the {@link IoThreadFactory} to use.
* @return The created {@link IoExecutor}
*/
Expand All @@ -44,7 +44,7 @@ public static <T extends Thread & IoThread> IoExecutor createIoExecutor(int ioTh
/**
* Creates a new {@link IoExecutor} with the specified number of {@code ioThreads}.
*
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @return The created {@link IoExecutor}
*/
public static IoExecutor createIoExecutor(int ioThreads) {
Expand Down Expand Up @@ -75,7 +75,7 @@ public static IoExecutor createIoExecutor(String threadNamePrefix) {
/**
* Creates a new {@link IoExecutor} with the specified number of {@code ioThreads}.
*
* @param ioThreads number of threads.
* @param ioThreads number of threads or {@code 0} (zero) to use the default value.
* @param threadNamePrefix the name prefix used for the created {@link Thread}s.
* @return The created {@link IoExecutor}
*/
Expand Down

0 comments on commit 8492622

Please sign in to comment.