Skip to content

Commit f34ea94

Browse files
authored
Config > DisableNAT (#9)
1 parent 6924eda commit f34ea94

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

client.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,19 @@ func createHost(_ context.Context, hostOpts []libp2p.Option, config Config, rela
186186
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.Port),
187187
fmt.Sprintf("/ip6/::/tcp/%d", config.Port),
188188
),
189-
libp2p.NATPortMap(),
190-
libp2p.EnableNATService(),
191-
libp2p.EnableHolePunching(),
189+
)
190+
191+
// Only enable NAT traversal features if not disabled
192+
// NAT features can cause data races in tests due to libp2p's NAT manager using non-thread-safe global state
193+
if !config.DisableNAT {
194+
hostOpts = append(hostOpts,
195+
libp2p.NATPortMap(),
196+
libp2p.EnableNATService(),
197+
libp2p.EnableHolePunching(),
198+
)
199+
}
200+
201+
hostOpts = append(hostOpts,
192202
libp2p.EnableRelay(),
193203
libp2p.EnableAutoRelayWithStaticRelays(relayPeers),
194204
)

config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@ type Config struct {
9797
// Recommended: 6-24 hours for production to reduce CPU overhead.
9898
// The cleanup frequency trades off between memory usage (stale records) and CPU usage.
9999
DHTCleanupInterval time.Duration
100+
101+
// DisableNAT disables NAT traversal features (UPnP/NAT-PMP port mapping, NAT service, hole punching).
102+
// Set to true in test environments where NAT traversal is not needed and can cause data races
103+
// due to libp2p's NAT manager using non-thread-safe global state.
104+
// Default: false (NAT features enabled)
105+
DisableNAT bool
100106
}

0 commit comments

Comments
 (0)