go web execution process

Works 1.Web servers can be simply summarized as follows:

Client establishes to the server via TCP / IP protocol TCP connection
the client sends HTTP protocol to the server request packet, the requesting server resources in the document
server sends the HTTP protocol to the client response packet, if the requested resource contains the contents of dynamic languages, then the server calls the dynamic language interpretation engine handles "dynamic content" and processing the resulting data back to the client
the client and the server is disconnected. By the client interprets the HTML document, the result of rendering graphics on the client screen


2.go web implementation process:

First call Http.HandleFunc

(1) in order to do a few things:

1 called DefaultServeMux of HandleFunc

2 of Handle calls DefaultServeMux

3 to DefaultServeMux the map [string] muxEntry increased corresponding handler and routing rules

(2) Second call http.ListenAndServe ( ": 9090", nil)

In order to do a few things:

1 instance of Server

2 Call Server, ListenAndServe ()

3 Call net.Listen ( "tcp", addr) listening port

4 starts a for loop, the loop in the Accept Request

5 instantiated for each request a Conn, and a turn to go c.serve goroutine servicing the request ()

6 reads each request content w, err: = c.readRequest ()

7 determines whether the handler is empty, if no handler (not provided in this example handler), it is set to handler DefaultServeMux

8 call handler of ServeHttp

9 In this example, the following proceeds to DefaultServeMux.ServeHttp

The selection handler 10 request, and the handler into the ServeHTTP

mux.handler(r).ServeHTTP(w, r)
11 选择handler:

A determines whether a route to meet this request (the loop through ServeMux muxEntry)

B If there are routes meet, call routing handler of this ServeHTTP

C If you do not meet the routing, call NotFoundHandler of ServeHTTP

Guess you like

Origin www.cnblogs.com/tomtellyou/p/12599520.html