use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project redisson by redisson.
the class CommandAsyncService method handleBlockingOperations.
private <R, V> void handleBlockingOperations(final AsyncDetails<V, R> details, final RedisConnection connection, Long popTimeout) {
final FutureListener<Boolean> listener = new FutureListener<Boolean>() {
@Override
public void operationComplete(Future<Boolean> future) throws Exception {
details.getMainPromise().tryFailure(new RedissonShutdownException("Redisson is shutdown"));
}
};
final AtomicBoolean canceledByScheduler = new AtomicBoolean();
final Timeout scheduledFuture;
if (popTimeout != 0) {
// to handle cases when connection has been lost
final Channel orignalChannel = connection.getChannel();
scheduledFuture = connectionManager.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
// and connection is still active
if (orignalChannel == connection.getChannel() && connection.isActive()) {
return;
}
canceledByScheduler.set(true);
details.getAttemptPromise().trySuccess(null);
}
}, popTimeout, TimeUnit.SECONDS);
} else {
scheduledFuture = null;
}
details.getMainPromise().addListener(new FutureListener<R>() {
@Override
public void operationComplete(Future<R> future) throws Exception {
if (scheduledFuture != null) {
scheduledFuture.cancel();
}
synchronized (listener) {
connectionManager.getShutdownPromise().removeListener(listener);
}
// handling cancel operation for commands from skipTimeout collection
if ((future.isCancelled() && details.getAttemptPromise().cancel(true)) || canceledByScheduler.get()) {
connection.forceFastReconnectAsync();
return;
}
if (future.cause() instanceof RedissonShutdownException) {
details.getAttemptPromise().tryFailure(future.cause());
}
}
});
synchronized (listener) {
if (!details.getMainPromise().isDone()) {
connectionManager.getShutdownPromise().addListener(listener);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startChunkDown.
protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
// 重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
setChannel(chunkInfo, future.channel());
}
synchronized (requestInfo) {
LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
if (httpDownInfo.getTaskInfo().isSupportRange()) {
requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
} else {
requestInfo.headers().remove(HttpHeaderNames.RANGE);
}
future.channel().writeAndFlush(httpDownInfo.getRequest());
}
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
future.channel().writeAndFlush(content);
}
} else {
LOGGER.debug("下载连接失败:" + chunkInfo);
chunkInfo.setStatus(HttpDownStatus.FAIL);
future.channel().close();
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method close.
public void close(ChunkInfo chunkInfo, boolean isDone) {
try {
if (!isDone) {
chunkInfo.setStatus(HttpDownStatus.WAIT);
}
if (!attr.containsKey(chunkInfo.getIndex())) {
return;
}
Channel channel = getChannel(chunkInfo);
LOGGER.debug("下载连接关闭:channelId[" + (channel != null ? channel.id() : "null") + "]\t" + chunkInfo);
HttpDownUtil.safeClose(channel);
Closeable closeable = (Closeable) getAttr(chunkInfo, ATTR_FILE_CLOSEABLE);
if (closeable != null) {
closeable.close();
}
attr.remove(chunkInfo.getIndex());
} catch (Exception e) {
LOGGER.error("closeChunk error", e);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project proxyee-down by monkeyWie.
the class HttpDownProxyServer method start.
public void start(int port) {
LOGGER.debug("HttpDownProxyServer listen " + port + "\tproxyConfig:" + proxyConfig);
// 监听http下载请求
proxyServer.proxyConfig(proxyConfig);
proxyServer.proxyInterceptInitializer(new HttpProxyInterceptInitializer() {
@Override
public void init(HttpProxyInterceptPipeline pipeline) {
pipeline.addLast(new BdyIntercept());
pipeline.addLast(new HttpDownSniffIntercept());
HttpProxyIntercept downIntercept = interceptFactory.create();
if (downIntercept != null) {
pipeline.addLast(downIntercept);
}
}
}).httpProxyExceptionHandle(new HttpProxyExceptionHandle() {
@Override
public void beforeCatch(Channel clientChannel, Throwable cause) throws Exception {
LOGGER.warn("beforeCatch:", cause);
}
@Override
public void afterCatch(Channel clientChannel, Channel proxyChannel, Throwable cause) throws Exception {
LOGGER.warn("afterCatch:", cause);
}
}).start(port);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project proxyee-down by monkeyWie.
the class HttpDownHandleInterceptFactory method create.
@Override
public HttpProxyIntercept create() {
return new HttpProxyIntercept() {
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
HttpRequest httpRequest = pipeline.getHttpRequest();
ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest, httpResponse.headers(), proxyConfig, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
ContentManager.DOWN.putBoot(httpDownInfo);
httpResponse.setStatus(HttpResponseStatus.OK);
httpResponse.headers().clear();
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
byte[] content;
if (HttpUtil.checkHead(httpRequest, HttpHeaderNames.HOST, "^.*\\.baidupcs\\.com.*$")) {
content = "<script>window.close();</script>".getBytes();
} else {
content = "<script>window.history.go(-1);</script>".getBytes();
}
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
clientChannel.writeAndFlush(httpResponse);
HttpContent httpContent = new DefaultLastHttpContent();
httpContent.content().writeBytes(content);
clientChannel.writeAndFlush(httpContent);
clientChannel.close();
httpDownDispatch.dispatch(httpDownInfo);
}
};
}
Aggregations