improve comment of router feature for ghttp.Server (#3014)

This commit is contained in:
John Guo
2023-10-16 20:41:10 +08:00
committed by GitHub
parent 0e7d7d1eee
commit f464f30e6f
3 changed files with 18 additions and 2 deletions

View File

@ -87,7 +87,10 @@ type (
// HandlerItem is the registered handler for route handling,
// including middleware and hook functions.
HandlerItem struct {
Id int // Unique handler item id mark.
// Unique handler item id mark.
// Note that the handler function may be registered multiple times as different handler items,
// which have different handler item id.
Id int
Name string // Handler name, which is automatically retrieved from runtime stack when registered.
Type HandlerType // Handler type: object/handler/middleware/hook.
Info handlerFuncInfo // Handler function information.

View File

@ -207,7 +207,8 @@ func (s *Server) doSetHandler(
// and the leaf node also has "*list" item. If the node is not a fuzzy node either
// a leaf, it neither has "*list" item.
// 2. The "*list" item is a list containing registered router items ordered by their
// priorities from high to low.
// priorities from high to low. If it's a fuzzy node, all the sub router items
// from this fuzzy node will also be added to its "*list" item.
// 3. There may be repeated router items in the router lists. The lists' priorities
// from root to leaf are from low to high.
var p = s.serveTree[domain]

View File

@ -112,6 +112,7 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*Han
)
// The default domain has the most priority when iteration.
// Please see doSetHandler if you want to get known about the structure of serveTree.
for _, domainItem := range []string{DefaultDomainName, domain} {
p, ok := s.serveTree[domainItem]
if !ok {
@ -157,6 +158,17 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*Han
item := e.Value.(*HandlerItem)
// Filter repeated handler items, especially the middleware and hook handlers.
// It is necessary, do not remove this checks logic unless you really know how it is necessary.
//
// The `repeatHandlerCheckMap` is used for repeat handler filtering during handler searching.
// As there are fuzzy nodes, and the fuzzy nodes have both sub-nodes and sub-list nodes, there
// may be repeated handler items in both sub-nodes and sub-list nodes. It here uses handler item id to
// identify the same handler item that registered.
//
// The same handler item is the one that is registered in the same function doSetHandler.
// Note that, one handler function(middleware or hook function) may be registered multiple times as
// different handler items using function doSetHandler, and they have different handler item id.
//
// Note that twice, the handler function may be registered multiple times as different handler items.
if _, isRepeatedHandler := repeatHandlerCheckMap[item.Id]; isRepeatedHandler {
continue
} else {