Skip to content

Commit

Permalink
加入KeepAlive设置
Browse files Browse the repository at this point in the history
  • Loading branch information
ying32 committed Jan 16, 2017
1 parent 6300abd commit 553217e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"net/http"
"time"
)

type TRPClient struct {
Expand All @@ -26,6 +27,8 @@ func (c *TRPClient) Start() error {
if err != nil {
return err
}
conn.(*net.TCPConn).SetKeepAlive(true)
conn.(*net.TCPConn).SetKeepAlivePeriod(time.Duration(2 * time.Second))
c.conn = conn
return c.process()
}
Expand Down
7 changes: 6 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (s *TRPServer) Start() error {
}

func (s *TRPServer) Close() error {
if s.conn != nil {
s.conn.Close()
s.conn = nil
}
if s.listener != nil {
err := s.listener.Close()
s.listener = nil
Expand Down Expand Up @@ -109,7 +113,8 @@ func (s *TRPServer) cliProcess(conn *net.TCPConn) error {
s.conn = nil
}
log.Println("连接新的客户端:", conn.RemoteAddr())

conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
s.conn = conn
return nil
}
Expand Down

0 comments on commit 553217e

Please sign in to comment.