Open Kilda Java Documentation
WatchDog.java
Go to the documentation of this file.
1 package org.openkilda.wfm;
2 
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 
6 public class WatchDog {
7  private static final Logger logger = LoggerFactory.getLogger(WatchDog.class);
8 
9  private final long safePeriod;
10  private long lastReset;
11  private boolean available = true;
12 
13  public WatchDog(float safePeriod) {
14  this((long) (safePeriod * 1000));
15  }
16 
17  public WatchDog(long safePeriod) {
18  this.lastReset = System.currentTimeMillis();
19  this.safePeriod = safePeriod;
20  }
21 
22  public void reset() {
23  lastReset = System.currentTimeMillis();
24  logger.debug("Being kicked");
25  }
26 
27  public boolean isTimeout() {
28  return ! this.isAvailable();
29  }
30 
31  public boolean isAvailable() {
32  long current = System.currentTimeMillis();
33  if (current < lastReset) {
34  lastReset = current;
35  }
36 
37  boolean become = (current - lastReset) < safePeriod;
38  if (available != become) {
39  logger.info(String.format("Become %savailable", become ? "" : "un"));
40  }
41  available = become;
42 
43  return available;
44  }
45 }
WatchDog(long safePeriod)
Definition: WatchDog.java:17
WatchDog(float safePeriod)
Definition: WatchDog.java:13