Home > Backend Development > Golang > Gin request process source code analysis

Gin request process source code analysis

Release: 2023-08-04 17:26:13
forward
1091 people have browsed it

http service of the standard library

Handler接口就可以注册到标准库的http server中。然后就会启动一个web应用。http请求流程当发生一个http请求的时候,在内部处理的流程是下面这样的:开启一个协程进行请求处理在conn.serve中调用serverHandler.ServeHTTP 函数如果有自己注册的Handle,那么就会调用注册的Handle的ServeHTTP 方法。这里还要注意的2个点如果自己在启动的时候没有注册自己的Handle,那么会采用标准库默认的ServeMux,全局名称为DefaultServeMux。如果请求URI为*并且请求Method为OPTIONS,那么Handle行为会被改成默认的globalOptionsHandler。上述分析的源码为GO 1.18.3。Gin 处理请求的流程前面我们看到只要注册自己的Handle接口到标准库就可以接管请求的处理;那么我们来看一下gin的Handle接口实现。在gin中,handleHTTPRequest就是匹配路径和对应handle 的处理函数。流程大致是这样:获取请求的路径在trees中找到对应的methodTree
Copy after login
  • methodTree中匹配对应路径的处理函数handle
    Copy after login
  • Execute the registered function through the Next method
  • Gin's Context source code

    Before executing the registered function, we found that a sync was used in the ServeHTTP method .Pool, it is actually the reuse of gin.Context.

    Gin request process source code analysis

    Let’s take a look at its structure:

    // Context is the most important part of gin. It allows us to pass variables between middleware,
    // manage the flow, validate the JSON of a request and render a JSON response for example.
    type Context struct {
     writermem responseWriter
     Request   *http.Request
     Writer    ResponseWriter
    
     Params   Params
     handlers HandlersChain
     index    int8
     fullPath string
    
     engine       *Engine
     params       *Params
     skippedNodes *[]skippedNode
    
     // This mutex protects Keys map.
     mu sync.RWMutex
      ...
    }
    Copy after login

    The official req and resp will be saved in Context in. And gin has added an extension to the official http.ResponseWriter function, that is, it has defined an interface gin.ResponseWriter

    Others Some methods are packages for daily use to facilitate development.

    Context's Bind class method analysis

    Gin request process source code analysis

    In the source code, you can see that a total of Binding supports these ;The implementation is deserialization, the details will not be discussed one by one.

    The key point is that after bind is completed, there is a validate method, which is actually used github.com/go-playground/validator/v10As a library for verifying data.

    Gin request process source code analysis

    And use lazy loading to initialize, which means that if you don’t use it, the object will not be initialized.

    Gin request process source code analysis

    For the development process of validating data, please see the detailed usage of validator[1].

    In the mode.go file of gin, there are controls for some behaviors. For example, DisableBindValidation can turn off the data validation function, which is Call this method before the service starts to shut down.

    Gin request process source code analysis

    ginS folder

    This folder defines a default internal global gin.Engine object.

    Gin request process source code analysis

    And it is also initialized using lazy loading.

    Gin request process source code analysis

    So if you want to use the global gin.Engine, you can use this package, so you don’t have to save your own global gin. Engine object.

The above is the detailed content of Gin request process source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
gin
source:Golang菜鸟
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template