Skip to content

Commit 553217e

Browse files
author
ying32
committed
加入KeepAlive设置
1 parent 6300abd commit 553217e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net"
88
"net/http"
9+
"time"
910
)
1011

1112
type TRPClient struct {
@@ -26,6 +27,8 @@ func (c *TRPClient) Start() error {
2627
if err != nil {
2728
return err
2829
}
30+
conn.(*net.TCPConn).SetKeepAlive(true)
31+
conn.(*net.TCPConn).SetKeepAlivePeriod(time.Duration(2 * time.Second))
2932
c.conn = conn
3033
return c.process()
3134
}

server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (s *TRPServer) Start() error {
3737
}
3838

3939
func (s *TRPServer) Close() error {
40+
if s.conn != nil {
41+
s.conn.Close()
42+
s.conn = nil
43+
}
4044
if s.listener != nil {
4145
err := s.listener.Close()
4246
s.listener = nil
@@ -109,7 +113,8 @@ func (s *TRPServer) cliProcess(conn *net.TCPConn) error {
109113
s.conn = nil
110114
}
111115
log.Println("连接新的客户端:", conn.RemoteAddr())
112-
116+
conn.SetKeepAlive(true)
117+
conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
113118
s.conn = conn
114119
return nil
115120
}

0 commit comments

Comments
 (0)