Engine

pkg/net/http/blademaster/server.go

  1. // Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
  2. // Create an instance of Engine, by using New() or Default()
  3. type Engine struct {
  4. RouterGroup
  5. lock sync.RWMutex
  6. conf *ServerConfig
  7. address string
  8. trees methodTrees
  9. server atomic.Value // store *http.Server
  10. metastore map[string]map[string]interface{} // metastore is the path as key and the metadata of this path as value, it export via /metadata
  11. pcLock sync.RWMutex
  12. methodConfigs map[string]*MethodConfig
  13. injections []injection
  14. // If enabled, the url.RawPath will be used to find parameters.
  15. UseRawPath bool
  16. // If true, the path value will be unescaped.
  17. // If UseRawPath is false (by default), the UnescapePathValues effectively is true,
  18. // as url.Path gonna be used, which is already unescaped.
  19. UnescapePathValues bool
  20. // If enabled, the router checks if another method is allowed for the
  21. // current route, if the current request can not be routed.
  22. // If this is the case, the request is answered with 'Method Not Allowed'
  23. // and HTTP status code 405.
  24. // If no other Method is allowed, the request is delegated to the NotFound
  25. // handler.
  26. HandleMethodNotAllowed bool
  27. allNoRoute []HandlerFunc
  28. allNoMethod []HandlerFunc
  29. noRoute []HandlerFunc
  30. noMethod []HandlerFunc
  31. pool sync.Pool
  32. }

Context

pkg/net/http/blademaster/context.go

  1. type Context struct {
  2. type Context struct {
  3. context.Context
  4. Request *http.Request
  5. Writer http.ResponseWriter
  6. // flow control
  7. index int8
  8. handlers []HandlerFunc
  9. // Keys is a key/value pair exclusively for the context of each request.
  10. Keys map[string]interface{}
  11. // This mutex protect Keys map
  12. keysMutex sync.RWMutex
  13. Error error
  14. method string
  15. engine *Engine
  16. RoutePath string
  17. Params Params
  18. }
  • 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 实例。