Skip to content

Commit b9d5c86

Browse files
committed
update
1 parent 3321c8a commit b9d5c86

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/main/java/cn/pan/connor/core/handle/resp/HeartbeatTimeoutRespHandle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.pan.connor.common.utils.JsonUtil;
44
import cn.pan.connor.core.model.response.HeartbeatTimeoutResponse;
5+
import cn.pan.connor.core.transport.ClientCache;
56
import io.netty.channel.ChannelHandlerContext;
67
import io.netty.channel.SimpleChannelInboundHandler;
78
import lombok.extern.slf4j.Slf4j;
@@ -20,6 +21,6 @@ public class HeartbeatTimeoutRespHandle extends SimpleChannelInboundHandler<Hear
2021
@Override
2122
protected void channelRead0(ChannelHandlerContext context, HeartbeatTimeoutResponse response) {
2223
log.info("accept connor service heartbeatTimeoutRespHandle : {}", JsonUtil.toStr(response));
23-
24+
ClientCache.ServiceCache.timeoutUpdate(response.getTimeoutServiceIds());
2425
}
2526
}

src/main/java/cn/pan/connor/core/model/response/HeartbeatTimeoutResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616
@Data
1717
public class HeartbeatTimeoutResponse {
18-
@SerializedName("service_ids")
19-
private List<String> serviceIds;
18+
@SerializedName("timeout_service_ids")
19+
private List<String> timeoutServiceIds;
2020
}

src/main/java/cn/pan/connor/core/transport/ClientCache.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.ConcurrentSkipListMap;
1313
import java.util.concurrent.SynchronousQueue;
1414
import java.util.concurrent.TimeUnit;
15+
import java.util.stream.Collectors;
1516

1617
/**
1718
* 客户端缓存
@@ -29,6 +30,19 @@ public static class ServiceCache {
2930
private static final SynchronousQueue<String> SERVICES_BLOCK = new SynchronousQueue<>();
3031
private static final ConcurrentSkipListMap<String, List<NewService>> SERVICES = new ConcurrentSkipListMap<>();
3132

33+
/**
34+
* 根据server的推送,移除超时的实例
35+
* @param timeoutServiceIds 超时的实例id
36+
*/
37+
public static void timeoutUpdate(List<String> timeoutServiceIds) {
38+
SERVICES.forEach((k,v) -> {
39+
List<NewService> services = v.stream()
40+
.filter(service -> !timeoutServiceIds.contains(service.getId()))
41+
.collect(Collectors.toList());
42+
SERVICES.put(k,services);
43+
});
44+
}
45+
3246
/**
3347
* 根据server的推送更新本地缓存
3448
*/

src/test/java/cn/pan/connor/ConnorStarterApplicationTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package cn.pan.connor;
22

33
import cn.hutool.core.date.DateUtil;
4+
import cn.pan.connor.core.model.NewService;
45
import lombok.extern.slf4j.Slf4j;
56
import org.junit.jupiter.api.Test;
67

8+
import java.util.List;
79
import java.util.Objects;
10+
import java.util.concurrent.ConcurrentSkipListMap;
811
import java.util.concurrent.ScheduledThreadPoolExecutor;
912
import java.util.concurrent.SynchronousQueue;
1013
import java.util.concurrent.TimeUnit;
14+
import java.util.stream.Collectors;
1115

1216
@Slf4j
1317
//@SpringBootTest
@@ -43,10 +47,40 @@ void test_01() {
4347
e.printStackTrace();
4448
}
4549
}
50+
public static ConcurrentSkipListMap<String, List<NewService>> SERVICES = new ConcurrentSkipListMap<>();
4651

52+
public static void timeoutUpdate(List<String> serviceIds) {
53+
SERVICES.forEach((k,v) -> {
54+
List<NewService> services = v.stream()
55+
.filter(service -> !serviceIds.contains(service.getId()))
56+
.collect(Collectors.toList());
57+
SERVICES.put(k,services);
58+
});
59+
}
4760

61+
@Test
62+
void test_02() {
63+
// 为SERVICES初始化数据
64+
SERVICES.put("1",
65+
List.of(
66+
NewService.builder().id("1").build(),
67+
NewService.builder().id("2").build(),
68+
NewService.builder().id("3").build()));
69+
SERVICES.put("2",
70+
List.of(
71+
NewService.builder().id("1").build(),
72+
NewService.builder().id("2").build(),
73+
NewService.builder().id("3").build()));
74+
// 创建一个ID list
75+
List<String> serviceIds = List.of("2");
76+
// 调用timeoutUpdate方法
77+
timeoutUpdate(serviceIds);
78+
// 打印结果
79+
SERVICES.forEach((k,v) -> {
80+
System.out.println(k + ":" + v);
81+
});
4882

49-
83+
}
5084

5185

5286
}

0 commit comments

Comments
 (0)