[PR #6631] [client] Race home and foreign relay on connection setup #28647

Open
opened 2026-08-05 08:06:41 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/6631
Author: @pappz
Created: 7/1/2026
Status: 🔄 Open

Base: mainHead: refactor/relay-foreign-cache


📝 Commits (10+)

  • 7d8e200 [relay] Extract foreign relay client cache into a dedicated type
  • 50a29c0 [relay] Race home and foreign relay for peer connection setup
  • 6774a43 [relay] Rename ConnRacer to FallbackOpener and align file names with types
  • 49c0aeb [relay] Update relay client tests for the new OpenConn signature
  • 3b1beb3 [relay] Return raceOutcome struct from handleResult to avoid nilnil
  • a737504 Fix timeout
  • 79b51a7 [relay] Release home relay lock before the connection race
  • cf101c4 [relay] Count winning attempt before draining losers
  • d663da8 Merge branch 'main' into refactor/relay-foreign-cache
  • 85f61d0 [relay] Unit test ForeignRelaysStore and FallbackOpener

📊 Changes

10 files changed (+747 additions, -255 deletions)

View changed files

📝 client/internal/peer/worker_relay.go (+7 -19)
shared/relay/client/fallback_opener.go (+165 -0)
shared/relay/client/fallback_opener_test.go (+175 -0)
shared/relay/client/foreign_relays_store.go (+155 -0)
shared/relay/client/foreign_relays_store_test.go (+184 -0)
📝 shared/relay/client/manager.go (+21 -189)
📝 shared/relay/client/manager_cleanup_test.go (+11 -21)
📝 shared/relay/client/manager_relaystates_test.go (+19 -14)
📝 shared/relay/client/manager_serverip_test.go (+3 -4)
📝 shared/relay/client/manager_test.go (+7 -8)

📄 Description

Describe your changes

Open peer relay connections by racing the home relay and the remote peer's
relay in parallel, using whichever connects first (falls back to the other
after a delay). Foreign relay clients are cached in a dedicated
ForeignRelaysStore (singleflight dedup + refcount) so the cache mutex is
never held during a network connect.

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • This change does not modify the public API, gRPC protocols, functionality behavior, CLI / service flags, or introduce a new feature — OR I have discussed it with the NetBird team beforehand (link the issue / Slack thread in the description). See CONTRIBUTING.md.

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

Summary by CodeRabbit

  • New Features
    • Added foreign-relay connection caching to improve reuse and lifecycle management.
    • Introduced a timed fallback mechanism that can race an alternate relay path when the preferred route struggles.
  • Bug Fixes
    • Improved relay target selection based on the remote offer and adjusted close-listener tracking for more reliable cleanup.
    • Strengthened behavior when relay server IP details are missing, including recovery once available.
  • Refactor
    • Unified relay dialing around a structured relay server target and a single manager flow.
  • Tests
    • Updated relay manager tests for the revised connection invocation format.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/6631 **Author:** [@pappz](https://github.com/pappz) **Created:** 7/1/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `refactor/relay-foreign-cache` --- ### 📝 Commits (10+) - [`7d8e200`](https://github.com/netbirdio/netbird/commit/7d8e20030b2e068b9585fdb65c6b4c90931719ed) [relay] Extract foreign relay client cache into a dedicated type - [`50a29c0`](https://github.com/netbirdio/netbird/commit/50a29c07cec6a96846221cc57a9815a8918f1366) [relay] Race home and foreign relay for peer connection setup - [`6774a43`](https://github.com/netbirdio/netbird/commit/6774a43eae96df27a676f41aaf56d9ae5b6cd32e) [relay] Rename ConnRacer to FallbackOpener and align file names with types - [`49c0aeb`](https://github.com/netbirdio/netbird/commit/49c0aeb6ce2f9cd91f9cf30402f5955ba6677865) [relay] Update relay client tests for the new OpenConn signature - [`3b1beb3`](https://github.com/netbirdio/netbird/commit/3b1beb34978da8295be30a2479a0a1475264a9d3) [relay] Return raceOutcome struct from handleResult to avoid nilnil - [`a737504`](https://github.com/netbirdio/netbird/commit/a737504ec939dd1537e9e6e8f5a966d20683b73e) Fix timeout - [`79b51a7`](https://github.com/netbirdio/netbird/commit/79b51a79e48953068094f59d1a37e4f316fe8f80) [relay] Release home relay lock before the connection race - [`cf101c4`](https://github.com/netbirdio/netbird/commit/cf101c44b488e9b26b26448f8800d28beed9ca3f) [relay] Count winning attempt before draining losers - [`d663da8`](https://github.com/netbirdio/netbird/commit/d663da8f823bf3afde4060a757d5aba32929bb3f) Merge branch 'main' into refactor/relay-foreign-cache - [`85f61d0`](https://github.com/netbirdio/netbird/commit/85f61d0c5701783c84531b49e8192042888b3ed9) [relay] Unit test ForeignRelaysStore and FallbackOpener ### 📊 Changes **10 files changed** (+747 additions, -255 deletions) <details> <summary>View changed files</summary> 📝 `client/internal/peer/worker_relay.go` (+7 -19) ➕ `shared/relay/client/fallback_opener.go` (+165 -0) ➕ `shared/relay/client/fallback_opener_test.go` (+175 -0) ➕ `shared/relay/client/foreign_relays_store.go` (+155 -0) ➕ `shared/relay/client/foreign_relays_store_test.go` (+184 -0) 📝 `shared/relay/client/manager.go` (+21 -189) 📝 `shared/relay/client/manager_cleanup_test.go` (+11 -21) 📝 `shared/relay/client/manager_relaystates_test.go` (+19 -14) 📝 `shared/relay/client/manager_serverip_test.go` (+3 -4) 📝 `shared/relay/client/manager_test.go` (+7 -8) </details> ### 📄 Description ## Describe your changes Open peer relay connections by racing the home relay and the remote peer's relay in parallel, using whichever connects first (falls back to the other after a delay). Foreign relay clients are cached in a dedicated ForeignRelaysStore (singleflight dedup + refcount) so the cache mutex is never held during a network connect. ## Issue ticket number and link ## Stack <!-- branch-stack --> ### Checklist - [ ] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [x] It is a refactor - [ ] Created tests that fail without the change (if possible) - [ ] This change does **not** modify the public API, gRPC protocols, functionality behavior, CLI / service flags, or introduce a new feature — **OR** I have discussed it with the NetBird team beforehand (link the issue / Slack thread in the description). See [CONTRIBUTING.md](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTING.md#discuss-changes-with-the-netbird-team-first). > By submitting this pull request, you confirm that you have read and agree to the terms of the [Contributor License Agreement](https://github.com/netbirdio/netbird/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT.md). ## Documentation Select exactly one: - [ ] I added/updated documentation for this change - [x] Documentation is **not needed** for this change (explain why) ### Docs PR URL (required if "docs added" is checked) Paste the PR link from https://github.com/netbirdio/docs here: https://github.com/netbirdio/docs/pull/__ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added foreign-relay connection caching to improve reuse and lifecycle management. * Introduced a timed fallback mechanism that can race an alternate relay path when the preferred route struggles. * **Bug Fixes** * Improved relay target selection based on the remote offer and adjusted close-listener tracking for more reliable cleanup. * Strengthened behavior when relay server IP details are missing, including recovery once available. * **Refactor** * Unified relay dialing around a structured relay server target and a single manager flow. * **Tests** * Updated relay manager tests for the revised connection invocation format. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 08:06:41 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#28647