24
24
import java .util .concurrent .LinkedBlockingQueue ;
25
25
import java .util .concurrent .TimeUnit ;
26
26
import java .util .concurrent .TimeoutException ;
27
+ import java .util .concurrent .atomic .AtomicReference ;
27
28
28
29
public class Connection {
29
30
public static final byte [] MAGIC_PROTOCOL_VERSION = " V2" .getBytes ();
@@ -39,6 +40,9 @@ public class Connection {
39
40
private final EventLoopGroup eventLoopGroup ;
40
41
private final NSQConfig config ;
41
42
43
+ public static final long HEARTBEAT_MAX_INTERVAL = 1L *60L *1000L ;//default one minute
44
+ private volatile AtomicReference <Long > lastHeartbeatSuccess = new AtomicReference <Long >(System .currentTimeMillis ());
45
+
42
46
43
47
public Connection (final ServerAddress serverAddress , final NSQConfig config ) throws NoConnectionsException {
44
48
this .address = serverAddress ;
@@ -92,6 +96,13 @@ public boolean isRequestInProgress() {
92
96
return requests .size () > 0 ;
93
97
}
94
98
99
+ public boolean isHeartbeatStatusOK (){
100
+ if (System .currentTimeMillis () - lastHeartbeatSuccess .get () > HEARTBEAT_MAX_INTERVAL ){
101
+ return false ;
102
+ }
103
+ return true ;
104
+ }
105
+
95
106
public void incoming (final NSQFrame frame ) {
96
107
if (frame instanceof ResponseFrame ) {
97
108
if ("_heartbeat_" .equals (((ResponseFrame ) frame ).getMessage ())) {
@@ -137,6 +148,7 @@ public void incoming(final NSQFrame frame) {
137
148
private void heartbeat () {
138
149
LogManager .getLogger (this ).trace ("HEARTBEAT!" );
139
150
command (NSQCommand .instance ("NOP" ));
151
+ lastHeartbeatSuccess .getAndSet (System .currentTimeMillis ());
140
152
}
141
153
142
154
public void setErrorCallback (final NSQErrorCallback callback ) {
0 commit comments