Skip to content

Commit

Permalink
Add a way to check if a IoEventLoopGroup / IoEventLoop is using a spe… (
Browse files Browse the repository at this point in the history
#14019)

…cific IoHandler

Motivation:

We need to provide a way for the user to check if an `IoEventLoopGroup`
is supporting NIO / Epoll / KQueue etc. Before people often used `group
instanceof NioEventLoopGroup` etc.

Modifications:

Add new method that people can use to check what kind of IO is used.

Result:

People can use `group.isIoType(NioHandler.class)` etc.

---------

Co-authored-by: Bryce Anderson <bryce_anderson@apple.com>
  • Loading branch information
normanmaurer and bryce-anderson committed May 2, 2024
1 parent b2060ea commit 062e9da
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
5 changes: 5 additions & 0 deletions transport/src/main/java/io/netty/channel/IoEventLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ default EventLoopGroup parent() {
*/
Future<IoRegistration> register(IoHandle handle, IoOps initialOps);

// Force sub-classes to implement.
@Override
boolean isCompatible(Class<? extends IoHandle> handleType);

// Force sub-classes to implement.
@Override
boolean isIoType(Class<? extends IoHandler> handlerType);
}
15 changes: 13 additions & 2 deletions transport/src/main/java/io/netty/channel/IoEventLoopGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ public interface IoEventLoopGroup extends EventLoopGroup {
IoEventLoop next();

/**
* Returns {@code true} if the given type is compatible with this {@link EventLoopGroup} and so can be registered
* to the contained {@link EventLoop}s, {@code false} otherwise.
* Returns {@code true} if the given type is compatible with this {@link IoEventLoopGroup} and so can be registered
* to the contained {@link IoEventLoop}s, {@code false} otherwise.
*
* @param handleType the type of the {@link IoHandle}.
* @return if compatible of not.
*/
default boolean isCompatible(Class<? extends IoHandle> handleType) {
return next().isCompatible(handleType);
}

/**
* Returns {@code true} if the given {@link IoHandler} type is used by this {@link IoEventLoopGroup},
* {@code false} otherwise.
*
* @param handlerType the type of the {@link IoHandler}.
* @return if used or not.
*/
default boolean isIoType(Class<? extends IoHandler> handlerType) {
return next().isIoType(handlerType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ protected final void cleanup() {
public boolean isCompatible(Class<? extends IoHandle> handleType) {
return ioHandler.isCompatible(handleType);
}

@Override
public boolean isIoType(Class<? extends IoHandler> handlerType) {
return ioHandler.getClass().equals(handlerType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2024 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;

import org.junit.jupiter.api.Test;

import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SingleThreadIoEventLoopTest {

@Test
void testIsIoType() {
IoHandler handler = new TestIoHandler();
IoHandler handler2 = new TestIoHandler() { };

IoEventLoopGroup group = new SingleThreadIoEventLoop(null, Executors.defaultThreadFactory(), handler);
assertTrue(group.isIoType(handler.getClass()));
assertFalse(group.isIoType(handler2.getClass()));
group.shutdownGracefully();
}

@Test
void testIsCompatible() {
IoHandler handler = new TestIoHandler() {
@Override
public boolean isCompatible(Class<? extends IoHandle> handleType) {
return handleType.equals(TestIoHandle.class);
}
};

IoHandle handle = new TestIoHandle() { };
IoEventLoopGroup group = new SingleThreadIoEventLoop(null, Executors.defaultThreadFactory(), handler);
assertTrue(group.isCompatible(TestIoHandle.class));
assertFalse(group.isCompatible(handle.getClass()));
group.shutdownGracefully();
}

private static class TestIoHandler implements IoHandler {
@Override
public int run(IoExecutionContext context) {
return 0;
}

@Override
public void prepareToDestroy() {
// NOOP
}

@Override
public void destroy() {
// NOOP
}

@Override
public IoRegistration register(IoEventLoop eventLoop, IoHandle handle, IoOps ops) {
return null;
}

@Override
public void wakeup(IoEventLoop eventLoop) {
// NOOP
}

@Override
public boolean isCompatible(Class<? extends IoHandle> handleType) {
return false;
}
}

private class TestIoHandle implements IoHandle {
@Override
public void handle(IoRegistration registration, IoOps readyOps) {
// NOOP
}

@Override
public void close() {
// NOOP
}
}
}

0 comments on commit 062e9da

Please sign in to comment.