Skip to content

Commit

Permalink
调整超时重试机制
Browse files Browse the repository at this point in the history
分段数最大限制修改为128
任务列表页面的相关tips
  • Loading branch information
monkeyWie committed Mar 29, 2018
1 parent 0ce34ec commit 2b9dce9
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 72 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
### 刷新任务下载链接
当任务下载链接失效了,下载没速度或失败则可以使用刷新下载链接的功能,使用新的链接继续下载,具体请[查看](https://github.com/monkeyWie/proxyee-down/blob/master/.guide/common/refresh/read.md)
### 百度云破解
百度云大文件、合并下载限制突破,成功安装下载器后,打开百度云页面会有如下提示
- 2018-03-29更新
百度云近期对批量下载的并发连接数做了限制,分段数需要建议不要超过64,否则可能会导致任务下载失败或下载速度慢。
- 百度云大文件、合并下载限制突破,成功安装下载器后,打开百度云页面会有如下提示,然后选择点击下载按钮即可调用proxyee-down下载
![百度云破解](https://github.com/monkeyWie/proxyee-down/raw/master/.guide/common/bdy-hook.png)
### 百度云解压工具
在下载器工具页面里进入百度云解压工具,选择百度云批量下载的文件进行解压
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public class HttpDownStatus {
*/
public final static int DONE = 7;
/**
* 合并文件
* 响应错误等待连接
*/
public final static int MERGE = 8;
/**
* 取消合并文件
*/
public final static int MERGE_CANCEL = 9;
public final static int ERROR_WAIT_CONNECT = 8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ private int getIndex() {
@Override
public void close() throws IOException {
try {
fileChannel.close();
Class<?> clazz = Class.forName("sun.nio.ch.FileChannelImpl");
Method m = clazz.getDeclaredMethod("unmap", MappedByteBuffer.class);
m.setAccessible(true);
for (MappedByteBuffer mappedBuffer : bufferList) {
m.invoke(clazz, mappedBuffer);
}
fileChannel.close();
} catch (Exception e) {
throw new IOException("LargeMappedByteBuffer close", e);
}
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/lee/study/down/model/ChunkInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public class ChunkInfo implements Serializable {
private long pauseTime = 0;
private int status = 0;
private int errorCount = 0;
private long lastDownTime = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exce
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
//重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
Expand Down Expand Up @@ -237,18 +239,19 @@ public void continueDown()

public void close(ChunkInfo chunkInfo) {
try {
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();
}
Channel channel = getChannel(chunkInfo);
LOGGER.debug(
"下载连接关闭:channelId[" + (channel != null ? channel.id() : "null") + "]\t" + chunkInfo);
attr.remove(chunkInfo.getIndex());
HttpDownUtil.safeClose(channel);
} catch (Exception e) {
LOGGER.error("closeChunk error", e);
}
Expand All @@ -257,6 +260,7 @@ public void close(ChunkInfo chunkInfo) {
public void close() {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (taskInfo) {
taskInfo.setStatus(HttpDownStatus.WAIT);
for (ChunkInfo chunkInfo : httpDownInfo.getTaskInfo().getChunkInfoList()) {
synchronized (chunkInfo) {
close(chunkInfo);
Expand All @@ -266,11 +270,11 @@ public void close() {
}

public void delete(boolean delFile) throws Exception {
close();
timeoutCheckTask.delBoot(httpDownInfo.getTaskInfo().getId());
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
//删除任务进度记录文件
synchronized (taskInfo) {
close();
timeoutCheckTask.delBoot(httpDownInfo.getTaskInfo().getId());
FileUtil.deleteIfExists(taskInfo.buildTaskRecordFilePath());
FileUtil.deleteIfExists(taskInfo.buildTaskRecordBakFilePath());
if (delFile) {
Expand Down
20 changes: 9 additions & 11 deletions core/src/main/java/lee/study/down/boot/TimeoutCheckTask.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lee.study.down.boot;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -14,7 +13,7 @@ public class TimeoutCheckTask extends Thread {

private static final Logger LOGGER = LoggerFactory.getLogger(TimeoutCheckTask.class);

private static int seconds = 30;
private static volatile int seconds = 30;
private static Map<String, AbstractHttpDownBootstrap> bootstrapContent = new ConcurrentHashMap<>();

public synchronized static void setTimeout(int seconds) {
Expand All @@ -31,7 +30,6 @@ public void delBoot(String id) {

@Override
public void run() {
Map<String, Long> flagMap = new HashMap<>();
while (true) {
try {
for (AbstractHttpDownBootstrap bootstrap : bootstrapContent.values()) {
Expand All @@ -41,23 +39,23 @@ public void run() {
//30秒没有反应则重新建立连接下载
if (taskInfo.getStatus() == HttpDownStatus.RUNNING
&& chunkInfo.getStatus() != HttpDownStatus.DONE
&& chunkInfo.getStatus() != HttpDownStatus.WAIT
&& chunkInfo.getStatus() != HttpDownStatus.PAUSE) {
String key = taskInfo.getId() + "_" + chunkInfo.getIndex();
Long downSize = flagMap.get(key);
//下载失败
if (downSize != null && downSize == chunkInfo.getDownSize()) {
long nowTime = System.currentTimeMillis();
if (nowTime - chunkInfo.getLastDownTime() > seconds * 1000) {
LOGGER.debug(seconds + "秒内无响应重试:" + chunkInfo);
//避免同时下载
if (chunkInfo.getStatus() == HttpDownStatus.ERROR_WAIT_CONNECT) {
chunkInfo.setErrorCount(chunkInfo.getErrorCount() + 1);
}
//重试下载
bootstrap.retryChunkDown(chunkInfo);
} else {
flagMap.put(key, chunkInfo.getDownSize());
}
}
}
}

}
TimeUnit.SECONDS.sleep(TimeoutCheckTask.seconds);
TimeUnit.MILLISECONDS.sleep(1000);
} catch (Exception e) {
LOGGER.error("checkTask:" + e);
}
Expand Down
28 changes: 9 additions & 19 deletions core/src/main/java/lee/study/down/handle/HttpDownInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
public class HttpDownInitializer extends ChannelInitializer {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpDownInitializer.class);
private static Executor executor = Executors.newFixedThreadPool(4);

private boolean isSsl;
private AbstractHttpDownBootstrap bootstrap;
Expand Down Expand Up @@ -75,6 +74,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
&& nowChannel == ctx.channel()) {
int readableBytes = bootstrap.doFileWriter(chunkInfo, byteBuf.nioBuffer());
if (readableBytes > 0) {
//最后一次下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
//文件已下载大小
chunkInfo.setDownSize(chunkInfo.getDownSize() + readableBytes);
taskInfo.setDownSize(taskInfo.getDownSize() + readableBytes);
Expand Down Expand Up @@ -120,26 +121,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
} else {
HttpResponse httpResponse = (HttpResponse) msg;
if ((httpResponse.status().code() + "").indexOf("20") != 0) {
Integer responseCode = httpResponse.status().code();
if (responseCode.toString().indexOf("20") != 0) {
//应对百度近期同一时段多个连接返回400的问题
if (HttpUtil
.checkUrl(bootstrap.getHttpDownInfo().getRequest(), "^.*.baidupcs.com.*$")
&& httpResponse.status().code() == 400) {
executor.execute(() -> {
safeClose(ctx.channel());
chunkInfo.setStatus(HttpDownStatus.CONNECTING_NORMAL);
try {
Thread.sleep(2000);
bootstrap.retryChunkDown(chunkInfo);
} catch (Exception e) {
e.printStackTrace();
}
});
return;
} else {
chunkInfo.setErrorCount(chunkInfo.getErrorCount() + 1);
LOGGER.debug(
"响应状态码异常:" + responseCode + "\t" + chunkInfo);
if (responseCode == 401 || responseCode == 403 || responseCode == 404) {
chunkInfo.setStatus(HttpDownStatus.ERROR_WAIT_CONNECT);
}
throw new RuntimeException("http down response error:" + httpResponse);
return;
}
realContentSize = HttpDownUtil.getDownContentSize(httpResponse.headers());
synchronized (chunkInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class HttpDownConstant {

public final static String HOME_PATH = PathUtil.ROOT_PATH;
public final static String TASK_RECORD_PATH = HOME_PATH + ".records.inf";
public final static String CONFIG_PATH = HOME_PATH + ".config.inf";
public final static String CONFIG_PATH = HOME_PATH + "config.inf";

public final static String CA_SUBJECT = "ProxyeeRoot";
public final static String SSL_PATH = HOME_PATH + "ssl" + File.separator;
Expand Down
5 changes: 1 addition & 4 deletions ui/src/main/java/lee/study/down/content/DownContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ public void init() {
} else {
taskInfo.reset();
}
if (taskInfo.getStatus() == HttpDownStatus.MERGE) {
//设置为合并取消状态
taskInfo.setStatus(HttpDownStatus.MERGE_CANCEL);
} else if (taskInfo.getStatus() != HttpDownStatus.FAIL) {
if (taskInfo.getStatus() != HttpDownStatus.FAIL) {
//设置为暂停状态
taskInfo.setStatus(HttpDownStatus.PAUSE);
taskInfo.getChunkInfoList().forEach((chunk) -> {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/main/java/lee/study/down/model/ConfigBaseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ConfigBaseInfo {
private int sniffModel = 2; //嗅探模式 1.全网 2.百度云 3.关闭
private int uiModel = 1; //嗅探模式 1.GUI 2.浏览器
private int connections = 32; //默认分段数
private int timeout = 30; //超时重试时间
private int timeout = 10; //超时重试时间
private boolean secProxyEnable; //二级代理开关
private String lastPath; //最后保存文件的路径
private int retryCount = 5; //失败重试次数
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lee.study.down.mvc.controller;

import com.sun.org.apache.bcel.internal.generic.NEW;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -77,8 +78,6 @@ public ResultInfo getTask(@RequestParam String id) throws Exception {
List<HttpDownInfo> sameTasks = ContentManager.DOWN.getDownInfos().stream()
.filter(downInfo -> HttpDownStatus.WAIT != downInfo.getTaskInfo().getStatus()
&& HttpDownStatus.DONE != downInfo.getTaskInfo().getStatus()
&& HttpDownStatus.MERGE != downInfo.getTaskInfo().getStatus()
&& HttpDownStatus.MERGE_CANCEL != downInfo.getTaskInfo().getStatus()
&& downInfo.getTaskInfo().getTotalSize() == taskInfo.getTotalSize()
).collect(Collectors.toList());
data.put("sameTasks", NewTaskForm.parse(sameTasks));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public void run() {
for (TaskInfo taskInfo : ContentManager.DOWN.getStartTasks()) {
if (taskInfo.getStatus() != HttpDownStatus.DONE
&& taskInfo.getStatus() != HttpDownStatus.FAIL
&& taskInfo.getStatus() != HttpDownStatus.PAUSE
&& taskInfo.getStatus() != HttpDownStatus.MERGE
&& taskInfo.getStatus() != HttpDownStatus.MERGE_CANCEL) {
&& taskInfo.getStatus() != HttpDownStatus.PAUSE) {
taskInfo.setLastTime(System.currentTimeMillis());
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
if (chunkInfo.getStatus() != HttpDownStatus.DONE
Expand Down
6 changes: 3 additions & 3 deletions ui/view/src/components/ConfigPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<el-slider
v-model="form.connections"
:min="2"
:max="256"
:max="128"
:step="2"
show-input>
</el-slider>
Expand Down Expand Up @@ -96,11 +96,11 @@
],
timeout: [
{required: true, message: '不能为空'},
{type: 'integer', min: 10, message: '请输入大于或等于10的数字'},
{type: 'integer', min: 5, message: '请输入大于或等于5的数字'},
],
retryCount: [
{required: true, message: '不能为空'},
{type: 'integer', min: 5, max: 30, message: '请输入5-30之间的数字'},
{type: 'integer', min: 5, max: 100, message: '请输入5-100之间的数字'},
]
},
proxyTypeOptions: [{
Expand Down
2 changes: 1 addition & 1 deletion ui/view/src/components/NewTask.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<el-slider
v-model="form.connections"
:min="2"
:max="256"
:max="128"
:step="2"
:disabled="!form.supportRange||!!form.oldId"
show-input>
Expand Down
28 changes: 15 additions & 13 deletions ui/view/src/components/TaskList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@
<b>{{leftTime(task)}}</b>
<el-tooltip v-show="task.status==6" class="item"
placement="right">
<div slot="content">下载链接失效,可尝试<a
href="https://github.com/monkeyWie/proxyee-down/blob/master/.guide/common/refresh/read.md"
target="_blank" style="color: #3a8ee6">刷新下载链接</a>
<div slot="content">下载链接失效,可尝试
<native-a
href="https://github.com/monkeyWie/proxyee-down/blob/master/.guide/common/refresh/read.md"
target="_blank" style="color: #3a8ee6">刷新下载链接
</native-a>
</div>
<i class="el-icon-question"></i>
</el-tooltip>
Expand Down Expand Up @@ -113,6 +115,7 @@

<script>
import Util from '../common/util'
import NativeA from './base/NativeA'
import BuildTask from './BuildTask'
import NewTask from './NewTask'
import TaskProgress from './base/TaskProgress'
Expand All @@ -122,7 +125,8 @@
components: {
BuildTask,
NewTask,
TaskProgress
TaskProgress,
NativeA
},
computed: {
newTaskTitle() {
Expand Down Expand Up @@ -219,12 +223,6 @@
if (task.status == 7) {
return '已完成';
}
if (task.status == 8) {
return '合并中';
}
if (task.status == 9) {
return '待合并';
}
if (task.status == 5) {
return '暂停中';
}
Expand All @@ -239,15 +237,13 @@
switch (task.status) {
case 7:
return 'success';
case 8:
return 'merge';
case 2:
case 6:
return 'exception';
case 5:
case 9:
return 'pause';
case 1:
case 8:
return 'ready';
default:
return null;
Expand Down Expand Up @@ -333,6 +329,12 @@
this.buildTaskHandle(result);
}).catch(() => {
});
this.$notify.info({
title: 'Tips',
position: 'bottom-right',
duration: 0,
message: '点击进度圈可以查看下载速度'
});
}
}
</script>
Expand Down

0 comments on commit 2b9dce9

Please sign in to comment.