Skip to content

Commit

Permalink
Merge pull request #8 from alauda/fix/AIT-42488
Browse files Browse the repository at this point in the history
set conn keepalive true
  • Loading branch information
wgkingk authored Dec 19, 2023
2 parents 0c4c7e8 + 14b6a29 commit ce512d9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ func (s *Server) Start() error {
addr := fmt.Sprintf("%s:%d", s.ServerConfig.BindAddress, s.ServerConfig.Port)
handler := s.Container()

connState := func(conn net.Conn, state http.ConnState) {
if state == http.StateNew {
if tcpConn, ok := conn.(*net.TCPConn); ok {
err := tcpConn.SetKeepAlive(true)
if err != nil {
logger.Error(fmt.Sprintf("failed to set keepalive, err: %v", err), zap.String("func", "connState"))
return
}

err = tcpConn.SetKeepAlivePeriod(time.Second * 30)
if err != nil {
logger.Error(fmt.Sprintf("failed to set keepalive period, err: %v", err), zap.String("func", "connState"))
return
}
}
}
}

if s.ServerConfig.TLSCertFile != "" && s.ServerConfig.TLSKeyFile != "" {

cert, err := tls.LoadX509KeyPair(s.ServerConfig.TLSCertFile, s.ServerConfig.TLSKeyFile)
Expand All @@ -200,7 +218,9 @@ func (s *Server) Start() error {
return err
}

return http.Serve(listener, handler)
srv := http.Server{Handler: handler, ConnState: connState}
return srv.Serve(listener)
}
return http.ListenAndServe(addr, handler)
srv := http.Server{Addr: addr, Handler: handler, ConnState: connState}
return srv.ListenAndServe()
}

0 comments on commit ce512d9

Please sign in to comment.