Gin은 Go(Golang)로 작성된 HTTP 웹 프레임워크입니다. Martini와 유사한 API를 가지고 있지만 Martini보다 성능이 40배 빠릅니다. 최고의 성능이 필요하다면 Gin을 사용하세요.
gin 왜 빠른가요?
먼저 공식 벤치마크 테스트를 살펴보겠습니다. Address[1]
이것은 일부일 뿐이며 위 주소로 이동하면 전체 버전을 볼 수 있습니다.
성능 테스트를 통해 이를 확인할 수 있습니다gin 요청 처리 성능 실제로 다른 것보다 낫습니다(27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">웹 프레임워크가 훨씬 빠릅니다. . gin 在处理请求的性能上确实比其他的web 框架快上不少。
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default()
type Engine struct {
RouterGroup // 用于注册根路由组使用
...
pool sync.Pool // 用于 gin.context 对象池
trees methodTrees // 用于保存的请求路径
...
}
在 gin 的Engine
성능 문제는 주로 🎜httprouter🎜🎜[2]🎜 및 httprouter사용<code style="font-size: 14px;word-wrap: break-word;padding: 2px 4px;border-radius: 4px;margin: 0 2px;color: #1e6bb8; background-color : rgba( 27,31,35,.05);font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;word-break: break-all;">Trie Tree(사전 트리) 경로 저장 구조, 고성능 조회. 🎜🎜🎜🎜gin.Engine 소스 코드🎜
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc
// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
Handlers HandlersChain
basePath string
engine *Engine
root bool
}
🎜 in gin의 엔진 그 중에서 가장 중요한 것은 이 세 가지 분야이다. 🎜
gin.RouterGroup 源码
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc
// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
Handlers HandlersChain
basePath string
engine *Engine
root bool
}
type node struct {
path string
indices string
wildChild bool
nType nodeType
priority uint32
children []*node // child nodes, at most 1 :param style node at the end of the array
handlers HandlersChain
fullPath string
}
type methodTree struct {
method string
root *node
}
type methodTrees []methodTree
현재 등록된 경로에 미들웨어 기능을 추가하고 등록된 기능을 맨 마지막에 넣습니다.글로벌 라우팅 테이블에 추가🎜 🎜IRoutes인터페이스 ul>🎜combineHandlers는 최대 63-1개 개별 기능을 지원합니다. 🎜
그룹 기능
🎜이제 그룹 구현, 이 메소드를 호출하면 새로운 🎜그룹 라우팅을 사용하는 것도 마지막 호출입니다 group.handle 등록 방법, basePath는 그룹화 중에 설정되었으며, 함수 등록 시 경로가 요청 라우팅 경로입니다. 🎜🎜한 가지 주의할 점은 이전에 ·그룹 ·을 사용할 생각을 한 후 후속 등록에서는 반환 값을 사용하지 않았다는 것입니다. 등록된 경로가 글로벌 라우팅 테이블에 등록되는 방식은 소스 코드를 읽고 나서야 이해됩니다. 🎜