add KeepAlive configuration for ghttp.Server

This commit is contained in:
John 2019-06-02 22:19:52 +08:00
parent 79f765c961
commit 4f1047e853
2 changed files with 17 additions and 4 deletions

View File

@ -46,6 +46,7 @@ type ServerConfig struct {
IdleTimeout time.Duration // 等待超时
MaxHeaderBytes int // 最大的header长度
TLSConfig tls.Config
KeepAlive bool
// 静态文件配置
IndexFiles []string // 默认访问的文件列表
@ -96,6 +97,7 @@ var defaultServerConfig = ServerConfig {
WriteTimeout : 60 * time.Second,
IdleTimeout : 60 * time.Second,
MaxHeaderBytes : 1024,
KeepAlive : true,
IndexFiles : []string{"index.html", "index.htm"},
IndexFolder : false,
@ -316,6 +318,15 @@ func (s *Server) SetRouterCacheExpire(expire int) {
s.config.RouterCacheExpire = expire
}
// 设置KeepAlive
func (s *Server) SetKeepAlive(enabled bool) {
if s.Status() == SERVER_STATUS_RUNNING {
glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR)
return
}
s.config.KeepAlive = enabled
}
// 获取WebServer名称
func (s *Server) GetName() string {
return s.name

View File

@ -21,9 +21,9 @@ import (
// 优雅的Web Server对象封装
type gracefulServer struct {
fd uintptr
addr string
httpServer *http.Server
fd uintptr // 热重启时传递的socket监听文件句柄
addr string // 监听地址信息
httpServer *http.Server // 底层http.Server
rawListener net.Listener // 原始listener
listener net.Listener // 接口化封装的listener
isHttps bool // 是否HTTPS
@ -45,7 +45,7 @@ func (s *Server) newGracefulServer(addr string, fd...int) *gracefulServer {
// 生成一个底层的Web Server对象
func (s *Server) newHttpServer(addr string) *http.Server {
return &http.Server {
server := &http.Server {
Addr : addr,
Handler : s.config.Handler,
ReadTimeout : s.config.ReadTimeout,
@ -53,6 +53,8 @@ func (s *Server) newHttpServer(addr string) *http.Server {
IdleTimeout : s.config.IdleTimeout,
MaxHeaderBytes : s.config.MaxHeaderBytes,
}
server.SetKeepAlivesEnabled(s.config.KeepAlive)
return server
}
// 执行HTTP监听