/** * Sends the result of the future downstream. * * @param future the future to consume the value of */ default void accept(ListenableFuture<? extends T> future) { Futures.addCallback(future, new FutureCallback<T>() { @Override public void onSuccess(T result) { success(result); } @Override public void onFailure(Throwable t) { error(t); } }, Execution.current().getEventLoop()); }
@Override public EventLoopGroup eventLoopGroup(ThreadFactory threadFactory) { return Execution.current().getController().getEventLoopGroup(); }
@Override @SuppressWarnings("unchecked") public T get() { Execution execution = getExecution(); HystrixCommandCache commandCache = execution.maybeGet(HystrixCommandCache.class) .orElseGet(() -> { HystrixCommandCache cache = new HystrixCommandCache(); execution.add(cache); return cache; }); Object command = commandCache.get(this); if (command == null) { command = rv.initialValue(); commandCache.putIfAbsent(this, command); } return (T) command; }
private Execution getExecution() { return Execution.current(); }
Execution.current().onComplete(() -> { if (byteBuf.refCnt() > 0) { byteBuf.release();
@Override protected ChannelPool newPool(HttpChannelKey key) { Bootstrap bootstrap = new Bootstrap() .remoteAddress(key.host, key.port) .group(key.execution.getEventLoop()) .channel(TransportDetector.getSocketChannelImpl()) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) key.connectTimeout.toMillis()) .option(ChannelOption.ALLOCATOR, spec.byteBufAllocator) .option(ChannelOption.AUTO_READ, false) .option(ChannelOption.SO_KEEPALIVE, isPooling()); if (isPooling()) { InstrumentedChannelPoolHandler channelPoolHandler = getPoolingHandler(key); hostStats.put(key.host, channelPoolHandler); CleanClosingFixedChannelPool channelPool = new CleanClosingFixedChannelPool(bootstrap, channelPoolHandler, getPoolSize(), getPoolQueueSize()); ((ExecControllerInternal) key.execution.getController()).onClose(() -> { remove(key); channelPool.closeCleanly(); }); return channelPool; } else { InstrumentedChannelPoolHandler channelPoolHandler = getSimpleHandler(key); hostStats.put(key.host, channelPoolHandler); return new SimpleChannelPool(bootstrap, channelPoolHandler, ALWAYS_UNHEALTHY); } } };
public void handle(Context ctx) throws ExecutionException { PathBindingStorage pathBindings = ctx.getExecution().get(PathBindingStorage.TYPE); PathBinding pathBinding = pathBindings.peek(); Optional<PathBinding> newBinding = cache.get(pathBinding, binder::bind); if (newBinding.isPresent()) { pathBindings.push(newBinding.get()); ctx.insert(handler); } else { ctx.next(); } }
ctx.getExecution().add(Form.class, form); ctx.next(); } else {
@Override public int hashCode() { int result = ssl ? 1 : 0; result = 31 * result + port; result = 31 * result + host.hashCode(); result = 31 * result + execution.getController().hashCode(); return result; }
@Override public Throwable apply(Throwable throwable, Object o) { Throwable t; if (throwable instanceof UnsupportedOperationException) { t = throwable.getCause(); } else if (throwable instanceof UndeclaredThrowableException) { t = ((UndeclaredThrowableException) throwable).getUndeclaredThrowable(); } else { t = throwable; } if (Execution.isActive()) { Promise.error(t).then(Action.noop()); } return t; } }
@Override public void connect(final Downstream<? super T> downstream) throws Exception { channelPool.acquire().addListener(acquireFuture -> { if (acquireFuture.isSuccess()) { Channel channel = (Channel) acquireFuture.getNow(); if (channel.eventLoop().equals(execution.getEventLoop())) { send(downstream, channel); } else { channel.deregister().addListener(deregisterFuture -> execution.getEventLoop().register(channel).addListener(registerFuture -> { if (registerFuture.isSuccess()) { send(downstream, channel); } else { channel.close(); channelPool.release(channel); connectFailure(downstream, registerFuture.cause()); } }) ); } } else { connectFailure(downstream, acquireFuture.cause()); } }); }
/** * * @param execution the execution that this segment belongs to * @param execType indicates whether this segment is execution on a compute or blocking thread * @param executionSegment the execution segment that is to be executed * @throws Exception any */ public void intercept(Execution execution, ExecType execType, Block executionSegment) throws Exception { MDCHolder holder = execution.maybeGet(MDCHolder.TYPE).orElse(null); if (holder == null) { MDC.clear(); holder = new MDCHolder(); init.execute(execution); execution.add(MDCHolder.TYPE, holder); } else { if (holder.map == null) { MDC.clear(); } else { MDC.setContextMap(holder.map); } } try { executionSegment.execute(); } finally { holder.map = MDC.getCopyOfContextMap(); MDC.clear(); } } }
@Override public Promise<ReceivedResponse> request(URI uri, final Action<? super RequestSpec> requestConfigurer) { return intercept( Promise.async(downstream -> new ContentAggregatingRequestAction(uri, this, 0, Execution.current(), requestConfigurer.append(spec.requestInterceptor)).connect(downstream)), spec.responseInterceptor, spec.errorInterceptor ); }
public void handle(Context context) throws Exception { String path = context.getExecution().get(PathBindingStorage.TYPE).peek().getPastBinding(); String decodedPath = decodeComponent(path, CharsetUtil.UTF_8); Path asset = context.file(decodedPath); if (asset == null) { context.clientError(404); } else { servePath(context, asset); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } HttpChannelKey that = (HttpChannelKey) o; return execution.getController() == that.execution.getController() && ssl == that.ssl && port == that.port && host.equals(that.host); }
private void drainRequestBody(Consumer<Throwable> next) { if (requestBody == null || !requestBody.isUnread()) { next.accept(null); } else { if (Execution.isActive()) { Promise.async(down -> requestBody.drain(e -> { if (e == null) { down.success(null); } else { down.error(e); } }) ) .onError(next::accept) .then(n -> next.accept(null)); } else { requestBody.drain(next); } } }
/** * Creates a promise for an (open) async file channel. * <p> * Uses {@link AsynchronousFileChannel#open(Path, Set, ExecutorService, FileAttribute[])}, * but uses the current execution's event loop as the executor service. * * @param file The path of the file to open or create * @param options Options specifying how the file is opened * @param attrs An optional list of file attributes to set atomically when creating the file * @see AsynchronousFileChannel#open(Path, Set, ExecutorService, FileAttribute[]) * @see #open(Path, OpenOption...) * @return a promise for an open async file channel */ public static Promise<AsynchronousFileChannel> open(Path file, Set<? extends OpenOption> options, FileAttribute<?>... attrs) { return Blocking.get(() -> AsynchronousFileChannel.open(file, options, Execution.current().getEventLoop(), attrs)); }
@Override public Promise<StreamedResponse> requestStream(URI uri, Action<? super RequestSpec> requestConfigurer) { return intercept( Promise.async(downstream -> new ContentStreamingRequestAction(uri, this, 0, Execution.current(), requestConfigurer.append(spec.requestInterceptor)).connect(downstream)), spec.responseInterceptor, spec.errorInterceptor ); }