Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix minor race condition in nat detection code #69

Merged
merged 1 commit into from
Jun 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type mapping struct {

cached ma.Multiaddr
cacheTime time.Time
cacheLk sync.Mutex
}

func (m *mapping) NAT() *NAT {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down