-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[management] Refactor expose feature: move business logic from gRPC to manager #5435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
006664d
Refactor expose feature: move business logic from gRPC to manager
mlsmaycon 36b7078
Improve expose tracking: add limits, expiring flag, and comprehensive…
mlsmaycon aff7ad7
fix comments
mlsmaycon 648a2c0
Handle expired expose session deletion errors more gracefully
mlsmaycon 3818cf6
Log warning instead of returning error when stopping non-existent exp…
mlsmaycon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 55 additions & 57 deletions
112
management/internals/modules/reverseproxy/interface_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
163 changes: 163 additions & 0 deletions
163
management/internals/modules/reverseproxy/manager/expose_tracker.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package manager | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/netbirdio/netbird/shared/management/status" | ||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const ( | ||
| exposeTTL = 90 * time.Second | ||
| exposeReapInterval = 30 * time.Second | ||
| maxExposesPerPeer = 10 | ||
| ) | ||
|
|
||
| type trackedExpose struct { | ||
| mu sync.Mutex | ||
| domain string | ||
| accountID string | ||
| peerID string | ||
| lastRenewed time.Time | ||
| expiring bool | ||
| } | ||
|
|
||
| type exposeTracker struct { | ||
| activeExposes sync.Map | ||
| exposeCreateMu sync.Mutex | ||
| manager *managerImpl | ||
| } | ||
|
|
||
| func exposeKey(peerID, domain string) string { | ||
| return peerID + ":" + domain | ||
| } | ||
|
|
||
| // TrackExposeIfAllowed atomically checks the per-peer limit and registers a new | ||
| // active expose session under the same lock. Returns (true, false) if the expose | ||
| // was already tracked (duplicate), (false, true) if tracking succeeded, and | ||
| // (false, false) if the peer has reached the limit. | ||
| func (t *exposeTracker) TrackExposeIfAllowed(peerID, domain, accountID string) (alreadyTracked, ok bool) { | ||
| t.exposeCreateMu.Lock() | ||
| defer t.exposeCreateMu.Unlock() | ||
|
|
||
| key := exposeKey(peerID, domain) | ||
| _, loaded := t.activeExposes.LoadOrStore(key, &trackedExpose{ | ||
| domain: domain, | ||
| accountID: accountID, | ||
| peerID: peerID, | ||
| lastRenewed: time.Now(), | ||
| }) | ||
| if loaded { | ||
| return true, false | ||
| } | ||
|
|
||
| if t.CountPeerExposes(peerID) > maxExposesPerPeer { | ||
| t.activeExposes.Delete(key) | ||
| return false, false | ||
| } | ||
|
|
||
| return false, true | ||
| } | ||
|
|
||
| // UntrackExpose removes an active expose session from tracking. | ||
| func (t *exposeTracker) UntrackExpose(peerID, domain string) { | ||
| t.activeExposes.Delete(exposeKey(peerID, domain)) | ||
| } | ||
|
|
||
| // CountPeerExposes returns the number of active expose sessions for a peer. | ||
| func (t *exposeTracker) CountPeerExposes(peerID string) int { | ||
| count := 0 | ||
| t.activeExposes.Range(func(_, val any) bool { | ||
| if expose := val.(*trackedExpose); expose.peerID == peerID { | ||
| count++ | ||
| } | ||
| return true | ||
| }) | ||
| return count | ||
| } | ||
|
|
||
| // MaxExposesPerPeer returns the maximum number of concurrent exposes allowed per peer. | ||
| func (t *exposeTracker) MaxExposesPerPeer() int { | ||
| return maxExposesPerPeer | ||
| } | ||
|
|
||
| // RenewTrackedExpose updates the in-memory lastRenewed timestamp for a tracked expose. | ||
| // Returns false if the expose is not tracked or is being reaped. | ||
| func (t *exposeTracker) RenewTrackedExpose(peerID, domain string) bool { | ||
| key := exposeKey(peerID, domain) | ||
| val, ok := t.activeExposes.Load(key) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| expose := val.(*trackedExpose) | ||
| expose.mu.Lock() | ||
| if expose.expiring { | ||
| expose.mu.Unlock() | ||
| return false | ||
| } | ||
| expose.lastRenewed = time.Now() | ||
| expose.mu.Unlock() | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // StopTrackedExpose removes an active expose session from tracking. | ||
| // Returns false if the expose was not tracked. | ||
| func (t *exposeTracker) StopTrackedExpose(peerID, domain string) bool { | ||
| key := exposeKey(peerID, domain) | ||
| _, ok := t.activeExposes.LoadAndDelete(key) | ||
| return ok | ||
| } | ||
|
|
||
| // StartExposeReaper starts a background goroutine that reaps expired expose sessions. | ||
| func (t *exposeTracker) StartExposeReaper(ctx context.Context) { | ||
| go func() { | ||
| ticker := time.NewTicker(exposeReapInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
| t.reapExpiredExposes() | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (t *exposeTracker) reapExpiredExposes() { | ||
| t.activeExposes.Range(func(key, val any) bool { | ||
| expose := val.(*trackedExpose) | ||
| expose.mu.Lock() | ||
| expired := time.Since(expose.lastRenewed) > exposeTTL | ||
| if expired { | ||
| expose.expiring = true | ||
| } | ||
| expose.mu.Unlock() | ||
|
|
||
| if !expired { | ||
| return true | ||
| } | ||
|
|
||
| log.Infof("reaping expired expose session for peer %s, domain %s", expose.peerID, expose.domain) | ||
|
|
||
| err := t.manager.deleteServiceFromPeer(context.Background(), expose.accountID, expose.peerID, expose.domain, true) | ||
|
|
||
| s, _ := status.FromError(err) | ||
|
|
||
| switch { | ||
| case err == nil: | ||
| t.activeExposes.Delete(key) | ||
| case s.ErrorType == status.NotFound: | ||
| log.Debugf("service %s was already deleted", expose.domain) | ||
| default: | ||
| log.Errorf("failed to delete expired peer-exposed service for domain %s: %v", expose.domain, err) | ||
| } | ||
|
|
||
| return true | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard
status.FromErrorto avoid nil deref panic.Line 150 assigns
swithout checkingok. IfdeleteServiceFromPeerreturns a non-status.Error,sis nil ands.ErrorTypewill panic, crashing the reaper goroutine.🛠️ Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will fix in a new Pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mlsmaycon, understood! Would you like me to create a GitHub issue to track this nil pointer dereference fix for the follow-up PR? I can include the context and suggested fix to make it easier to address later.