List of usage examples for io.netty.util.concurrent Future isSuccess
boolean isSuccess();
From source file:com.linecorp.armeria.client.endpoint.dns.DnsEndpointGroup.java
License:Apache License
private void onDnsRecords(Future<? super List<DnsRecord>> future) { if (stopped) { if (future.isSuccess()) { @SuppressWarnings("unchecked") final List<DnsRecord> result = (List<DnsRecord>) future.getNow(); result.forEach(ReferenceCountUtil::safeRelease); }// ww w. ja va2s. co m return; } if (!future.isSuccess()) { // Failed. Try again with the delay given by Backoff. final long delayMillis = backoff.nextDelayMillis(attemptsSoFar); logger.warn("{} DNS query failed; retrying in {} ms (attempts so far: {}):", logPrefix, delayMillis, attemptsSoFar, future.cause()); scheduledFuture = eventLoop.schedule(this::sendQueries, delayMillis, TimeUnit.MILLISECONDS); return; } // Reset the counter so that Backoff is reset. attemptsSoFar = 0; @SuppressWarnings("unchecked") final List<DnsRecord> records = (List<DnsRecord>) future.getNow(); final long serverTtl = records.stream().mapToLong(DnsRecord::timeToLive).min().orElse(minTtl); final int effectiveTtl = (int) Math.max(Math.min(serverTtl, maxTtl), minTtl); try { setEndpoints(onDnsRecords(records, effectiveTtl)); } catch (Throwable t) { logger.warn("{} Failed to process the DNS query result: {}", logPrefix, records, t); } finally { records.forEach(ReferenceCountUtil::safeRelease); scheduledFuture = eventLoop.schedule(this::sendQueries, effectiveTtl, TimeUnit.SECONDS); } }
From source file:com.linecorp.armeria.client.http.HttpClientDelegate.java
License:Apache License
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { final Endpoint endpoint = ctx.endpoint().resolve().withDefaultPort(ctx.sessionProtocol().defaultPort()); autoFillHeaders(ctx, endpoint, req); final PoolKey poolKey = new PoolKey(InetSocketAddress.createUnresolved(endpoint.host(), endpoint.port()), ctx.sessionProtocol());/*from ww w . j a v a 2 s .com*/ final EventLoop eventLoop = ctx.eventLoop(); final Future<Channel> channelFuture = pool(eventLoop).acquire(poolKey); final DecodedHttpResponse res = new DecodedHttpResponse(eventLoop); if (channelFuture.isDone()) { if (channelFuture.isSuccess()) { Channel ch = channelFuture.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } } else { channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { Channel ch = future.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } }); } return res; }
From source file:com.linecorp.armeria.client.HttpClientDelegate.java
License:Apache License
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { final Endpoint endpoint = ctx.endpoint().resolve(ctx).withDefaultPort(ctx.sessionProtocol().defaultPort()); autoFillHeaders(ctx, endpoint, req); if (!sanitizePath(req)) { req.abort();//w ww . j ava 2s . com return HttpResponse.ofFailure(new IllegalArgumentException("invalid path: " + req.path())); } final EventLoop eventLoop = ctx.eventLoop(); final PoolKey poolKey = new PoolKey(endpoint.host(), endpoint.ipAddr(), endpoint.port(), ctx.sessionProtocol()); final Future<Channel> channelFuture = factory.pool(eventLoop).acquire(poolKey); final DecodedHttpResponse res = new DecodedHttpResponse(eventLoop); if (channelFuture.isDone()) { if (channelFuture.isSuccess()) { final Channel ch = channelFuture.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } } else { channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { final Channel ch = future.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } }); } return res; }
From source file:com.linecorp.armeria.client.HttpRemoteInvoker.java
License:Apache License
@Override public <T> Future<T> invoke(EventLoop eventLoop, URI uri, ClientOptions options, ClientCodec codec, Method method, Object[] args) throws Exception { requireNonNull(uri, "uri"); requireNonNull(options, "options"); requireNonNull(codec, "codec"); requireNonNull(method, "method"); final Scheme scheme = Scheme.parse(uri.getScheme()); final SessionProtocol sessionProtocol = validateSessionProtocol(scheme.sessionProtocol()); final InetSocketAddress remoteAddress = convertToSocketAddress(uri, sessionProtocol.isTls()); final PoolKey poolKey = new PoolKey(remoteAddress, sessionProtocol); final Future<Channel> channelFuture = pool(eventLoop).acquire(poolKey); final Promise<T> resultPromise = eventLoop.newPromise(); codec.prepareRequest(method, args, resultPromise); if (channelFuture.isSuccess()) { Channel ch = channelFuture.getNow(); invoke0(codec, ch, method, args, options, resultPromise, poolKey); } else {// ww w. j a v a 2s . co m channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { Channel ch = future.getNow(); invoke0(codec, ch, method, args, options, resultPromise, poolKey); } else { resultPromise.setFailure(channelFuture.cause()); } }); } return resultPromise; }
From source file:com.linecorp.armeria.client.HttpRemoteInvoker.java
License:Apache License
static <T> void invoke0(ClientCodec codec, Channel channel, Method method, Object[] args, ClientOptions options, Promise<T> resultPromise, PoolKey poolKey) { final HttpSession session = HttpSessionHandler.get(channel); final SessionProtocol sessionProtocol = session.protocol(); if (sessionProtocol == null) { resultPromise.setFailure(ClosedSessionException.INSTANCE); return;//from www. j av a 2s.com } final EncodeResult encodeResult = codec.encodeRequest(channel, sessionProtocol, method, args); if (encodeResult.isSuccess()) { ServiceInvocationContext ctx = encodeResult.invocationContext(); Promise<FullHttpResponse> responsePromise = channel.eventLoop().newPromise(); final Invocation invocation = new Invocation(ctx, options, responsePromise, encodeResult.content()); //write request final ChannelFuture writeFuture = writeRequest(channel, invocation, ctx, options); writeFuture.addListener(fut -> { if (!fut.isSuccess()) { ctx.rejectPromise(responsePromise, fut.cause()); } else { long responseTimeoutMillis = options.responseTimeoutPolicy().timeout(ctx); scheduleTimeout(channel, responsePromise, responseTimeoutMillis, false); } }); //handle response if (responsePromise.isSuccess()) { decodeResult(codec, resultPromise, ctx, responsePromise.getNow()); } else { responsePromise.addListener((Future<FullHttpResponse> future) -> { if (future.isSuccess()) { decodeResult(codec, resultPromise, ctx, responsePromise.getNow()); } else { ctx.rejectPromise(resultPromise, future.cause()); } }); } } else { final Throwable cause = encodeResult.cause(); if (!resultPromise.tryFailure(cause)) { logger.warn("Failed to reject an invocation promise ({}) with {}", resultPromise, cause, cause); } } if (!session.onRequestSent()) { // Can't send a request via the current session anymore; do not return the channel to the pool. return; } // Return the channel to the pool. final KeyedChannelPool<PoolKey> pool = KeyedChannelPool.findPool(channel); if (sessionProtocol.isMultiplex()) { pool.release(poolKey, channel); } else { resultPromise.addListener(fut -> pool.release(poolKey, channel)); } }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private void notifyConnect(K key, Future<Channel> future, Promise<Channel> promise) { assert future.isDone(); try {/*from w w w . j ava 2 s. c o m*/ if (future.isSuccess()) { Channel channel = future.getNow(); channel.attr(KeyedChannelPoolUtil.POOL).set(this); channelPoolHandler.channelCreated(key, channel); channel.closeFuture().addListener(f -> channelPoolHandler.channelClosed(key, channel)); promise.setSuccess(channel); } else { promise.setFailure(future.cause()); } } catch (Exception e) { promise.setFailure(e); } }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private void notifyHealthCheck(final K key, Future<Boolean> future, Channel ch, Promise<Channel> promise) { assert ch.eventLoop().inEventLoop(); if (future.isSuccess()) { if (future.getNow() == Boolean.TRUE) { try { ch.attr(KeyedChannelPoolUtil.POOL).set(this); channelPoolHandler.channelAcquired(key, ch); promise.setSuccess(ch);/*ww w .j a va2s.c om*/ } catch (Throwable cause) { closeAndFail(ch, cause, promise); } } else { closeChannel(ch); acquireHealthyFromPoolOrNew(key, promise); } } else { closeChannel(ch); acquireHealthyFromPoolOrNew(key, promise); } }
From source file:com.linecorp.armeria.client.tracing.TracingRemoteInvoker.java
License:Apache License
/** * Returns client side annotations that should be added to span. *///from w w w.ja v a 2s.c o m @SuppressWarnings("UnusedParameters") protected <T> List<KeyValueAnnotation> annotations(URI uri, ClientOptions options, ClientCodec codec, Method method, Object[] args, @Nullable Future<? super T> result) { final KeyValueAnnotation clientUriAnnotation = KeyValueAnnotation.create("client.uri", uri.toString() + '#' + method.getName()); if (result == null || !result.isDone()) { return Collections.singletonList(clientUriAnnotation); } final List<KeyValueAnnotation> annotations = new ArrayList<>(3); annotations.add(clientUriAnnotation); final String clientResultText = result.isSuccess() ? "success" : "failure"; annotations.add(KeyValueAnnotation.create("client.result", clientResultText)); if (result.cause() != null) { annotations.add(KeyValueAnnotation.create("client.cause", result.cause().toString())); } return annotations; }
From source file:com.linecorp.armeria.server.tracing.TracingServiceInvocationHandler.java
License:Apache License
/** * Returns server side annotations that should be added to span. *//*from w ww . ja v a2 s .c o m*/ protected <T> List<KeyValueAnnotation> annotations(ServiceInvocationContext ctx, Future<? super T> result) { final List<KeyValueAnnotation> annotations = new ArrayList<>(5); final StringBuilder uriBuilder = new StringBuilder(); uriBuilder.append(ctx.scheme() != null ? ctx.scheme().uriText() : "unknown"); uriBuilder.append("://"); uriBuilder.append(ctx.host() != null ? ctx.host() : "<unknown-host>"); uriBuilder.append(ctx.path() != null ? ctx.path() : "/<unknown-path>"); if (ctx.method() != null) { uriBuilder.append('#'); uriBuilder.append(ctx.method()); } annotations.add(KeyValueAnnotation.create("server.uri", uriBuilder.toString())); if (ctx.remoteAddress() != null) { annotations.add(KeyValueAnnotation.create("server.remote", ctx.remoteAddress().toString())); } if (ctx.localAddress() != null) { annotations.add(KeyValueAnnotation.create("server.local", ctx.localAddress().toString())); } if (result != null && result.isDone()) { final String resultText = result.isSuccess() ? "success" : "failure"; annotations.add(KeyValueAnnotation.create("server.result", resultText)); if (result.cause() != null) { annotations.add(KeyValueAnnotation.create("server.cause", result.cause().toString())); } } return annotations; }
From source file:com.look.netty.demo.socksproxy.SocksServerConnectHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception { if (message instanceof Socks4CommandRequest) { final Socks4CommandRequest request = (Socks4CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override// www. jav a 2 s . c o m public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel() .writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.SUCCESS)); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); } else if (message instanceof Socks5CommandRequest) { final Socks5CommandRequest request = (Socks5CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse( Socks5CommandStatus.SUCCESS, request.dstAddrType())); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); } else { ctx.close(); } }