Skip to content

Commit 89cc47f

Browse files
authored
Merge pull request astaxie#1156 from fredrb/patch-1
update 03.3md with proper code block syntax
2 parents e0fa4db + 132f52e commit 89cc47f

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

en/03.3.md

+27-25
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,38 @@ In the previous section we saw that Go uses `ListenAndServe` to handle these ste
3434

3535
Let's take a look at the `http` package's source code.
3636

37-
//Build version go1.1.2.
38-
func (srv *Server) Serve(l net.Listener) error {
39-
defer l.Close()
40-
var tempDelay time.Duration // how long to sleep on accept failure
41-
for {
42-
rw, e := l.Accept()
43-
if e != nil {
44-
if ne, ok := e.(net.Error); ok && ne.Temporary() {
45-
if tempDelay == 0 {
46-
tempDelay = 5 * time.Millisecond
47-
} else {
48-
tempDelay *= 2
49-
}
50-
if max := 1 * time.Second; tempDelay > max {
51-
tempDelay = max
52-
}
53-
log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay)
54-
time.Sleep(tempDelay)
55-
continue
37+
```go
38+
//Build version go1.1.2.
39+
func (srv *Server) Serve(l net.Listener) error {
40+
defer l.Close()
41+
var tempDelay time.Duration // how long to sleep on accept failure
42+
for {
43+
rw, e := l.Accept()
44+
if e != nil {
45+
if ne, ok := e.(net.Error); ok && ne.Temporary() {
46+
if tempDelay == 0 {
47+
tempDelay = 5 * time.Millisecond
48+
} else {
49+
tempDelay *= 2
5650
}
57-
return e
58-
}
59-
tempDelay = 0
60-
c, err := srv.newConn(rw)
61-
if err != nil {
51+
if max := 1 * time.Second; tempDelay > max {
52+
tempDelay = max
53+
}
54+
log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay)
55+
time.Sleep(tempDelay)
6256
continue
6357
}
64-
go c.serve()
58+
return e
59+
}
60+
tempDelay = 0
61+
c, err := srv.newConn(rw)
62+
if err != nil {
63+
continue
6564
}
65+
go c.serve()
6666
}
67+
}
68+
```
6769

6870

6971
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.

0 commit comments

Comments
 (0)