Commit a37c480a authored by Jakub Sztandera's avatar Jakub Sztandera

feat: cache full InterfaceListenAddresses

License: MIT
Signed-off-by: default avatarJakub Sztandera <kubuxu@protonmail.ch>
parent 9f9dd240
...@@ -60,7 +60,7 @@ type Swarm struct { ...@@ -60,7 +60,7 @@ type Swarm struct {
listeners struct { listeners struct {
sync.RWMutex sync.RWMutex
ifaceAddresses []ma.Multiaddr ifaceListenAddres []ma.Multiaddr
cacheEOL time.Time cacheEOL time.Time
m map[transport.Listener]struct{} m map[transport.Listener]struct{}
......
...@@ -29,15 +29,14 @@ const ifaceAddrsCacheDuration = 1 * time.Minute ...@@ -29,15 +29,14 @@ const ifaceAddrsCacheDuration = 1 * time.Minute
// use the known local interfaces. // use the known local interfaces.
func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) { func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
s.listeners.RLock() // RLock start s.listeners.RLock() // RLock start
listenAddrs := s.listenAddressesNoLock()
ifaceAddrs := s.listeners.ifaceAddresses ifaceListenAddres := s.listeners.ifaceListenAddres
isEOL := time.Now().After(s.listeners.cacheEOL) isEOL := time.Now().After(s.listeners.cacheEOL)
s.listeners.RUnlock() // RLock end s.listeners.RUnlock() // RLock end
if listenAddrs != nil && !isEOL { if ifaceListenAddres != nil && !isEOL {
// Cache is valid // Cache is valid
return addrutil.ResolveUnspecifiedAddresses(listenAddrs, ifaceAddrs) return ifaceListenAddres, nil
} }
// Cache is not valid // Cache is not valid
...@@ -45,24 +44,25 @@ func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) { ...@@ -45,24 +44,25 @@ func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
s.listeners.Lock() // Lock start s.listeners.Lock() // Lock start
listenAddrs = s.listenAddressesNoLock() ifaceListenAddres = s.listeners.ifaceListenAddres
ifaceAddrs = s.listeners.ifaceAddresses
isEOL = time.Now().After(s.listeners.cacheEOL) isEOL = time.Now().After(s.listeners.cacheEOL)
if listenAddrs == nil || isEOL { if ifaceListenAddres == nil || isEOL {
// Cache is still invalid // Cache is still invalid
var err error var err error
ifaceAddrs, err = addrutil.InterfaceAddresses() ifaceListenAddres, err = addrutil.ResolveUnspecifiedAddresses(
s.listenAddressesNoLock(), nil)
if err != nil { if err != nil {
s.listeners.Unlock() // Lock early exit s.listeners.Unlock() // Lock early exit
return nil, err return nil, err
} }
s.listeners.ifaceAddresses = ifaceAddrs s.listeners.ifaceListenAddres = ifaceListenAddres
s.listeners.cacheEOL = time.Now().Add(ifaceAddrsCacheDuration) s.listeners.cacheEOL = time.Now().Add(ifaceAddrsCacheDuration)
} }
s.listeners.Unlock() // Lock end s.listeners.Unlock() // Lock end
return addrutil.ResolveUnspecifiedAddresses(listenAddrs, ifaceAddrs) return ifaceListenAddres, 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