Fixed typos and grammar for 03.3.md
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
# 3.3 How Go works with web
|
||||
|
||||
We learned to use `net/http` package to build a simple web server in previous section, but all the working principles are the same as we talked in first section of this chapter.
|
||||
We learned to use the `net/http` package to build a simple web server in the previous section, and all those working principles are the same as those we will talk about in the first section of this chapter.
|
||||
|
||||
## Some concepts in web working principles
|
||||
## Concepts in web principles
|
||||
|
||||
Request: request data from users, including POST, GET, Cookie and URL.
|
||||
|
||||
@@ -10,29 +10,29 @@ Response: response data from server to clients.
|
||||
|
||||
Conn: connections between clients and servers.
|
||||
|
||||
Handler: logic of handle request and produce response.
|
||||
Handler: Request handling logic and response generation.
|
||||
|
||||
## http package operating mechanism
|
||||
|
||||
The following picture shows that work flow of Go's web server.
|
||||
The following picture shows the work flow of a Go web server.
|
||||
|
||||

|
||||
|
||||
Figure 3.9 http work flow
|
||||
|
||||
1. Create listening socket, listen to a port and wait for clients.
|
||||
1. Create a listening socket, listen to a port and wait for clients.
|
||||
2. Accept requests from clients.
|
||||
3. Handle requests, read HTTP header, if it uses POST method, also need to read data in message body and give them to handlers. Finally, socket returns response data to clients.
|
||||
3. Handle requests, read HTTP header. If the request uses POST method, read data in the message body and pass them to handlers. Finally, socket returns response data to clients.
|
||||
|
||||
Once we know the answers of three following questions, it's easy to know how web works in Go.
|
||||
Once we know the answers to the three following questions, it's easy to know how the web works in Go.
|
||||
|
||||
- How to listen to a port?
|
||||
- How to accept client requests?
|
||||
- How to allocate handlers?
|
||||
- How do we listen to a port?
|
||||
- How do we accept client requests?
|
||||
- How do we allocate handlers?
|
||||
|
||||
In the previous section we saw that Go uses `ListenAndServe` to handle these problems: initialize a server object, call `net.Listen("tcp", addr)` to setup a TCP listener and listen to specific address and port.
|
||||
In the previous section we saw that Go uses `ListenAndServe` to handle these steps: initialize a server object, call `net.Listen("tcp", addr)` to setup a TCP listener and listen to a specific address and port.
|
||||
|
||||
Let's take a look at `http` package's source code.
|
||||
Let's take a look at the `http` package's source code.
|
||||
|
||||
//Build version go1.1.2.
|
||||
func (srv *Server) Serve(l net.Listener) error {
|
||||
@@ -66,15 +66,15 @@ Let's take a look at `http` package's source code.
|
||||
}
|
||||
|
||||
|
||||
How to accept client requests after listened to the port? In the source code, we can see that it calls `srv.Serve(net.Listener)` to handle client requests. In body of function there is a `for{}`, it accepts request, creates a new connection, and then starts a new goroutine, and passes request data to this goroutine: `go c.serve()`. This is how Go supports high concurrency, and every goroutine is independent.
|
||||
How do we accept client requests after we begin listening to a port? In the source code, we can see that `srv.Serve(net.Listener)` is called to handle client requests. In the body of the function there is a `for{}`. It accepts a request, creates a new connection then starts a new goroutine, passing the request data to the `go c.serve()` goroutine. This is how Go supports high concurrency, and every goroutine is independent.
|
||||
|
||||
Now, how to use specific functions to handle requests? `conn` parses request `c.ReadRequest()` at first, and get corresponding handler: `handler := c.server.Handler` which is the second argument we passed when we called `ListenAndServe`. Because we passed `nil`, so Go uses it's default handler `handler = DefaultServeMux`. So what is `DefaultServeMux` doing here? Well, this is the variable of router at this time, it calls handler functions for specific URLs. Did we setting this? Yes, we did. Remember in the first line we used `http.HandleFunc("/", sayhelloName)`. It's like you use this function to register the router rule for "/" path. When the URL is `/`, router calls function `sayhelloName`. DefaultServeMux calls ServerHTTP to get handler function for different path, and it calls `sayhelloName` in this case. Finally, server writes data and response to clients.
|
||||
How do we use specific functions to handle requests? `conn` parses request `c.ReadRequest()` at first, then gets the corresponding handler: `handler := c.server.Handler` which is the second argument we passed when we called `ListenAndServe`. Because we passed `nil`, Go uses its default handler `handler = DefaultServeMux`. So what is `DefaultServeMux` doing here? Well, its the router variable which can call handler functions for specific URLs. Did we set this? Yes, we did. We did this in the first line where we used `http.HandleFunc("/", sayhelloName)`. We're using this function to register the router rule for the "/" path. When the URL is `/`, the router calls the function `sayhelloName`. DefaultServeMux calls ServerHTTP to get handler functions for different paths, calling `sayhelloName` in this specific case. Finally, the server writes data and responds to clients.
|
||||
|
||||
Detailed work flow:
|
||||
|
||||

|
||||
|
||||
Figure 3.10 Work flow of handling a HTTP request
|
||||
Figure 3.10 Work flow of handling an HTTP request
|
||||
|
||||
I think you should know how Go runs web servers now.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user