@cryptotaxi247 / kubo / commits / 1f415714f

WIP: quick fix to the NAT spam too many open fd's bug

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Sep 8, 2015 at 18:52 UTC 1f415714f4d23d3ea72d47fd8bb269292d53f922
1 file changed +19
p2p/nat/nat.go
+19
@@ -29,12 +29,17 @@ var log = eventlog.Logger("nat")
29 // Port mappings are renewed every (MappingDuration / 3)
30 const MappingDuration = time.Second * 60
31
32 +// CacheTime is the time a mapping will cache an external address for
33 +const CacheTime = time.Second * 15
34 +
35 // DiscoverNAT looks for a NAT device in the network and
36 // returns an object that can manage port mappings.
37 func DiscoverNAT() *NAT {
38 + log.Error("DISCOVER NAT")
39 nat, err := nat.DiscoverGateway()
40 if err != nil {
41 log.Debug("DiscoverGateway error:", err)
42 + log.Error("DISCOVER GATEWAY ERROR: ", err)
43 return nil
44 }
45 addr, err := nat.GetDeviceAddress()
@@ -43,6 +48,7 @@ func DiscoverNAT() *NAT {
48 } else {
49 log.Debug("DiscoverGateway address:", addr)
50 }
51 + log.Error("NEW NAT!")
52 return newNAT(nat)
53 }
54
@@ -159,6 +165,9 @@ type mapping struct {
165 extport int
166 intaddr ma.Multiaddr
167 proc goprocess.Process
168 +
169 + cached ma.Multiaddr
170 + cacheTime time.Time
171 }
172
173 func (m *mapping) NAT() *NAT {
@@ -198,6 +207,10 @@ func (m *mapping) InternalAddr() ma.Multiaddr {
207 }
208
209 func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
210 + if time.Now().Sub(m.cacheTime) < CacheTime {
211 + return m.cached, nil
212 + }
213 +
214 if m.ExternalPort() == 0 { // dont even try right now.
215 return nil, ErrNoMapping
216 }
@@ -224,6 +237,9 @@ func (m *mapping) ExternalAddr() (ma.Multiaddr, error) {
237 }
238
239 maddr2 := ipmaddr.Encapsulate(tcp)
240 +
241 + m.cached = maddr2
242 + m.cacheTime = time.Now()
243 return maddr2, nil
244 }
245
@@ -266,6 +282,7 @@ func (nat *NAT) rmMapping(m *mapping) {
282 // Clients should not store the mapped results, but rather always
283 // poll our object for the latest mappings.
284 func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
285 + log.Error("NEW MAPPING!")
286 if nat == nil {
287 return nil, fmt.Errorf("no nat available")
288 }
@@ -313,6 +330,7 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
330 }
331
332 func (nat *NAT) establishMapping(m *mapping) {
333 + log.Error("establishMapping!")
334 oldport := m.ExternalPort()
335 log.Debugf("Attempting port map: %s/%d", m.Protocol(), m.InternalPort())
336 newport, err := nat.nat.AddPortMapping(m.Protocol(), m.InternalPort(), "http", MappingDuration)
@@ -413,6 +431,7 @@ func (nat *NAT) MappedAddrs() map[ma.Multiaddr]ma.Multiaddr {
431 // This set of mappings _may not_ be correct, as NAT devices are finicky.
432 // Consider this with _best effort_ semantics.
433 func (nat *NAT) ExternalAddrs() []ma.Multiaddr {
434 + log.Error("EXTERNAL ADDRS")
435 mappings := nat.Mappings()
436 addrs := make([]ma.Multiaddr, 0, len(mappings))
437 for _, m := range mappings {