[PR #6015] [MERGED] [relay] evict foreign client cache on disconnect #27330

Closed
opened 2026-08-05 07:08:34 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/6015
Author: @pappz
Created: 4/28/2026
Status: Merged
Merged: 4/28/2026
Merged by: @pappz

Base: mainHead: fix/relay-foreign-disconnect-evict


📝 Commits (2)

  • 3dcf559 [relay] evict foreign client cache on disconnect
  • 10a4ed1 [client] treat empty NB_HOME_RELAY_SERVERS as unset

📊 Changes

6 files changed (+111 additions, -21 deletions)

View changed files

📝 client/internal/connect.go (+4 -0)
📝 client/internal/engine.go (+6 -1)
📝 client/internal/peer/env.go (+27 -1)
📝 shared/relay/client/guard.go (+18 -12)
📝 shared/relay/client/manager.go (+35 -5)
📝 shared/relay/client/manager_test.go (+21 -2)

📄 Description

When a foreign relay's TCP connection drops, the manager's onServerDisconnected handler only triggered reconnect logic for the home server; the disconnected foreign entry stayed in the relayClients cache. Subsequent OpenConn calls reused the closed client until the 60-second cleanup tick evicted it, breaking peer connectivity through that relay for up to a minute.

Evict the foreign entry from the cache on disconnect so the next OpenConn dials a fresh client.

Also:

  • Make the reconnect backoff cap configurable via WithMaxBackoffInterval ManagerOption; the previous hard-coded 60s constant forced TestAutoReconnect to sleep ~61s. Test now polls Ready() and finishes in ~2s.
  • Add NB_HOME_RELAY_SERVERS env var that overrides the relay URL list received from management, so a peer can be pinned to a specific home relay (used by the netbird-conn-lab Edge 4 reproducer).

Describe your changes

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)

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

    • Environment variable support to override relay server URLs at runtime (with informational logging when an override is used).
  • Improvements

    • Configurable reconnect backoff interval per relay manager instance.
    • Foreign relay disconnects now evict stale cached clients to ensure fresh reconnections.
  • Tests

    • Improved test reliability by using readiness-based connection polling.

🔄 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/6015 **Author:** [@pappz](https://github.com/pappz) **Created:** 4/28/2026 **Status:** ✅ Merged **Merged:** 4/28/2026 **Merged by:** [@pappz](https://github.com/pappz) **Base:** `main` ← **Head:** `fix/relay-foreign-disconnect-evict` --- ### 📝 Commits (2) - [`3dcf559`](https://github.com/netbirdio/netbird/commit/3dcf559272c67170c2ae389ff51ebc407b507c94) [relay] evict foreign client cache on disconnect - [`10a4ed1`](https://github.com/netbirdio/netbird/commit/10a4ed175be2264bf5de4f40fd8ac87b61ea5c7c) [client] treat empty NB_HOME_RELAY_SERVERS as unset ### 📊 Changes **6 files changed** (+111 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `client/internal/connect.go` (+4 -0) 📝 `client/internal/engine.go` (+6 -1) 📝 `client/internal/peer/env.go` (+27 -1) 📝 `shared/relay/client/guard.go` (+18 -12) 📝 `shared/relay/client/manager.go` (+35 -5) 📝 `shared/relay/client/manager_test.go` (+21 -2) </details> ### 📄 Description When a foreign relay's TCP connection drops, the manager's onServerDisconnected handler only triggered reconnect logic for the home server; the disconnected foreign entry stayed in the relayClients cache. Subsequent OpenConn calls reused the closed client until the 60-second cleanup tick evicted it, breaking peer connectivity through that relay for up to a minute. Evict the foreign entry from the cache on disconnect so the next OpenConn dials a fresh client. Also: - Make the reconnect backoff cap configurable via WithMaxBackoffInterval ManagerOption; the previous hard-coded 60s constant forced TestAutoReconnect to sleep ~61s. Test now polls Ready() and finishes in ~2s. - Add NB_HOME_RELAY_SERVERS env var that overrides the relay URL list received from management, so a peer can be pinned to a specific home relay (used by the netbird-conn-lab Edge 4 reproducer). ## Describe your changes ## Issue ticket number and link ## Stack <!-- branch-stack --> ### Checklist - [x] 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) > 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** * Environment variable support to override relay server URLs at runtime (with informational logging when an override is used). * **Improvements** * Configurable reconnect backoff interval per relay manager instance. * Foreign relay disconnects now evict stale cached clients to ensure fresh reconnections. * **Tests** * Improved test reliability by using readiness-based connection polling. <!-- 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 07:08:34 -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#27330