Commit ec8044c7 authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub
Browse files

Merge pull request #69 from ipfs/fix/nat-cache-sync

fix minor race condition in nat detection code
parents 5100dcec eca66a88
......@@ -164,6 +164,7 @@ type mapping struct {
cached ma.Multiaddr
cacheTime time.Time
cacheLk sync.Mutex
}
func (m *mapping) NAT() *NAT {
......@@ -203,8 +204,12 @@ func (m *mapping) InternalAddr() ma.Multiaddr {
}
func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
if time.Now().Sub(m.cacheTime) < CacheTime {
return m.cached, nil
m.cacheLk.Lock()
ctime := m.cacheTime
cval := m.cached
m.cacheLk.Unlock()
if time.Now().Sub(ctime) < CacheTime {
return cval, nil
}
if m.ExternalPort() == 0 { // dont even try right now.
......@@ -234,8 +239,10 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
maddr2 := ipmaddr.Encapsulate(tcp)
m.cacheLk.Lock()
m.cached = maddr2
m.cacheTime = time.Now()
m.cacheLk.Unlock()
return maddr2, nil
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment