Skip to content

Commit

Permalink
Merge pull request #184 from jamebal/develop
Browse files Browse the repository at this point in the history
test: 添加索引进度日志
  • Loading branch information
jamebal authored Nov 28, 2024
2 parents 9723fb3 + 2fe1459 commit d71721a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
33 changes: 23 additions & 10 deletions src/main/java/com/jmal/clouddisk/controller/sse/SseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -75,16 +75,14 @@ private void sendMessage(Object message, String username, String uuid) {
if (emitter != null) {
try {
emitter.send(message);
} catch (IllegalStateException e) {
if (e.getMessage().contains("completed")) {
removeUuid(username, uuid);
}
} catch (IOException e) {
} catch (Exception e) {
log.warn("Failed to send message to client {}: {}", uuid, e.getMessage());
removeUuid(username, uuid);
}
}
}


private void removeUuid(String username, String uuid) {
emitters.remove(uuid);
Set<String> uuids = users.get(username);
Expand All @@ -101,12 +99,27 @@ private void removeUuid(String username, String uuid) {
*/
@Scheduled(fixedRate = 3000)
public void heartbeat() {
emitters.forEach((uuid, emitter) -> {
// 使用迭代器安全删除
Iterator<Map.Entry<String, SseEmitter>> iterator = emitters.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, SseEmitter> entry = iterator.next();
try {
emitter.send("h");
} catch (IOException | IllegalStateException ignored) {
entry.getValue().send("h");
} catch (Exception e) {
// 发生异常时移除该连接
iterator.remove();
// 清理关联的用户数据
for (Map.Entry<String, Set<String>> userEntry : users.entrySet()) {
if (userEntry.getValue().remove(entry.getKey())) {
if (userEntry.getValue().isEmpty()) {
users.remove(userEntry.getKey());
}
break;
}
}
}
});
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void init() {
}

private void getSyncFileVisitorService() {
int processors = Runtime.getRuntime().availableProcessors() - 1;
int processors = Runtime.getRuntime().availableProcessors() - 4;
if (processors < 1) {
processors = 1;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
if (StrUtil.isBlank(username)) {
return super.visitFile(dir, attrs);
}
//processFile(dir, username);
processFile(dir, username);
return super.preVisitDirectory(dir, attrs);
}

Expand All @@ -429,7 +429,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
if (StrUtil.isBlank(username)) {
return super.visitFile(file, attrs);
}
//processFile(file, username);
processFile(file, username);
return super.visitFile(file, attrs);
}

Expand Down

0 comments on commit d71721a

Please sign in to comment.