-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeartbeatMonitor.java
More file actions
57 lines (49 loc) · 1.71 KB
/
HeartbeatMonitor.java
File metadata and controls
57 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Heartbeat monitor, a part of code comes from classmate kxy.
*
*/
import java.net.Socket;
import java.util.List;
class HeartbeatMonitor extends Thread {
Socket connSocket;
private List<Socket> pool;
public HeartbeatMonitor() {
}
public void Mnt(Socket connSocket) {
if (connSocket != null)
pool.add(connSocket);
}
public void run() {
int T = 1000;
double Threshold = 0.25;// limit 1ms less than 0.25 request
while (true) {
int reqCnt = BasicWebServer.reqCnt;
long TimeSrcFlag = System.currentTimeMillis();
try {
while (System.currentTimeMillis() - TimeSrcFlag < T) {
}
if ((BasicWebServer.reqCnt - reqCnt) * 1.0 / T > Threshold) {
if (!pool.isEmpty()) {
connSocket = pool.remove(0);
WebRequestHandler wrh =
new WebRequestHandler( connSocket, BasicWebServer.cfgMap,
BasicWebServer.fileCache, BasicWebServer.cacheSize);
wrh.outputError(503, "overloading");
connSocket.close();// close the thread of the first
System.out.println("Thread : " + connSocket + "has been closed!");
}
}
sleep(2000);
} catch (Exception e) {
}
}
}
}
/* in main
* reqCnt++;
// process a request
WebRequestHandler wrh =
new WebRequestHandler( connectionSocket, cfgMap, fileCache, cacheSize);
cm.Mnt(connectionSocket);
wrh.processRequest();
*/