use of io.netty.channel.EventLoop in project netty by netty.
the class SimpleChannelPool method release.
@Override
public Future<Void> release(final Channel channel, final Promise<Void> promise) {
try {
checkNotNull(channel, "channel");
checkNotNull(promise, "promise");
EventLoop loop = channel.eventLoop();
if (loop.inEventLoop()) {
doReleaseChannel(channel, promise);
} else {
loop.execute(new Runnable() {
@Override
public void run() {
doReleaseChannel(channel, promise);
}
});
}
} catch (Throwable cause) {
closeAndFail(channel, cause, promise);
}
return promise;
}
use of io.netty.channel.EventLoop in project netty by netty.
the class SimpleChannelPool method acquireHealthyFromPoolOrNew.
/**
* Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
* @param promise the promise to provide acquire result.
* @return future for acquiring a channel.
*/
private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise) {
try {
final Channel ch = pollChannel();
if (ch == null) {
// No Channel left in the pool bootstrap a new Channel
Bootstrap bs = bootstrap.clone();
bs.attr(POOL_KEY, this);
ChannelFuture f = connectChannel(bs);
if (f.isDone()) {
notifyConnect(f, promise);
} else {
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
notifyConnect(future, promise);
}
});
}
} else {
EventLoop loop = ch.eventLoop();
if (loop.inEventLoop()) {
doHealthCheck(ch, promise);
} else {
loop.execute(new Runnable() {
@Override
public void run() {
doHealthCheck(ch, promise);
}
});
}
}
} catch (Throwable cause) {
promise.tryFailure(cause);
}
return promise;
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCacheTest method testAddMultipleAddressesForSameHostname.
@Test
public void testAddMultipleAddressesForSameHostname() throws Exception {
InetAddress addr1 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 });
InetAddress addr2 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 });
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCache cache = new DefaultDnsCache();
cache.cache("netty.io", null, addr1, 1, loop);
cache.cache("netty.io", null, addr2, 10000, loop);
List<? extends DnsCacheEntry> entries = cache.get("netty.io", null);
assertEquals(2, entries.size());
assertEntry(entries.get(0), addr1);
assertEntry(entries.get(1), addr2);
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DefaultDnsCacheTest method testDotHandling.
@Test
public void testDotHandling() throws Exception {
InetAddress addr1 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 1 });
InetAddress addr2 = InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 });
EventLoopGroup group = new DefaultEventLoopGroup(1);
try {
EventLoop loop = group.next();
final DefaultDnsCache cache = new DefaultDnsCache(1, 100, 100);
cache.cache("netty.io", null, addr1, 10000, loop);
cache.cache("netty.io.", null, addr2, 10000, loop);
List<? extends DnsCacheEntry> entries = cache.get("netty.io", null);
assertEquals(2, entries.size());
assertEntry(entries.get(0), addr1);
assertEntry(entries.get(1), addr2);
List<? extends DnsCacheEntry> entries2 = cache.get("netty.io.", null);
assertEquals(2, entries2.size());
assertEntry(entries2.get(0), addr1);
assertEntry(entries2.get(1), addr2);
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project netty by netty.
the class DnsNameResolverTest method testCachesClearedOnClose.
@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testCachesClearedOnClose() throws Exception {
final CountDownLatch resolveLatch = new CountDownLatch(1);
final CountDownLatch authoritativeLatch = new CountDownLatch(1);
DnsNameResolver resolver = newResolver().resolveCache(new DnsCache() {
@Override
public void clear() {
resolveLatch.countDown();
}
@Override
public boolean clear(String hostname) {
return false;
}
@Override
public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
return null;
}
@Override
public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, InetAddress address, long originalTtl, EventLoop loop) {
return null;
}
@Override
public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, Throwable cause, EventLoop loop) {
return null;
}
}).authoritativeDnsServerCache(new DnsCache() {
@Override
public void clear() {
authoritativeLatch.countDown();
}
@Override
public boolean clear(String hostname) {
return false;
}
@Override
public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
return null;
}
@Override
public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, InetAddress address, long originalTtl, EventLoop loop) {
return null;
}
@Override
public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, Throwable cause, EventLoop loop) {
return null;
}
}).build();
resolver.close();
resolveLatch.await();
authoritativeLatch.await();
}
Aggregations