diff --git a/g/net/ghttp/ghttp_server_config.go b/g/net/ghttp/ghttp_server_config.go index 6f7fbad39..e6ae04ed0 100644 --- a/g/net/ghttp/ghttp_server_config.go +++ b/g/net/ghttp/ghttp_server_config.go @@ -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 diff --git a/g/net/ghttp/ghttp_server_graceful.go b/g/net/ghttp/ghttp_server_graceful.go index 5b8fa01ba..7ea854cd3 100644 --- a/g/net/ghttp/ghttp_server_graceful.go +++ b/g/net/ghttp/ghttp_server_graceful.go @@ -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监听