Merge pull request #1156 from fredrb/patch-1
update 03.3md with proper code block syntax
This commit is contained in:
52
en/03.3.md
52
en/03.3.md
@@ -34,36 +34,38 @@ In the previous section we saw that Go uses `ListenAndServe` to handle these ste
|
|||||||
|
|
||||||
Let's take a look at the `http` package's source code.
|
Let's take a look at the `http` package's source code.
|
||||||
|
|
||||||
//Build version go1.1.2.
|
```go
|
||||||
func (srv *Server) Serve(l net.Listener) error {
|
//Build version go1.1.2.
|
||||||
defer l.Close()
|
func (srv *Server) Serve(l net.Listener) error {
|
||||||
var tempDelay time.Duration // how long to sleep on accept failure
|
defer l.Close()
|
||||||
for {
|
var tempDelay time.Duration // how long to sleep on accept failure
|
||||||
rw, e := l.Accept()
|
for {
|
||||||
if e != nil {
|
rw, e := l.Accept()
|
||||||
if ne, ok := e.(net.Error); ok && ne.Temporary() {
|
if e != nil {
|
||||||
if tempDelay == 0 {
|
if ne, ok := e.(net.Error); ok && ne.Temporary() {
|
||||||
tempDelay = 5 * time.Millisecond
|
if tempDelay == 0 {
|
||||||
} else {
|
tempDelay = 5 * time.Millisecond
|
||||||
tempDelay *= 2
|
} else {
|
||||||
}
|
tempDelay *= 2
|
||||||
if max := 1 * time.Second; tempDelay > max {
|
|
||||||
tempDelay = max
|
|
||||||
}
|
|
||||||
log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay)
|
|
||||||
time.Sleep(tempDelay)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
return e
|
if max := 1 * time.Second; tempDelay > max {
|
||||||
}
|
tempDelay = max
|
||||||
tempDelay = 0
|
}
|
||||||
c, err := srv.newConn(rw)
|
log.Printf("http: Accept error: %v; retrying in %v", e, tempDelay)
|
||||||
if err != nil {
|
time.Sleep(tempDelay)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go c.serve()
|
return e
|
||||||
}
|
}
|
||||||
|
tempDelay = 0
|
||||||
|
c, err := srv.newConn(rw)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go c.serve()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user