Skip to content

Commit ff29c1b

Browse files
authored
Merge pull request #50 from lexiLiu/1.0.1.0
1.0.1.0
2 parents 96091b6 + 6fbda03 commit ff29c1b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.getui.push.v2.sdk.common;
2+
3+
import com.getui.push.v2.sdk.common.util.Utils;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
12+
/**
13+
* create by getui on 2024/4/8
14+
*
15+
* @author getui
16+
*/
17+
public class Monitor {
18+
private static Logger log = LoggerFactory.getLogger(Monitor.class);
19+
/**
20+
* 单位时间内失败总数
21+
*/
22+
static volatile Map<String, AtomicInteger> hostToFailedNumMap;
23+
static volatile boolean MONITOR_ENABLE = false;
24+
25+
public static void init(long refreshTimes) {
26+
hostToFailedNumMap = new ConcurrentHashMap<>(16);
27+
MONITOR_ENABLE = true;
28+
Thread thread = new Thread(() -> {
29+
while (true) {
30+
try {
31+
Thread.sleep(refreshTimes);
32+
log.debug("will reset monitor|{}", hostToFailedNumMap);
33+
Iterator<Map.Entry<String, AtomicInteger>> iterator = hostToFailedNumMap.entrySet().iterator();
34+
while (iterator.hasNext()) {
35+
iterator.next().getValue().set(0);
36+
}
37+
} catch (Throwable e) {
38+
}
39+
}
40+
});
41+
thread.setDaemon(true);
42+
thread.setName("gtResetMonitor");
43+
thread.start();
44+
}
45+
46+
public static int get(String host) {
47+
if (!MONITOR_ENABLE || host == null) {
48+
return 0;
49+
}
50+
AtomicInteger num = hostToFailedNumMap.computeIfAbsent(Utils.v2UrlToHost(host), (k) -> new AtomicInteger());
51+
return num.get();
52+
}
53+
54+
public static void reset(String host) {
55+
if (!MONITOR_ENABLE || host == null) {
56+
return;
57+
}
58+
AtomicInteger num = hostToFailedNumMap.computeIfAbsent(Utils.v2UrlToHost(host), (k) -> new AtomicInteger());
59+
num.set(0);
60+
}
61+
62+
public static void incrementFailedNum(String host) {
63+
if (!MONITOR_ENABLE || host == null) {
64+
return;
65+
}
66+
AtomicInteger num = hostToFailedNumMap.computeIfAbsent(Utils.v2UrlToHost(host), (k) -> new AtomicInteger());
67+
num.incrementAndGet();
68+
}
69+
70+
}

0 commit comments

Comments
 (0)