From 97100f2d6885c38eb8892fcbadfae090d52f874f Mon Sep 17 00:00:00 2001 From: ldynia Date: Tue, 18 Jul 2017 18:32:19 +0200 Subject: [PATCH] adding some fixes --- en/02.7.md | 42 +++++++++++++++++++++--------------------- en/03.1.md | 18 +++++++++--------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/en/02.7.md b/en/02.7.md index 2b804ff8c..ab84dada9 100644 --- a/en/02.7.md +++ b/en/02.7.md @@ -1,15 +1,15 @@ # Concurrency -It is said that Go is the C language of the 21st century. I think there are two reasons: first, Go is a simple language; second, concurrency is a hot topic in today's world, and Go supports this feature at the language level. +It is said that Go is the C of the 21st century. I think there are two reasons for it. First, Go is a simple language. Second, concurrency is a hot topic in today's world, and Go supports this feature at the language level. ## goroutine - -goroutines and concurrency are built into the core design of Go. They're similar to threads but work differently. More than a dozen goroutines maybe only have 5 or 6 underlying threads. Go also gives you full support to sharing memory in your goroutines. One goroutine usually uses 4~5 KB of stack memory. Therefore, it's not hard to run thousands of goroutines on a single computer. A goroutine is more lightweight, more efficient and more convenient than system threads. + +goroutines and concurrency are built into the core design of Go. They're similar to threads but work differently. Go also gives you full support to sharing memory in your goroutines. One goroutine usually uses 4~5 KB of stack memory. Therefore, it's not hard to run thousands of goroutines on a single computer. A goroutine is more lightweight, more efficient and more convenient than system threads. goroutines run on the thread manager at runtime in Go. We use the `go` keyword to create a new goroutine, which is a function at the underlying level ( ***main() is a goroutine*** ). ```Go go hello(a, b, c) -``` +``` Let's see an example. ```Go package main @@ -43,28 +43,28 @@ Output: hello world hello -``` +``` We see that it's very easy to use concurrency in Go by using the keyword `go`. In the above example, these two goroutines share some memory, but we would better off following the design recipe: Don't use shared data to communicate, use communication to share data. runtime.Gosched() means let the CPU execute other goroutines, and come back at some point. In Go 1.5,the runtime now sets the default number of threads to run simultaneously, defined by GOMAXPROCS, to the number of cores available on the CPU. -Before Go 1.5,The scheduler only uses one thread to run all goroutines, which means it only implements concurrency. If you want to use more CPU cores in order to take advantage of parallel processing, you have to call runtime.GOMAXPROCS(n) to set the number of cores you want to use. If `n<1`, it changes nothing. +Before Go 1.5,The scheduler only uses one thread to run all goroutines, which means it only implements concurrency. If you want to use more CPU cores in order to take advantage of parallel processing, you have to call runtime.GOMAXPROCS(n) to set the number of cores you want to use. If `n<1`, it changes nothing. ## channels -goroutines run in the same memory address space, so you have to maintain synchronization when you want to access shared memory. How do you communicate between different goroutines? Go uses a very good communication mechanism called `channel`. `channel` is like a two-way pipeline in Unix shells: use `channel` to send or receive data. The only data type that can be used in channels is the type `channel` and the keyword `chan`. Be aware that you have to use `make` to create a new `channel`. +goroutines run in the same memory address space, so you have to maintain synchronization when you want to access shared memory. How do you communicate between different goroutines? Go uses a very good communication mechanism called `channel`. A `channel` is like two-way pipeline in Unix shells: use `channel` to send or receive data. The only data type that can be used in channels is the type `channel` and the keyword `chan`. Be aware that you have to use `make` to create a new `channel`. ```Go ci := make(chan int) cs := make(chan string) cf := make(chan interface{}) -``` +``` channel uses the operator `<-` to send or receive data. ```Go ch <- v // send v to channel ch. v := <-ch // receive data from ch, and assign to v -``` +``` Let's see more examples. ```Go package main @@ -90,8 +90,8 @@ func main() { fmt.Println(x, y, x+y) } -``` -Sending and receiving data in channels blocks by default, so it's much easier to use synchronous goroutines. What I mean by block is that a goroutine will not continue when receiving data from an empty channel, i.e (`value := <-ch`), until other goroutines send data to this channel. On the other hand, the goroutine will not continue until the data it sends to a channel, i.e (`ch<-5`), is received. +``` +Sending and receiving data in channels blocks by default, so it's much easier to use synchronous goroutines. What I mean by block is that a goroutine will not continue when receiving data from an empty channel, i.e (`value := <-ch`), until other goroutines send data to this channel. On the other hand, the goroutine will not continue until the data it sends to a channel, i.e (`ch<-5`), is received. ## Buffered channels @@ -101,9 +101,9 @@ ch := make(chan type, n) n == 0 ! non-buffer(block) n > 0 ! buffer(non-block until n elements in the channel) -``` +``` You can try the following code on your computer and change some values. -```Go +```Go package main import "fmt" @@ -116,7 +116,7 @@ func main() { fmt.Println(<-c) } -``` +``` ## Range and Close We can use range to operate on buffer channels as in slice and map. @@ -144,12 +144,12 @@ func main() { } } -``` +``` `for i := range c` will not stop reading data from channel until the channel is closed. We use the keyword `close` to close the channel in above example. It's impossible to send or receive data on a closed channel; you can use `v, ok := <-ch` to test if a channel is closed. If `ok` returns false, it means the there is no data in that channel and it was closed. Remember to always close channels in producers and not in consumers, or it's very easy to get into panic status. -Another thing you need to remember is that channels are not like files. You don't have to close them frequently unless you are sure the channel is completely useless, or you want to exit range loops. +Another thing you need to remember is that channels are not like files. You don't have to close them frequently unless you are sure the channel is completely useless, or you want to exit range loops. ## Select @@ -186,7 +186,7 @@ func main() { fibonacci(c, quit) } -``` +``` `select` has a `default` case as well, just like `switch`. When all the channels are not ready for use, it executes the default case (it doesn't wait for the channel anymore). ```Go select { @@ -195,7 +195,7 @@ case i := <-c: default: // executes here when c is blocked } -``` +``` ## Timeout Sometimes a goroutine becomes blocked. How can we avoid this to prevent the whole program from blocking? It's simple, we can set a timeout in the select. @@ -218,7 +218,7 @@ func main() { <-o } -``` +``` ## Runtime goroutine The package `runtime` has some functions for dealing with goroutines. @@ -226,11 +226,11 @@ The package `runtime` has some functions for dealing with goroutines. - `runtime.Goexit()` Exits the current goroutine, but defered functions will be executed as usual. - + - `runtime.Gosched()` Lets the scheduler execute other goroutines and comes back at some point. - + - `runtime.NumCPU() int` Returns the number of CPU cores diff --git a/en/03.1.md b/en/03.1.md index 8af704e20..0166639eb 100644 --- a/en/03.1.md +++ b/en/03.1.md @@ -2,7 +2,7 @@ Every time you open your browsers, type some URLs and press enter, you will see beautiful web pages appear on your screen. But do you know what is happening behind these simple actions? -Normally, your browser is a client. After you type a URL, it takes the host part of the URL and sends it to a DNS server in order to get the IP address of the host. Then it connects to the IP address and asks to setup a TCP connection. The browser sends HTTP requests through the connection. The server handles them and replies with HTTP responses containing the content that make up the web page. Finally, the browser renders the body of the web page and disconnects from the server. +Normally, your browser is a client. After you type a URL, it takes the host part of the URL and sends it to a Domain Name Server (DNS) in order to get the IP address of the host. Then it connects to the IP address and asks to setup a TCP connection. The browser sends HTTP requests through the connection. The server handles them and replies with HTTP responses containing the content that make up the web page. Finally, the browser renders the body of the web page and disconnects from the server. ![](images/3.1.web2.png?raw=true) @@ -28,13 +28,13 @@ The full name of a URL is Uniform Resource Locator. It's for describing resource scheme://host[:port#]/path/.../[?query-string][#anchor] scheme assign underlying protocol (such as HTTP, HTTPS, FTP) host IP or domain name of HTTP server - port# default port is 80, and it can be omitted in this case. + port# default port is 80, and it can be omitted in this case. If you want to use other ports, you must specify which port. For example, http://www.cnblogs.com:8080/ path resources path query-string data are sent to server anchor anchor - + DNS is an abbreviation of Domain Name System. It's the naming system for computer network services, and it converts domain names to actual IP addresses, just like a translator. ![](images/3.1.dns_hierachy.png?raw=true) @@ -47,7 +47,7 @@ To understand more about its working principle, let's see the detailed DNS resol 2. If no mapping relationships exist in the hosts' files, the operating system will check if any cache exists in the DNS. If so, then the domain name resolution is complete. 3. If no mapping relationships exist in both the host and DNS cache, the operating system finds the first DNS resolution server in your TCP/IP settings, which is likely your local DNS server. When the local DNS server receives the query, if the domain name that you want to query is contained within the local configuration of its regional resources, it returns the results to the client. This DNS resolution is authoritative. 4. If the local DNS server doesn't contain the domain name but a mapping relationship exists in the cache, the local DNS server gives back this result to the client. This DNS resolution is not authoritative. -5. If the local DNS server cannot resolve this domain name either by configuration of regional resources or cache, it will proceed to the next step, which depends on the local DNS server's settings. +5. If the local DNS server cannot resolve this domain name either by configuration of regional resources or cache, it will proceed to the next step, which depends on the local DNS server's settings. -If the local DNS server doesn't enable forwarding, it routes the request to the root DNS server, then returns the IP address of a top level DNS server which may know the domain name, `.com` in this case. If the first top level DNS server doesn't recognize the domain name, it again reroutes the request to the next top level DNS server until it reaches one that recognizes the domain name. Then the top level DNS server asks this next level DNS server for the IP address corresponding to `www.qq.com`. -If the local DNS server has forwarding enabled, it sends the request to an upper level DNS server. If the upper level DNS server also doesn't recognize the domain name, then the request keeps getting rerouted to higher levels until it finally reaches a DNS server which recognizes the domain name. @@ -65,7 +65,7 @@ Now we know clients get IP addresses in the end, so the browsers are communicati The HTTP protocol is a core part of web services. It's important to know what the HTTP protocol is before you understand how the web works. -HTTP is the protocol that is used to facilitate communication between browsers and web servers. It is based on the TCP protocol and usually uses port 80 on the side of the web server. It is a protocol that utilizes the request-response model -clients send requests and servers respond. According to the HTTP protocol, clients always setup new connections and send HTTP requests to servers. Servers are not able to connect to clients proactively, or establish callback connections. The connection between a client and a server can be closed by either side. For example, you can cancel your download request and HTTP connection and your browser will disconnect from the server before you finish downloading. +HTTP is the protocol that is used to facilitate communication between browser and web server. It is based on the TCP protocol and usually uses port 80 on the side of the web server. It is a protocol that utilizes the request-response model -clients send requests and servers respond. According to the HTTP protocol, clients always setup new connections and send HTTP requests to servers. Servers are not able to connect to clients proactively, or establish callback connections. The connection between a client and a server can be closed by either side. For example, you can cancel your download request and HTTP connection and your browser will disconnect from the server before you finish downloading. The HTTP protocol is stateless, which means the server has no idea about the relationship between the two connections even though they are both from same client. To solve this problem, web applications use cookies to maintain the state of connections. @@ -73,7 +73,7 @@ Because the HTTP protocol is based on the TCP protocol, all TCP attacks will aff ### HTTP request package (browser information) -Request packages all have three parts: request line, request header, and body. There is one blank line between header and body. +Request packages all have three parts: request line, request header, and body. There is one blank line between header and body. GET /domains/example/ HTTP/1.1 // request line: request method, URL, protocol and its version Host:www.iana.org // domain name @@ -107,7 +107,7 @@ Let's see what information is contained in the response packages. Date:Date: Tue, 30 Oct 2012 04:14:25 GMT // responded time Content-Type: text/html // responded data type Transfer-Encoding: chunked // it means data were sent in fragments - Connection: keep-alive // keep connection + Connection: keep-alive // keep connection Content-Length: 90 // length of body // blank line