优化list——head
This commit is contained in:
parent
b5f94d01cc
commit
47db6c9c60
11
context.go
11
context.go
@ -26,17 +26,6 @@ func (this *Context) do() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Context) Next() {
|
|
||||||
this.suspended = true
|
|
||||||
this.l.ForEachFrom(this.c.entry.Next(),
|
|
||||||
func(data interface{}) bool {
|
|
||||||
m := data.(*middleware)
|
|
||||||
this.c = m
|
|
||||||
m.handlerFunc(this)
|
|
||||||
return !this.aborted
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Context) Abort() {
|
func (this *Context) Abort() {
|
||||||
this.aborted = true
|
this.aborted = true
|
||||||
}
|
}
|
||||||
|
@ -241,10 +241,10 @@ func (this *dbPool) borrowConn(name string) *dataSource {
|
|||||||
this.lock.Lock()
|
this.lock.Lock()
|
||||||
if head, ok := this.dataSourceHash[name]; ok {
|
if head, ok := this.dataSourceHash[name]; ok {
|
||||||
if !head.Empty() {
|
if !head.Empty() {
|
||||||
next := head.Next()
|
ds := head.FirstEntry().(*dataSource)
|
||||||
next.Del()
|
ds.entry.DelInit()
|
||||||
this.lock.Unlock()
|
this.lock.Unlock()
|
||||||
return next.GetData().(*dataSource)
|
return ds
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.lock.Unlock()
|
this.lock.Unlock()
|
||||||
|
102
httpserver.go
102
httpserver.go
@ -20,21 +20,11 @@ type HttpServer struct {
|
|||||||
|
|
||||||
restHandlersMutex sync.RWMutex
|
restHandlersMutex sync.RWMutex
|
||||||
restHandlers map[string]*q5.ListHead
|
restHandlers map[string]*q5.ListHead
|
||||||
|
|
||||||
groupHandlersMutex sync.RWMutex
|
|
||||||
groupHandlers map[string]*q5.ListHead
|
|
||||||
|
|
||||||
globalMiddlewareMutex sync.RWMutex
|
|
||||||
globalMiddlewareList q5.ListHead
|
|
||||||
|
|
||||||
currGroups []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
|
func (this *HttpServer) Init(serviceName string, logOutputTime int32) *HttpServer {
|
||||||
this.handlers = make(map[string]HandlerFunc)
|
this.handlers = make(map[string]HandlerFunc)
|
||||||
this.restHandlers = make(map[string]*q5.ListHead)
|
this.restHandlers = make(map[string]*q5.ListHead)
|
||||||
this.groupHandlers = make(map[string]*q5.ListHead)
|
|
||||||
this.globalMiddlewareList.Init(nil)
|
|
||||||
this.RegisterRestHandle("/webapp/index.php", this.dispatchRequest)
|
this.RegisterRestHandle("/webapp/index.php", this.dispatchRequest)
|
||||||
this.RegisterHandle("Ops", "selfChecking",
|
this.RegisterHandle("Ops", "selfChecking",
|
||||||
func (c *Context) {
|
func (c *Context) {
|
||||||
@ -67,7 +57,6 @@ func (this *HttpServer) UnInit() {
|
|||||||
|
|
||||||
func (this *HttpServer) Start(listen_port int32) {
|
func (this *HttpServer) Start(listen_port int32) {
|
||||||
atomic.AddInt64(&this.totalRequestTimes, 1)
|
atomic.AddInt64(&this.totalRequestTimes, 1)
|
||||||
this.installGlobalMiddleware()
|
|
||||||
GetSysLog().Info("HttpServer.Start listen_port:%d", listen_port)
|
GetSysLog().Info("HttpServer.Start listen_port:%d", listen_port)
|
||||||
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", listen_port), nil)
|
go http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", listen_port), nil)
|
||||||
}
|
}
|
||||||
@ -126,11 +115,9 @@ func (this *HttpServer) RegisterRestHandle(pattern string,
|
|||||||
p.handlerFunc = handle
|
p.handlerFunc = handle
|
||||||
if l, ok := this.restHandlers[pattern]; ok {
|
if l, ok := this.restHandlers[pattern]; ok {
|
||||||
l.AddTail(&p.entry)
|
l.AddTail(&p.entry)
|
||||||
this.applyCurrGroup(l)
|
|
||||||
} else {
|
} else {
|
||||||
l := q5.MakeListHead()
|
l := q5.NewListHead()
|
||||||
l.AddTail(&p.entry)
|
l.AddTail(&p.entry)
|
||||||
this.applyCurrGroup(l)
|
|
||||||
this.restHandlers[pattern] = l
|
this.restHandlers[pattern] = l
|
||||||
http.HandleFunc(pattern,
|
http.HandleFunc(pattern,
|
||||||
func (w http.ResponseWriter, r *http.Request) {
|
func (w http.ResponseWriter, r *http.Request) {
|
||||||
@ -143,90 +130,3 @@ func (this *HttpServer) RegisterRestHandle(pattern string,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpServer) Use(handle HandlerFunc) {
|
|
||||||
if this.totalRequestTimes > 0 {
|
|
||||||
panic("")
|
|
||||||
}
|
|
||||||
this.globalMiddlewareMutex.Lock()
|
|
||||||
defer this.globalMiddlewareMutex.Unlock()
|
|
||||||
|
|
||||||
p := new(middleware)
|
|
||||||
p.entry.Init(nil)
|
|
||||||
p.handlerFunc = handle
|
|
||||||
this.globalMiddlewareList.AddTail(&p.entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HttpServer) installGlobalMiddleware() {
|
|
||||||
this.restHandlersMutex.Lock()
|
|
||||||
this.globalMiddlewareMutex.Lock()
|
|
||||||
defer this.restHandlersMutex.Unlock()
|
|
||||||
defer this.globalMiddlewareMutex.Unlock()
|
|
||||||
|
|
||||||
for _, l := range this.restHandlers {
|
|
||||||
this.globalMiddlewareList.ForEach_r(
|
|
||||||
func (data interface{}) bool {
|
|
||||||
m := data.(*middleware)
|
|
||||||
p := new(middleware)
|
|
||||||
p.entry.Init(p)
|
|
||||||
p.handlerFunc = m.handlerFunc
|
|
||||||
l.AddHead(&p.entry)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HttpServer) UseGroupBegin(groupNames ...string) {
|
|
||||||
if this.currGroups != nil {
|
|
||||||
panic("")
|
|
||||||
}
|
|
||||||
this.currGroups = groupNames
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HttpServer) UseGroupEnd() {
|
|
||||||
if this.currGroups == nil {
|
|
||||||
panic("")
|
|
||||||
}
|
|
||||||
this.currGroups = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HttpServer) applyCurrGroup(l *q5.ListHead) {
|
|
||||||
if this.currGroups != nil {
|
|
||||||
for _, groupName := range this.currGroups {
|
|
||||||
this.groupHandlersMutex.Lock()
|
|
||||||
defer this.groupHandlersMutex.Unlock()
|
|
||||||
|
|
||||||
gl, ok := this.groupHandlers[groupName];
|
|
||||||
if ok {
|
|
||||||
gl.ForEach_r(
|
|
||||||
func (data interface{}) bool {
|
|
||||||
m := data.(*middleware)
|
|
||||||
p := new(middleware)
|
|
||||||
p.entry.Init(p)
|
|
||||||
p.handlerFunc = m.handlerFunc
|
|
||||||
l.AddHead(&p.entry)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
panic("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HttpServer) DefineGroup(groupName string, handlers ...HandlerFunc) {
|
|
||||||
this.groupHandlersMutex.Lock()
|
|
||||||
defer this.groupHandlersMutex.Unlock()
|
|
||||||
|
|
||||||
l, ok := this.groupHandlers[groupName];
|
|
||||||
if !ok {
|
|
||||||
l = q5.MakeListHead()
|
|
||||||
this.groupHandlers[groupName] = l
|
|
||||||
}
|
|
||||||
for i := 0; i < len(handlers); i++ {
|
|
||||||
p := new(middleware)
|
|
||||||
p.entry.Init(p)
|
|
||||||
p.handlerFunc = handlers[i]
|
|
||||||
l.AddTail(&p.entry)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user