Engine
pkg/net/http/blademaster/server.go
// 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 {RouterGrouplock sync.RWMutexconf *ServerConfigaddress stringtrees methodTreesserver atomic.Value // store *http.Servermetastore map[string]map[string]interface{} // metastore is the path as key and the metadata of this path as value, it export via /metadatapcLock sync.RWMutexmethodConfigs map[string]*MethodConfiginjections []injection// If enabled, the url.RawPath will be used to find parameters.UseRawPath bool// If true, the path value will be unescaped.// If UseRawPath is false (by default), the UnescapePathValues effectively is true,// as url.Path gonna be used, which is already unescaped.UnescapePathValues bool// If enabled, the router checks if another method is allowed for the// current route, if the current request can not be routed.// If this is the case, the request is answered with 'Method Not Allowed'// and HTTP status code 405.// If no other Method is allowed, the request is delegated to the NotFound// handler.HandleMethodNotAllowed boolallNoRoute []HandlerFuncallNoMethod []HandlerFuncnoRoute []HandlerFuncnoMethod []HandlerFuncpool sync.Pool}
Context
pkg/net/http/blademaster/context.go
type Context struct {type Context struct {context.ContextRequest *http.RequestWriter http.ResponseWriter// flow controlindex int8handlers []HandlerFunc// Keys is a key/value pair exclusively for the context of each request.Keys map[string]interface{}// This mutex protect Keys mapkeysMutex sync.RWMutexError errormethod stringengine *EngineRoutePath stringParams Params}
- blademaster 会使用配置的 server timeout (默认1s) 作为一次请求整个过程中的超时时间,使用该context调用dao做数据库、缓存操作查询时均会将该超时时间传递下去,一旦抵达deadline,后续相关操作均会返回
context deadline exceeded。 - Request 和 Writer 字段用于获取当前请求的与输出响应。
- index 和 handlers 用于 handler 的流程控制;handlers 中存储了当前请求需要执行的所有 handler,index 用于标记当前正在执行的 handler 的索引位。
- Keys 用于在 handler 之间传递一些额外的信息。
- Error 用于存储整个请求处理过程中的错误。
- method 用于检查当前请求的 Method 是否与预定义的相匹配。
- engine 字段指向当前 blademaster 的 Engine 实例。
