mirror of
https://github.com/golang/go.git
synced 2025-11-09 21:21:03 +00:00
net: change SetTimeout to SetDeadline
Previously, a timeout (in int64 nanoseconds) applied to a granularity even smaller than one operation: a 100 byte read with a 1 second timeout could take 100 seconds, if the bytes all arrived on the network 1 second apart. This was confusing. Rather than making the timeout granularity be per-Read/Write, this CL makes callers set an absolute deadline (in time.Time) after which operations will fail. This makes it possible to set deadlines at higher levels, without knowing exactly how many read/write operations will happen in e.g. reading an HTTP request. Fixes #2723 R=r, rsc, dave CC=golang-dev https://golang.org/cl/5555048
This commit is contained in:
parent
5e77b009d0
commit
b71883e9b0
23 changed files with 250 additions and 231 deletions
|
|
@ -45,7 +45,11 @@ func exchange(cfg *dnsConfig, c Conn, name string, qtype uint16) (*dnsMsg, error
|
|||
return nil, err
|
||||
}
|
||||
|
||||
c.SetReadTimeout(int64(cfg.timeout) * 1e9) // nanoseconds
|
||||
if cfg.timeout == 0 {
|
||||
c.SetReadDeadline(time.Time{})
|
||||
} else {
|
||||
c.SetReadDeadline(time.Now().Add(time.Duration(cfg.timeout) * time.Second))
|
||||
}
|
||||
|
||||
buf := make([]byte, 2000) // More than enough.
|
||||
n, err = c.Read(buf)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue