use of io.netty.channel.EventLoop in project zuul by Netflix.
the class PerServerConnectionPool method release.
@Override
public boolean release(PooledConnection conn) {
if (conn == null) {
return false;
}
if (conn.isInPool()) {
return false;
}
// Get the eventloop for this channel.
EventLoop eventLoop = conn.getChannel().eventLoop();
Deque<PooledConnection> connections = getPoolForEventLoop(eventLoop);
CurrentPassport passport = CurrentPassport.fromChannel(conn.getChannel());
// Discard conn if already at least above waterline in the pool already for this server.
int poolWaterline = config.perServerWaterline();
if (poolWaterline > -1 && connections.size() >= poolWaterline) {
closeAboveHighWaterMarkCounter.increment();
conn.close();
conn.setInPool(false);
return false;
} else // Attempt to return connection to the pool.
if (connections.offer(conn)) {
conn.setInPool(true);
connsInPool.incrementAndGet();
passport.add(PassportState.ORIGIN_CH_POOL_RETURNED);
return true;
} else {
// If the pool is full, then close the conn and discard.
conn.close();
conn.setInPool(false);
return false;
}
}
use of io.netty.channel.EventLoop in project LanternServer by LanternPowered.
the class NetworkSession method sendWithFuture.
/**
* Sends a array of {@link Message}s and returns the {@link ChannelFuture}.
*
* @param messages The messages
* @return The channel future
*/
public ChannelFuture sendWithFuture(Message... messages) {
checkNotNull(messages, "messages");
checkArgument(messages.length != 0, "messages cannot be empty");
final ChannelPromise promise = this.channel.newPromise();
if (!this.channel.isActive()) {
return promise;
}
promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
// there is only one message.
if (messages.length == 1) {
this.channel.writeAndFlush(messages[0], promise);
} else {
final EventLoop eventLoop = this.channel.eventLoop();
final ChannelPromise voidPromise = this.channel.voidPromise();
if (eventLoop.inEventLoop()) {
final int last = messages.length - 1;
for (int i = 0; i < last; i++) {
ReferenceCountUtil.retain(messages[i]);
this.channel.writeAndFlush(messages[i], voidPromise);
}
ReferenceCountUtil.retain(messages[last]);
this.channel.writeAndFlush(messages[last], promise);
} else {
// If there are more then one message, combine them inside the
// event loop to reduce overhead of wakeup calls and object creation
// Create a copy of the list, to avoid concurrent modifications
final List<Message> messages0 = ImmutableList.copyOf(messages);
messages0.forEach(ReferenceCountUtil::retain);
eventLoop.submit(() -> {
final Iterator<Message> it0 = messages0.iterator();
do {
final Message message0 = it0.next();
// Only use a normal channel promise for the last message
this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
} while (it0.hasNext());
});
}
}
return promise;
}
use of io.netty.channel.EventLoop in project cassandra by apache.
the class NettyStreamingConnectionFactory method connect.
public static NettyStreamingChannel connect(OutboundConnectionSettings template, int messagingVersion, StreamingChannel.Kind kind) throws IOException {
EventLoop eventLoop = MessagingService.instance().socketFactory.outboundStreamingGroup().next();
int attempts = 0;
while (true) {
Future<Result<StreamingSuccess>> result = initiateStreaming(eventLoop, template.withDefaults(ConnectionCategory.STREAMING), messagingVersion);
// initiate has its own timeout, so this is "guaranteed" to return relatively promptly
result.awaitUninterruptibly();
if (result.isSuccess()) {
Channel channel = result.getNow().success().channel;
NettyStreamingChannel streamingChannel = new NettyStreamingChannel(messagingVersion, channel, kind);
if (kind == StreamingChannel.Kind.CONTROL) {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("stream", streamingChannel);
}
return streamingChannel;
}
if (++attempts == MAX_CONNECT_ATTEMPTS)
throw new IOException("failed to connect to " + template.to + " for streaming data", result.cause());
}
}
use of io.netty.channel.EventLoop in project cassandra by apache.
the class Dispatcher method flush.
private void flush(FlushItem<?> item) {
EventLoop loop = item.channel.eventLoop();
Flusher flusher = flusherLookup.get(loop);
if (flusher == null) {
Flusher created = useLegacyFlusher ? Flusher.legacy(loop) : Flusher.immediate(loop);
Flusher alt = flusherLookup.putIfAbsent(loop, flusher = created);
if (alt != null)
flusher = alt;
}
flusher.enqueue(item);
flusher.start();
}
use of io.netty.channel.EventLoop in project netty by netty.
the class AbstractEpollChannel method doClose.
@Override
protected void doClose() throws Exception {
active = false;
// Even if we allow half closed sockets we should give up on reading. Otherwise we may allow a read attempt on a
// socket which has not even been connected yet. This has been observed to block during unit tests.
inputClosedSeenErrorOnRead = true;
try {
ChannelPromise promise = connectPromise;
if (promise != null) {
// Use tryFailure() instead of setFailure() to avoid the race against cancel().
promise.tryFailure(new ClosedChannelException());
connectPromise = null;
}
Future<?> future = connectTimeoutFuture;
if (future != null) {
future.cancel(false);
connectTimeoutFuture = null;
}
if (isRegistered()) {
// Need to check if we are on the EventLoop as doClose() may be triggered by the GlobalEventExecutor
// if SO_LINGER is used.
//
// See https://github.com/netty/netty/issues/7159
EventLoop loop = eventLoop();
if (loop.inEventLoop()) {
doDeregister();
} else {
loop.execute(new Runnable() {
@Override
public void run() {
try {
doDeregister();
} catch (Throwable cause) {
pipeline().fireExceptionCaught(cause);
}
}
});
}
}
} finally {
socket.close();
}
}
Aggregations