2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// 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,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2017-12-07 14:57:16 +08:00
|
|
|
|
"sync"
|
|
|
|
|
"errors"
|
2018-04-10 10:32:37 +08:00
|
|
|
|
"strings"
|
2017-12-12 17:25:50 +08:00
|
|
|
|
"reflect"
|
2017-12-26 11:46:48 +08:00
|
|
|
|
"net/http"
|
2018-04-19 13:41:06 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/glog"
|
2018-04-11 16:06:45 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gcache"
|
2018-01-02 15:52:32 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
2018-04-09 17:55:46 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gtype"
|
2018-04-11 16:06:45 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gqueue"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-11 17:16:59 +08:00
|
|
|
|
const (
|
2018-04-14 12:20:14 +08:00
|
|
|
|
gHTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
|
2018-04-10 10:32:37 +08:00
|
|
|
|
gDEFAULT_SERVER = "default"
|
|
|
|
|
gDEFAULT_DOMAIN = "default"
|
2018-04-12 14:09:33 +08:00
|
|
|
|
gDEFAULT_METHOD = "ALL"
|
2018-04-10 10:32:37 +08:00
|
|
|
|
gDEFAULT_COOKIE_PATH = "/" // 默认path
|
|
|
|
|
gDEFAULT_COOKIE_MAX_AGE = 86400*365 // 默认cookie有效期(一年)
|
|
|
|
|
gDEFAULT_SESSION_MAX_AGE = 600 // 默认session有效期(600秒)
|
|
|
|
|
gDEFAULT_SESSION_ID_NAME = "gfsessionid" // 默认存放Cookie中的SessionId名称
|
2017-12-11 17:16:59 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-07 14:57:16 +08:00
|
|
|
|
// http server结构体
|
|
|
|
|
type Server struct {
|
2018-04-13 18:48:19 +08:00
|
|
|
|
hmmu sync.RWMutex // handler互斥锁
|
|
|
|
|
hhmu sync.RWMutex // hooks互斥锁
|
2018-04-15 10:30:59 +08:00
|
|
|
|
hmcmu sync.RWMutex // handlerCache互斥锁
|
|
|
|
|
hhcmu sync.RWMutex // hooksCache互斥锁
|
2018-04-10 10:32:37 +08:00
|
|
|
|
name string // 服务名称,方便识别
|
|
|
|
|
server http.Server // 底层http server对象
|
|
|
|
|
config ServerConfig // 配置对象
|
|
|
|
|
status int8 // 当前服务器状态(0:未启动,1:运行中)
|
|
|
|
|
methodsMap map[string]bool // 所有支持的HTTP Method(初始化时自动填充)
|
2018-04-12 14:09:33 +08:00
|
|
|
|
handlerMap HandlerMap // 所有注册的回调函数(静态匹配)
|
|
|
|
|
handlerTree map[string]interface{} // 所有注册的回调函数(动态匹配,树型+链表优先级匹配)
|
2018-04-13 18:48:19 +08:00
|
|
|
|
hooksTree map[string]interface{} // 所有注册的事件回调函数(动态匹配,树型+链表优先级匹配)
|
|
|
|
|
handlerCache *gcache.Cache // 服务注册路由内存缓存
|
|
|
|
|
hooksCache *gcache.Cache // 回调事件注册路由内存缓存
|
2018-04-10 10:32:37 +08:00
|
|
|
|
servedCount *gtype.Int // 已经服务的请求数(4-8字节,不考虑溢出情况)
|
|
|
|
|
cookieMaxAge *gtype.Int // Cookie有效期
|
|
|
|
|
sessionMaxAge *gtype.Int // Session有效期
|
|
|
|
|
sessionIdName *gtype.String // SessionId名称
|
|
|
|
|
cookies *gmap.IntInterfaceMap // 当前服务器正在服务(请求正在执行)的Cookie(每个请求一个Cookie对象)
|
|
|
|
|
sessions *gcache.Cache // Session内存缓存
|
2018-04-13 18:48:19 +08:00
|
|
|
|
closeQueue *gqueue.Queue // 请求结束的关闭队列(存放的是需要异步关闭处理的*Request对象)
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-11 17:16:59 +08:00
|
|
|
|
// 域名、URI与回调函数的绑定记录表
|
2018-04-12 14:09:33 +08:00
|
|
|
|
type HandlerMap map[string]*HandlerItem
|
2017-12-11 17:16:59 +08:00
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// http回调函数注册信息
|
2017-12-26 10:13:49 +08:00
|
|
|
|
type HandlerItem struct {
|
2018-04-12 14:09:33 +08:00
|
|
|
|
ctype reflect.Type // 控制器类型
|
|
|
|
|
fname string // 回调方法名称
|
|
|
|
|
faddr HandlerFunc // 准确的执行方法内存地址(与以上两个参数二选一)
|
|
|
|
|
uri string // 注册时的pattern - uri
|
|
|
|
|
method string // 注册时的pattern - method
|
|
|
|
|
domain string // 注册时的pattern - domain
|
|
|
|
|
priority int // 优先级,用于链表排序,值越大优先级越高
|
2017-12-12 17:25:50 +08:00
|
|
|
|
}
|
2017-12-07 14:57:16 +08:00
|
|
|
|
|
2017-12-26 10:13:49 +08:00
|
|
|
|
// http注册函数
|
2018-01-02 15:52:32 +08:00
|
|
|
|
type HandlerFunc func(*Request)
|
2017-12-26 10:13:49 +08:00
|
|
|
|
|
2017-12-08 12:03:21 +08:00
|
|
|
|
// Server表,用以存储和检索名称与Server对象之间的关联关系
|
2017-12-13 16:45:00 +08:00
|
|
|
|
var serverMapping = gmap.NewStringInterfaceMap()
|
2017-12-07 17:34:51 +08:00
|
|
|
|
|
2017-12-18 10:42:59 +08:00
|
|
|
|
// 获取/创建一个默认配置的HTTP Server(默认监听端口是80)
|
|
|
|
|
// 单例模式,请保证name的唯一性
|
2017-12-30 18:35:24 +08:00
|
|
|
|
func GetServer(names...string) (*Server) {
|
|
|
|
|
name := gDEFAULT_SERVER
|
|
|
|
|
if len(names) > 0 {
|
|
|
|
|
name = names[0]
|
|
|
|
|
}
|
2017-12-08 12:03:21 +08:00
|
|
|
|
if s := serverMapping.Get(name); s != nil {
|
|
|
|
|
return s.(*Server)
|
2017-12-07 17:34:51 +08:00
|
|
|
|
}
|
2018-04-11 16:06:45 +08:00
|
|
|
|
s := &Server {
|
2018-04-10 10:32:37 +08:00
|
|
|
|
name : name,
|
|
|
|
|
methodsMap : make(map[string]bool),
|
2018-04-12 14:09:33 +08:00
|
|
|
|
handlerMap : make(HandlerMap),
|
|
|
|
|
handlerTree : make(map[string]interface{}),
|
2018-04-13 18:48:19 +08:00
|
|
|
|
hooksTree : make(map[string]interface{}),
|
|
|
|
|
handlerCache : gcache.New(),
|
|
|
|
|
hooksCache : gcache.New(),
|
2018-04-10 10:32:37 +08:00
|
|
|
|
cookies : gmap.NewIntInterfaceMap(),
|
|
|
|
|
sessions : gcache.New(),
|
|
|
|
|
cookieMaxAge : gtype.NewInt(gDEFAULT_COOKIE_MAX_AGE),
|
|
|
|
|
sessionMaxAge : gtype.NewInt(gDEFAULT_SESSION_MAX_AGE),
|
|
|
|
|
sessionIdName : gtype.NewString(gDEFAULT_SESSION_ID_NAME),
|
2018-04-13 18:48:19 +08:00
|
|
|
|
servedCount : gtype.NewInt(),
|
|
|
|
|
closeQueue : gqueue.New(),
|
2017-12-30 17:09:00 +08:00
|
|
|
|
}
|
2018-04-13 18:48:19 +08:00
|
|
|
|
s.hooksCache.SetCap(10000)
|
|
|
|
|
s.handlerCache.SetCap(10000)
|
2017-12-26 11:46:48 +08:00
|
|
|
|
for _, v := range strings.Split(gHTTP_METHODS, ",") {
|
|
|
|
|
s.methodsMap[v] = true
|
|
|
|
|
}
|
2017-12-08 12:03:21 +08:00
|
|
|
|
s.SetConfig(defaultServerConfig)
|
|
|
|
|
serverMapping.Set(name, s)
|
|
|
|
|
return s
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:06:45 +08:00
|
|
|
|
// 阻塞执行监听
|
2017-12-07 17:34:51 +08:00
|
|
|
|
func (s *Server) Run() error {
|
2017-12-08 12:03:21 +08:00
|
|
|
|
if s.status == 1 {
|
|
|
|
|
return errors.New("server is already running")
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 底层http server配置
|
|
|
|
|
if s.config.Handler == nil {
|
|
|
|
|
s.config.Handler = http.HandlerFunc(s.defaultHttpHandle)
|
|
|
|
|
}
|
2017-12-07 14:57:16 +08:00
|
|
|
|
// 底层http server初始化
|
2017-11-23 10:21:28 +08:00
|
|
|
|
s.server = http.Server {
|
|
|
|
|
Addr : s.config.Addr,
|
|
|
|
|
Handler : s.config.Handler,
|
|
|
|
|
ReadTimeout : s.config.ReadTimeout,
|
|
|
|
|
WriteTimeout : s.config.WriteTimeout,
|
|
|
|
|
IdleTimeout : s.config.IdleTimeout,
|
|
|
|
|
MaxHeaderBytes : s.config.MaxHeaderBytes,
|
|
|
|
|
}
|
2018-03-13 14:41:49 +08:00
|
|
|
|
// 开启异步处理队列处理循环
|
|
|
|
|
s.startCloseQueueLoop()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 执行端口监听
|
2017-12-07 14:57:16 +08:00
|
|
|
|
if err := s.server.ListenAndServe(); err != nil {
|
2018-04-19 13:41:06 +08:00
|
|
|
|
glog.Error(err)
|
2017-12-07 14:57:16 +08:00
|
|
|
|
return err
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2017-12-07 17:34:51 +08:00
|
|
|
|
s.status = 1
|
2017-12-07 14:57:16 +08:00
|
|
|
|
return nil
|
2018-04-15 10:30:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空当前的handlerCache
|
|
|
|
|
func (s *Server) clearHandlerCache() {
|
|
|
|
|
// 只有在运行时才会生效
|
|
|
|
|
if s.status != 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.hmcmu.Lock()
|
|
|
|
|
defer s.hmcmu.Unlock()
|
|
|
|
|
s.handlerCache.Close()
|
|
|
|
|
s.handlerCache = gcache.New()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空当前的hooksCache
|
|
|
|
|
func (s *Server) clearHooksCache() {
|
|
|
|
|
// 只有在运行时才会生效
|
|
|
|
|
if s.status != 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.hhcmu.Lock()
|
|
|
|
|
defer s.hhcmu.Unlock()
|
|
|
|
|
s.hooksCache.Close()
|
|
|
|
|
s.hooksCache = gcache.New()
|
2018-04-13 15:19:31 +08:00
|
|
|
|
}
|