Commit db84f301 authored by zhouwei's avatar zhouwei

修改统计错误率的时间窗口

parent e89fb371
...@@ -15,6 +15,13 @@ public class AppConfig { ...@@ -15,6 +15,13 @@ public class AppConfig {
@Value("${gateway.request.timeout:30000}") @Value("${gateway.request.timeout:30000}")
private Integer requestTimeout; private Integer requestTimeout;
@Value("${gateway.window.time:600000}")
private Integer windowsTime;
public Integer getWindowsTime() {
return windowsTime;
}
public Integer getRequestTimeout() { public Integer getRequestTimeout() {
return requestTimeout; return requestTimeout;
} }
......
...@@ -61,16 +61,18 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -61,16 +61,18 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
// 内部类:环形队列,用于记录请求结果 // 内部类:环形队列,用于记录请求结果
private static class CircularQueue { private static class CircularQueue {
AppConfig appConfig;
private final long[] timestamps; private final long[] timestamps;
private final boolean[] results; // true表示成功,false表示失败 private final boolean[] results; // true表示成功,false表示失败
private int currentIndex = 0; private int currentIndex = 0;
private final int capacity; private final int capacity;
private final Object lock = new Object(); private final Object lock = new Object();
public CircularQueue(int capacity) { public CircularQueue(int capacity, AppConfig appConfig) {
this.capacity = capacity; this.capacity = capacity;
this.timestamps = new long[capacity]; this.timestamps = new long[capacity];
this.results = new boolean[capacity]; this.results = new boolean[capacity];
this.appConfig = appConfig;
} }
public void add(boolean success) { public void add(boolean success) {
...@@ -84,7 +86,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -84,7 +86,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
public double getErrorRate() { public double getErrorRate() {
synchronized (lock) { synchronized (lock) {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long oneMinuteAgo = currentTime - WINDOW_SIZE_SECONDS * 1000; long oneMinuteAgo = currentTime - appConfig.getWindowsTime();
int totalRequests = 0; int totalRequests = 0;
int failedRequests = 0; int failedRequests = 0;
...@@ -104,7 +106,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -104,7 +106,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
private CircularQueue getOrCreateErrorStats(String primaryUrl) { private CircularQueue getOrCreateErrorStats(String primaryUrl) {
return errorStatsMap.computeIfAbsent(primaryUrl, return errorStatsMap.computeIfAbsent(primaryUrl,
k -> new CircularQueue(WINDOW_SIZE_SECONDS * 10)); // 存储10次/秒的采样 k -> new CircularQueue(WINDOW_SIZE_SECONDS * 10, appConfig)); // 存储10次/秒的采样
} }
private boolean shouldSkipPrimary(String primaryUrl) { private boolean shouldSkipPrimary(String primaryUrl) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment