mirror of
https://github.com/golang/go
synced 2024-11-20 00:24:43 -07:00
net: check /etc/hosts for modifications every 5 seconds, not 5 minutes
But also cache the previous parsed form and don't reread if the size and modification time are both unchanged from before. On systems with stable /etc/hosts this should result in more stat calls but only a single parsing of /etc/hosts. On systems with variable /etc/hosts files (like some Docker systems) this should result in quicker adoption of changes. Fixes #13340. Change-Id: Iba93b204be73d6d903cd17c58038a4fcfd0952b9 Reviewed-on: https://go-review.googlesource.com/18258 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
fb394017f1
commit
b598a7fc5d
@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const cacheMaxAge = 5 * time.Minute
|
const cacheMaxAge = 5 * time.Second
|
||||||
|
|
||||||
func parseLiteralIP(addr string) string {
|
func parseLiteralIP(addr string) string {
|
||||||
var ip IP
|
var ip IP
|
||||||
@ -44,12 +44,23 @@ var hosts struct {
|
|||||||
|
|
||||||
expire time.Time
|
expire time.Time
|
||||||
path string
|
path string
|
||||||
|
mtime time.Time
|
||||||
|
size int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func readHosts() {
|
func readHosts() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
hp := testHookHostsPath
|
hp := testHookHostsPath
|
||||||
if len(hosts.byName) == 0 || now.After(hosts.expire) || hosts.path != hp {
|
|
||||||
|
if now.Before(hosts.expire) && hosts.path == hp && len(hosts.byName) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mtime, size, err := stat(hp)
|
||||||
|
if err == nil && hosts.path == hp && hosts.mtime.Equal(mtime) && hosts.size == size {
|
||||||
|
hosts.expire = now.Add(cacheMaxAge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
hs := make(map[string][]string)
|
hs := make(map[string][]string)
|
||||||
is := make(map[string][]string)
|
is := make(map[string][]string)
|
||||||
var file *file
|
var file *file
|
||||||
@ -83,9 +94,10 @@ func readHosts() {
|
|||||||
hosts.path = hp
|
hosts.path = hp
|
||||||
hosts.byName = hs
|
hosts.byName = hs
|
||||||
hosts.byAddr = is
|
hosts.byAddr = is
|
||||||
|
hosts.mtime = mtime
|
||||||
|
hosts.size = size
|
||||||
file.close()
|
file.close()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// lookupStaticHost looks up the addresses for the given host from /etc/hosts.
|
// lookupStaticHost looks up the addresses for the given host from /etc/hosts.
|
||||||
func lookupStaticHost(host string) []string {
|
func lookupStaticHost(host string) []string {
|
||||||
|
@ -10,6 +10,7 @@ package net
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
_ "unsafe" // For go:linkname
|
_ "unsafe" // For go:linkname
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -71,6 +72,14 @@ func open(name string) (*file, error) {
|
|||||||
return &file{fd, make([]byte, 0, os.Getpagesize()), false}, nil
|
return &file{fd, make([]byte, 0, os.Getpagesize()), false}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stat(name string) (mtime time.Time, size int64, err error) {
|
||||||
|
st, err := os.Stat(name)
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, 0, err
|
||||||
|
}
|
||||||
|
return st.ModTime(), st.Size(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// byteIndex is strings.IndexByte. It returns the index of the
|
// byteIndex is strings.IndexByte. It returns the index of the
|
||||||
// first instance of c in s, or -1 if c is not present in s.
|
// first instance of c in s, or -1 if c is not present in s.
|
||||||
// strings.IndexByte is implemented in runtime/asm_$GOARCH.s
|
// strings.IndexByte is implemented in runtime/asm_$GOARCH.s
|
||||||
|
Loading…
Reference in New Issue
Block a user