Skip to content

Commit fb3473e

Browse files
committed
use timestamp to heartbeat
1 parent 7eb7db8 commit fb3473e

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

โ€ŽCHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Broken API: change ReadTimeout/WriteTimeout to IdleTimeout
1818
- Support passing Deadline of client contexts to server side
1919
- remove InprocessClient plugin
20+
- use heartbeat/tcp_keepalive to avoid client hanging
2021

2122

2223
## 5.0

โ€Žclient/client.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"crypto/tls"
88
"errors"
9+
"fmt"
910
"io"
1011
"net"
1112
"net/url"
@@ -742,15 +743,15 @@ func (client *Client) heartbeat() {
742743
if client.option.MaxWaitForHeartbeat == 0 {
743744
client.option.MaxWaitForHeartbeat = 30 * time.Second
744745
}
745-
var request = "ping"
746-
var reply string
746+
747747
for range t.C {
748748
if client.IsShutdown() || client.IsClosing() {
749749
t.Stop()
750750
return
751751
}
752752

753-
reply = ""
753+
request := fmt.Sprintf("%d", time.Now().UnixNano())
754+
reply := ""
754755
ctx, cancel := context.WithTimeout(context.Background(), client.option.MaxWaitForHeartbeat)
755756
err := client.Call(ctx, "", "", &request, &reply)
756757
abnormal := false
@@ -764,6 +765,8 @@ func (client *Client) heartbeat() {
764765
abnormal = true
765766
}
766767

768+
// check request == reply
769+
767770
if abnormal {
768771
client.Close()
769772
}

โ€Žserver/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ func (s *Server) serveConn(conn net.Conn) {
425425
s.Plugins.DoHeartbeatRequest(ctx, req)
426426
req.SetMessageType(protocol.Response)
427427
data := req.EncodeSlicePointer()
428+
conn.Write(*data)
428429
protocol.PutData(data)
429-
conn.Write([]byte("pong"))
430430
return
431431
}
432432

0 commit comments

Comments
ย (0)