From 47db6c9c60d59681e7fc02bd5649d7fe918b124e Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 14 Sep 2023 22:26:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96list=E2=80=94=E2=80=94head?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context.go | 11 ------ dbpool.go | 6 +-- httpserver.go | 102 +------------------------------------------------- 3 files changed, 4 insertions(+), 115 deletions(-) diff --git a/context.go b/context.go index 1ed8c7f..7e7418b 100644 --- a/context.go +++ b/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() { this.aborted = true } diff --git a/dbpool.go b/dbpool.go index 724463f..c9ce380 100644 --- a/dbpool.go +++ b/dbpool.go @@ -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() diff --git a/httpserver.go b/httpserver.go index d4bd48b..2153386 100644 --- a/httpserver.go +++ b/httpserver.go @@ -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) - } -}