优化list——head

This commit is contained in:
aozhiwei 2023-09-14 22:26:52 +08:00
parent b5f94d01cc
commit 47db6c9c60
3 changed files with 4 additions and 115 deletions

View File

@ -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() {
this.aborted = true
}

View File

@ -241,10 +241,10 @@ func (this *dbPool) borrowConn(name string) *dataSource {
this.lock.Lock()
if head, ok := this.dataSourceHash[name]; ok {
if !head.Empty() {
next := head.Next()
next.Del()
ds := head.FirstEntry().(*dataSource)
ds.entry.DelInit()
this.lock.Unlock()
return next.GetData().(*dataSource)
return ds
}
}
this.lock.Unlock()

View File

@ -20,21 +20,11 @@ type HttpServer struct {
restHandlersMutex sync.RWMutex
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 {
this.handlers = make(map[string]HandlerFunc)
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.RegisterHandle("Ops", "selfChecking",
func (c *Context) {
@ -67,7 +57,6 @@ func (this *HttpServer) UnInit() {
func (this *HttpServer) Start(listen_port int32) {
atomic.AddInt64(&this.totalRequestTimes, 1)
this.installGlobalMiddleware()
GetSysLog().Info("HttpServer.Start listen_port:%d", listen_port)
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
if l, ok := this.restHandlers[pattern]; ok {
l.AddTail(&p.entry)
this.applyCurrGroup(l)
} else {
l := q5.MakeListHead()
l := q5.NewListHead()
l.AddTail(&p.entry)
this.applyCurrGroup(l)
this.restHandlers[pattern] = l
http.HandleFunc(pattern,
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)
}
}