GO

iris 路由注册和上下文context

2018-11-15  本文已影响0人  fjxCode

路由注册到app.routes

http请求的处理,相较于基本的net/http包,iris框架将http请求(w,*r)及其它上下文,封装成ctx,逐个调用handler闭包进行处理,最后分发函数返回值(反射)以及响应。请求响应相当于流水线上的一个商品,被一组当中的每个handler处理。路由注册的过程,在完成之前的方法解析,path解析之后,就需要组装一个handler链到路由了。

每一步先由api.relativePath和方法传入的参数构成fullPath。

第二步然后添加handler,顺序为:

第三步构建Route:

type Route struct {
        Name       string          `json:"name"`
        Method     string          `json:"method"`
        methodBckp string
        Subdomain  string          `json:"subdomain"`
        tmpl       *macro.Template
        beginHandlers context.Handlers
        Handlers        context.Handlers `json:"-"`
        MainHandlerName string           `json:"mainHandlerName"`
        doneHandlers context.Handlers
        Path         string `json:"path"`
        FormattedPath string `json:"formattedPath"`
}

Name格式为defaultName := method + subdomain + 未注值的路径格式。其中路径有三个相关变量:Subdomain Path FormattedPath。FormattedPath是将path中的:变量替换为%v。构建路由Router时将路径解析的macroHandler,添加在最前面。

最后将路由注册到app.APIBuilder.routes,类型为[]Route。

context数据结构

iris框架定义了handler闭包的操作对象context.context。不是指TCP的context,也不是net/http中的context。数据结构如下:

type context struct {
        id uint64
        writer ResponseWriter
        request *http.Request
        currentRouteName string

        app Application
        handlers Handlers
        currentHandlerIndex int
}

前文已经提过,由于反射字段的不可复用性,造成go的反射效率下降。如果处理每次的请求都要在运行时重新构建context,就会降低性能。iris用c.pool保存ctx,实现ctx的可复用。c.pool的类型是sync.pool。id确定ctx的唯一性,方便保存在c.pool集合中。request writer都是net/http标准库的类型,直接传递。

params RequestParams 是一个专用的KV存储,类型为[string]interface{}。用来保存path param。values用来保存其它KV信息。

app引用自iris实例,可以获取全部的实例字段。其中路由信息单独拿出:currentRouteName路由名,对应c.routes的键。路由handlers,currentHandlerIndex。

ctx是可复用的,需要构建ctx时,不是初始化而是直接从池中拿一个实例。空实例仅配置了app字段。第一步包装net/http包的(w,*r)。第二步根据request确定路由信息,并赋到ctx相应字段。第三步会执行流水线式地Do(handlers),反射调用得到响应信息,并写入到ctx中。最后一步将针对特定http请求的信息清掉,并放回c.pool。

标准库中的net/http Request

type Request struct {
    Method string

    URL *url.URL

    Header Header

    Body io.ReadCloser

    GetBody func() (io.ReadCloser, error)

    ContentLength int64

    TransferEncoding []string

    Close bool

    Host string

    Form url.Values

    PostForm url.Values

    MultipartForm *multipart.Form

    Trailer Header

    RemoteAddr string

    RequestURI string

    TLS *tls.ConnectionState

    Cancel <-chan struct{}

    Response *Response

    ctx context.Context
}

标准库net/http Response

type Response struct {
    Status     string 
    StatusCode int    
    Proto      string 
    ProtoMajor int    
    ProtoMinor int    

    Header Header

    Body io.ReadCloser

    ContentLength int64

    TransferEncoding []string

    Close bool

    Uncompressed bool

    Trailer Header

    Request *Request

    TLS *tls.ConnectionState
}
上一篇 下一篇

猜你喜欢

热点阅读