Skip to content

Commit

Permalink
Add a util method to warm up EventLoopGroup (#3610)
Browse files Browse the repository at this point in the history
Motivation:

Netty's event loop threads are spawned lazily, i.e. they are created
on the first task is scheduled on. A user sometimes wants to ensure
the event loop threads are started when the server starts up.

Modifications:

- Add `EventLoopGroups.warmUp()`

Result:

- Closes #3363
  • Loading branch information
kezhenxu94 authored Jun 14, 2021
1 parent 35473d2 commit 42bd0d7
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public final class EventLoopGroups {

private static final EventLoop directEventLoop = new DirectEventLoop();
private static final Runnable NO_OP = () -> {};

/**
* Returns a newly-created {@link EventLoopGroup}.
Expand Down Expand Up @@ -108,6 +109,18 @@ public static EventLoopGroup newEventLoopGroup(int numThreads, ThreadFactory thr
return type.newEventLoopGroup(numThreads, unused -> threadFactory);
}

/**
* Warms up all {@link EventLoop}s in the given {@code eventLoopGroup}
* by making sure all event loop threads are active.
*/
public static EventLoopGroup warmUp(EventLoopGroup eventLoopGroup) {
requireNonNull(eventLoopGroup, "eventLoopGroup");

eventLoopGroup.forEach(executor -> executor.submit(NO_OP));

return eventLoopGroup;
}

/**
* Returns a special {@link EventLoop} which executes submitted tasks in the caller thread.
* Note that this {@link EventLoop} will raise an {@link UnsupportedOperationException} for any operations
Expand Down

0 comments on commit 42bd0d7

Please sign in to comment.