Skip to content

Conversation

@icellan
Copy link
Contributor

@icellan icellan commented Nov 4, 2025

Summary

Adds configurable DHT mode (client/server) and cleanup interval to optimize CPU usage in DHT provider record management.

Changes

Configuration Options

  • DHTMode: Specify "client" or "server" mode (default: "server")
    • Server mode: Full DHT participation, advertises on DHT, stores provider records
    • Client mode: Query-only, no provider storage, no cleanup overhead
  • DHTCleanupInterval: Configure how often provider records are cleaned up (default: libp2p default of 1 hour)

Implementation Details

  1. Added DHTMode and DHTCleanupInterval fields to Config struct
  2. Modified setupDHT() to support mode selection and custom ProviderManager
  3. Added in-memory datastore for custom ProviderManager to avoid nil pointer issues
  4. Added P2PClient type alias for backward compatibility

Test Coverage

  • 11 new tests covering all configuration scenarios
  • Mode selection tests (default, server, client)
  • Cleanup interval tests (default, custom, ignored in client mode)
  • Configuration combination tests
  • Backward compatibility tests
  • Benchmarks for both modes

All tests pass ✅

Performance Impact

CPU Usage Reduction

With custom cleanup interval (e.g., 24 hours):

  • Before: ~50% CPU on DHT cleanup every hour
  • After: ~2% CPU on DHT cleanup every 24 hours
  • Reduction: 24x less CPU time spent on cleanup

Use Cases

  • Server mode (default): Production nodes that need peer discovery
  • Client mode: Resource-constrained nodes, private nodes
  • Custom cleanup intervals: High-traffic nodes that want to minimize overhead

Backward Compatibility

  • Default behavior unchanged (server mode, libp2p default cleanup)
  • Added P2PClient type alias to maintain compatibility with existing code
  • All existing tests continue to pass

Related Issues

Addresses high CPU usage in DHT ProviderManager cleanup as identified in production profiling.

@icellan icellan requested a review from mrz1836 as a code owner November 4, 2025 12:56
@github-actions github-actions bot added size/L Large change (201–500 lines) chore Simple dependency updates or version bumps feature Any new significant addition refactor Any significant refactoring update General updates labels Nov 4, 2025
@icellan icellan requested review from oskarszoon and removed request for oskarszoon November 4, 2025 12:58
mrz1836 and others added 2 commits November 4, 2025 08:01
- Extract common test setup into createTestClient helper function
- Consolidate overlapping tests (TestDHTModeDefault, TestDHTModeServer, TestDHTModeClient) into TestDHTModeSelection
- Merge TestDHTModeServerWithVariousCleanupIntervals into TestDHTCleanupIntervalConfiguration
- Reduce test count from 11 to 5 while maintaining full coverage
- Use t.Helper() and t.Cleanup() for better test organization
- Add descriptive assertion messages for clearer failures
- Improve benchmark error handling

Changes:
- Lines of code reduced: 416 -> 312 (25% reduction)
- Tests consolidated: 11 -> 5 (same coverage, better organization)
- Code duplication eliminated: 9 repeated log setups, 11 repeated client creations now use 1 helper
- All tests still pass with identical coverage
@icellan icellan force-pushed the feature/dht-mode-cleanup branch from 2755235 to bb01e1a Compare November 4, 2025 13:01
Copy link
Contributor

@mrz1836 mrz1836 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@galt-tr
Copy link
Collaborator

galt-tr commented Nov 4, 2025

⚠️ Auto-merge failed

Could not enable auto-merge: Command failed: gh pr merge --auto --squash --delete-branch "#8"

This might be due to:

  • Branch protection rules
  • Missing permissions
  • Repository settings

Added Go modules configuration for the /example/ directory to ensure
weekly updates for direct dependencies, including security patterns
and commit message prefixing.
Updated dependencies to their latest versions for improved stability and performance.
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 4, 2025

@mrz1836 mrz1836 merged commit e2a129e into main Nov 4, 2025
45 checks passed
@github-actions github-actions bot deleted the feature/dht-mode-cleanup branch November 4, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Simple dependency updates or version bumps feature Any new significant addition refactor Any significant refactoring size/L Large change (201–500 lines) update General updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants