Skip to content

Commit

Permalink
internal/socket: update zoneCache on cache misses to cover appearing …
Browse files Browse the repository at this point in the history
…interfaces

Updates golang/go#28535

Change-Id: Id653b21b4d893cc8b6b9a74b129d1ce9b7e26a9f
Reviewed-on: https://go-review.googlesource.com/c/147739
Reviewed-by: Mikio Hara <mikioh.public.networking@gmail.com>
Run-TryBot: Mikio Hara <mikioh.public.networking@gmail.com>
  • Loading branch information
stapelberg authored and mikioh committed Nov 7, 2018
1 parent 10aee18 commit a544f70
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions internal/socket/sys_posix.go
Expand Up @@ -121,18 +121,21 @@ var zoneCache = ipv6ZoneCache{
toName: make(map[int]string),
}

func (zc *ipv6ZoneCache) update(ift []net.Interface) {
// update refreshes the network interface information if the cache was last
// updated more than 1 minute ago, or if force is set. It returns whether the
// cache was updated.
func (zc *ipv6ZoneCache) update(ift []net.Interface, force bool) (updated bool) {
zc.Lock()
defer zc.Unlock()
now := time.Now()
if zc.lastFetched.After(now.Add(-60 * time.Second)) {
return
if !force && zc.lastFetched.After(now.Add(-60*time.Second)) {
return false
}
zc.lastFetched = now
if len(ift) == 0 {
var err error
if ift, err = net.Interfaces(); err != nil {
return
return false
}
}
zc.toIndex = make(map[string]int, len(ift))
Expand All @@ -143,24 +146,41 @@ func (zc *ipv6ZoneCache) update(ift []net.Interface) {
zc.toName[ifi.Index] = ifi.Name
}
}
return true
}

func (zc *ipv6ZoneCache) name(zone int) string {
zoneCache.update(nil)
updated := zoneCache.update(nil, false)
zoneCache.RLock()
defer zoneCache.RUnlock()
name, ok := zoneCache.toName[zone]
zoneCache.RUnlock()
if !ok {
if !updated {
zoneCache.update(nil, true)
zoneCache.RLock()
name, ok = zoneCache.toName[zone]
zoneCache.RUnlock()
}
}
if !ok {
name = strconv.Itoa(zone)
}
return name
}

func (zc *ipv6ZoneCache) index(zone string) int {
zoneCache.update(nil)
updated := zoneCache.update(nil, false)
zoneCache.RLock()
defer zoneCache.RUnlock()
index, ok := zoneCache.toIndex[zone]
zoneCache.RUnlock()
if !ok {
if !updated {
zoneCache.update(nil, true)
zoneCache.RLock()
index, ok = zoneCache.toIndex[zone]
zoneCache.RUnlock()
}
}
if !ok {
index, _ = strconv.Atoi(zone)
}
Expand Down

0 comments on commit a544f70

Please sign in to comment.