Skip to content

Conversation

@hurricanehrndz
Copy link
Contributor

@hurricanehrndz hurricanehrndz commented Oct 24, 2025

Describe your changes

Host config should only be applied when it has changed

Issue ticket number and link

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

  • Bug Fixes
    • Improved DNS configuration handling to detect per-instance config changes and skip reapplying identical settings, reducing redundant updates and work.
    • Ensures the initial configuration is always applied.
    • Adds a safe fallback so config updates still proceed if change-detection encounters an error.

Copilot AI review requested due to automatic review settings October 24, 2025 18:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes DNS host configuration updates by applying changes only when the configuration has actually changed, preventing unnecessary updates when the config remains identical.

Key Changes:

  • Added hash-based change detection to compare current and new DNS host configurations
  • Introduced currentConfigHash field to track the hash of the applied configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

if s.currentConfigHash == hash {
log.Infof("not applying host config as there are no changes")
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

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

[nitpick] Changed 'Infof' to 'Debugf' for consistency with logging levels - configuration skip events are typically debug-level information rather than info-level.

Suggested change
log.Infof("not applying host config as there are no changes")
log.Debugf("not applying host config as there are no changes")

Copilot uses AI. Check for mistakes.
@hurricanehrndz hurricanehrndz force-pushed the apply_dns_hostconfig_on_change_only branch 2 times, most recently from a0ba525 to 7bd88f1 Compare November 7, 2025 12:30
@coderabbitai
Copy link

coderabbitai bot commented Nov 7, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds per-instance hash caching for merged host DNS configuration by introducing a currentConfigHash uint64 field in DefaultServer. The code computes a hash of the merged config (including extra domains) before applying changes, skips application if the hash matches, updates currentConfigHash only on successful hashing and application, and initializes the field to max uint64 so the first config is applied.

Changes

Cohort / File(s) Summary
DNS Server Configuration Hashing
client/internal/dns/server.go
Added currentConfigHash uint64 to DefaultServer. Initialize it to ^uint64(0) in newDefaultServer. Compute hash of merged host DNS config (including extra domains) before applying; compare with currentConfigHash to short-circuit redundant updates. Add error handling for hash computation and update currentConfigHash only on successful hash and apply.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant DefaultServer
    participant Hasher as Hash

    Caller->>DefaultServer: Apply host DNS config
    DefaultServer->>Hasher: Compute hash(merged config + extra domains)
    alt Hash computation fails
        Hasher-->>DefaultServer: Error
        DefaultServer->>DefaultServer: Proceed to apply (fail-safe)
        DefaultServer-->>Caller: Return (applied despite hash error)
    else Hash computed
        Hasher-->>DefaultServer: hashValue
        alt hashValue == currentConfigHash
            DefaultServer->>DefaultServer: Skip applying config
            DefaultServer-->>Caller: Return (no-op)
        else hashValue != currentConfigHash
            DefaultServer->>DefaultServer: Apply new config
            DefaultServer->>DefaultServer: Set currentConfigHash = hashValue
            DefaultServer-->>Caller: Return (updated)
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Inspect currentConfigHash initialization and usages in newDefaultServer and applyHostConfig.
  • Verify hash computation includes all relevant fields (merged config + extra domains) and that error handling correctly falls back to applying config.
  • Ensure currentConfigHash is only updated on successful hash computation and application.

Poem

🐰 I chased a hash through fields and frames,
I counted domains and called their names,
When new and old are found the same,
I skip the work, save a bit of flame,
A rabbit's thrift in DNS game 🌿🔍

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Apply DNS host config on change only' clearly and concisely describes the main change: DNS host configuration is now applied only when it has changed, matching the core purpose of the changeset.
Description check ✅ Passed The description follows the required template with all essential sections completed: change description provided, bug fix and feature enhancement marked, documentation marked as not needed with explanation. All required elements are present.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Initialize currentConfigHash to max uint64 to ensure first config application, implement fail-safe error handling for hash computation failures, and only update hash after successful config application to maintain consistency
@hurricanehrndz hurricanehrndz force-pushed the apply_dns_hostconfig_on_change_only branch from 7bd88f1 to 2d29138 Compare November 7, 2025 15:50
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 7, 2025

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
client/internal/dns/server.go (1)

591-613: Well-implemented hash-based change detection with proper fail-safe behavior.

The logic correctly:

  • Computes hash of the merged config (including extra domains)
  • Applies a fail-safe approach by proceeding with application if hashing fails
  • Skips application only when hashing succeeds and the hash matches
  • Updates currentConfigHash only when both hashing and application succeed

The fail-safe handling at lines 598-599 ensures that transient hash computation failures won't prevent necessary configuration updates, which is the right trade-off between optimization and reliability.

Minor: Comment clarity (optional refinement)
The comment at line 610 is accurate, but could be more explicit. Consider:

// Only update hash if both operations succeeded:
// - Hash computation succeeded (err == nil from line 591)
// - Config application succeeded (would have returned at line 607 otherwise)

This makes it clearer why checking err == nil alone is sufficient.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7bd88f1 and 2d29138.

📒 Files selected for processing (1)
  • client/internal/dns/server.go (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
client/internal/dns/server.go (2)
client/internal/dns/host.go (1)
  • applyDNSConfig (11-16)
client/internal/dns/host_darwin.go (1)
  • s (50-110)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: Client / Unit (amd64)
  • GitHub Check: Relay / Unit (386)
  • GitHub Check: Client / Unit (386)
  • GitHub Check: Relay / Unit (amd64, -race)
  • GitHub Check: Client (Docker) / Unit
  • GitHub Check: Management / Unit (amd64, sqlite)
  • GitHub Check: Management / Unit (amd64, mysql)
  • GitHub Check: Management / Unit (amd64, postgres)
  • GitHub Check: Linux
  • GitHub Check: Windows
  • GitHub Check: Darwin
  • GitHub Check: Client / Unit
  • GitHub Check: JS / Lint
  • GitHub Check: iOS / Build
  • GitHub Check: Android / Build
  • GitHub Check: release_ui
  • GitHub Check: release_ui_darwin
  • GitHub Check: Client / Unit
  • GitHub Check: release
  • GitHub Check: Client / Unit
🔇 Additional comments (2)
client/internal/dns/server.go (2)

83-83: LGTM: New field added to track config state.

The currentConfigHash field is appropriately placed and will enable efficient change detection.


211-211: Excellent initialization strategy.

Using ^uint64(0) (max uint64) as the sentinel value is a robust approach that ensures the first configuration is always applied while minimizing collision risk with actual hash values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant