2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-05-06 22:35:08 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-06 22:35:08 +08:00
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-26 14:56:05 +08:00
|
|
|
"github.com/gogf/gf/os/gproc"
|
2019-10-11 22:54:25 +08:00
|
|
|
"log"
|
2019-06-19 09:06:52 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-05-15 18:34:00 +08:00
|
|
|
)
|
|
|
|
|
2018-05-06 22:35:08 +08:00
|
|
|
// 优雅的Web Server对象封装
|
|
|
|
type gracefulServer struct {
|
2020-01-21 15:42:08 +08:00
|
|
|
server *Server // Belonged server.
|
2019-06-19 09:06:52 +08:00
|
|
|
fd uintptr // 热重启时传递的socket监听文件句柄
|
2019-08-03 15:54:12 +08:00
|
|
|
itemFunc string // 监听地址信息
|
2019-06-19 09:06:52 +08:00
|
|
|
httpServer *http.Server // 底层http.Server
|
|
|
|
rawListener net.Listener // 原始listener
|
|
|
|
listener net.Listener // 接口化封装的listener
|
|
|
|
isHttps bool // 是否HTTPS
|
|
|
|
status int // 当前Server状态(关闭/运行)
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建一个优雅的Http Server
|
2019-08-03 15:54:12 +08:00
|
|
|
func (s *Server) newGracefulServer(itemFunc string, fd ...int) *gracefulServer {
|
2019-06-19 09:06:52 +08:00
|
|
|
gs := &gracefulServer{
|
2020-01-21 15:42:08 +08:00
|
|
|
server: s,
|
2019-08-03 15:54:12 +08:00
|
|
|
itemFunc: itemFunc,
|
|
|
|
httpServer: s.newHttpServer(itemFunc),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
// 是否有继承的文件描述符
|
|
|
|
if len(fd) > 0 && fd[0] > 0 {
|
|
|
|
gs.fd = uintptr(fd[0])
|
|
|
|
}
|
|
|
|
return gs
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:38:09 +08:00
|
|
|
// 生成一个底层的Web Server对象
|
2019-08-03 15:54:12 +08:00
|
|
|
func (s *Server) newHttpServer(itemFunc string) *http.Server {
|
2019-06-19 09:06:52 +08:00
|
|
|
server := &http.Server{
|
2019-08-03 15:54:12 +08:00
|
|
|
Addr: itemFunc,
|
2019-06-19 09:06:52 +08:00
|
|
|
Handler: s.config.Handler,
|
|
|
|
ReadTimeout: s.config.ReadTimeout,
|
|
|
|
WriteTimeout: s.config.WriteTimeout,
|
|
|
|
IdleTimeout: s.config.IdleTimeout,
|
|
|
|
MaxHeaderBytes: s.config.MaxHeaderBytes,
|
2019-11-08 19:52:49 +08:00
|
|
|
ErrorLog: log.New(&errorLogger{logger: s.config.Logger}, "", 0),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
server.SetKeepAlivesEnabled(s.config.KeepAlive)
|
|
|
|
return server
|
2018-05-08 23:38:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:35:08 +08:00
|
|
|
// 执行HTTP监听
|
|
|
|
func (s *gracefulServer) ListenAndServe() error {
|
2019-08-03 15:54:12 +08:00
|
|
|
itemFunc := s.httpServer.Addr
|
|
|
|
ln, err := s.getNetListener(itemFunc)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.listener = ln
|
|
|
|
s.rawListener = ln
|
|
|
|
return s.doServe()
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:00:10 +08:00
|
|
|
// 获得文件描述符
|
|
|
|
func (s *gracefulServer) Fd() uintptr {
|
2019-06-19 09:06:52 +08:00
|
|
|
if s.rawListener != nil {
|
|
|
|
file, err := s.rawListener.(*net.TCPListener).File()
|
|
|
|
if err == nil {
|
|
|
|
return file.Fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
2018-05-13 22:00:10 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:16:41 +08:00
|
|
|
// 设置自定义fd
|
|
|
|
func (s *gracefulServer) setFd(fd int) {
|
2019-06-19 09:06:52 +08:00
|
|
|
s.fd = uintptr(fd)
|
2018-05-10 19:16:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:35:08 +08:00
|
|
|
// 执行HTTPS监听
|
2019-06-19 09:06:52 +08:00
|
|
|
func (s *gracefulServer) ListenAndServeTLS(certFile, keyFile string, tlsConfig ...*tls.Config) error {
|
2019-08-03 15:54:12 +08:00
|
|
|
itemFunc := s.httpServer.Addr
|
2019-11-07 16:40:34 +08:00
|
|
|
var config *tls.Config
|
|
|
|
if len(tlsConfig) > 0 && tlsConfig[0] != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
config = tlsConfig[0]
|
|
|
|
} else if s.httpServer.TLSConfig != nil {
|
2019-11-07 16:40:34 +08:00
|
|
|
config = s.httpServer.TLSConfig
|
|
|
|
} else {
|
|
|
|
config = &tls.Config{}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if config.NextProtos == nil {
|
|
|
|
config.NextProtos = []string{"http/1.1"}
|
|
|
|
}
|
|
|
|
err := error(nil)
|
|
|
|
if len(config.Certificates) == 0 {
|
|
|
|
config.Certificates = make([]tls.Certificate, 1)
|
|
|
|
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf(`open cert file "%s","%s" failed: %s`, certFile, keyFile, err.Error()))
|
|
|
|
}
|
2019-08-03 15:54:12 +08:00
|
|
|
ln, err := s.getNetListener(itemFunc)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.listener = tls.NewListener(ln, config)
|
|
|
|
s.rawListener = ln
|
|
|
|
return s.doServe()
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 18:34:17 +08:00
|
|
|
// 获取服务协议字符串
|
|
|
|
func (s *gracefulServer) getProto() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
proto := "http"
|
|
|
|
if s.isHttps {
|
|
|
|
proto = "https"
|
|
|
|
}
|
|
|
|
return proto
|
2018-05-11 18:34:17 +08:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:35:08 +08:00
|
|
|
// 开始执行Web Server服务处理
|
|
|
|
func (s *gracefulServer) doServe() error {
|
2019-06-19 09:06:52 +08:00
|
|
|
action := "started"
|
|
|
|
if s.fd != 0 {
|
|
|
|
action = "reloaded"
|
|
|
|
}
|
2020-01-21 15:42:08 +08:00
|
|
|
s.server.Logger().Printf("%d: %s server %s listening on [%s]", gproc.Pid(), s.getProto(), action, s.itemFunc)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.status = SERVER_STATUS_RUNNING
|
|
|
|
err := s.httpServer.Serve(s.listener)
|
|
|
|
s.status = SERVER_STATUS_STOPPED
|
|
|
|
return err
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 自定义的net.Listener
|
2019-08-03 15:54:12 +08:00
|
|
|
func (s *gracefulServer) getNetListener(itemFunc string) (net.Listener, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
var ln net.Listener
|
|
|
|
var err error
|
|
|
|
if s.fd > 0 {
|
|
|
|
f := os.NewFile(s.fd, "")
|
|
|
|
ln, err = net.FileListener(f)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("%d: net.FileListener error: %v", gproc.Pid(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-26 14:56:05 +08:00
|
|
|
ln, err = net.Listen("tcp", itemFunc)
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
2019-11-26 14:56:05 +08:00
|
|
|
err = fmt.Errorf("%d: net.Listen error: %v", gproc.Pid(), err)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 14:56:05 +08:00
|
|
|
return ln, err
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 执行请求优雅关闭
|
|
|
|
func (s *gracefulServer) shutdown() {
|
2019-06-19 09:06:52 +08:00
|
|
|
if s.status == SERVER_STATUS_STOPPED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.httpServer.Shutdown(context.Background()); err != nil {
|
2020-01-21 15:42:08 +08:00
|
|
|
s.server.Logger().Errorf("%d: %s server [%s] shutdown error: %v", gproc.Pid(), s.getProto(), s.itemFunc, err)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-06 22:35:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 00:17:12 +08:00
|
|
|
// 执行请求强制关闭
|
|
|
|
func (s *gracefulServer) close() {
|
2019-06-19 09:06:52 +08:00
|
|
|
if s.status == SERVER_STATUS_STOPPED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.httpServer.Close(); err != nil {
|
2020-01-21 15:42:08 +08:00
|
|
|
s.server.Logger().Errorf("%d: %s server [%s] closed error: %v", gproc.Pid(), s.getProto(), s.itemFunc, err)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-13 00:17:12 +08:00
|
|
|
}
|