-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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.
137 changes: 137 additions & 0 deletions
137
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,137 @@ | ||
| package manager | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| type exposeTracker struct { | ||
| activeExposes sync.Map | ||
| exposeCreateMu sync.Mutex | ||
| manager *managerImpl | ||
| } | ||
|
|
||
| func exposeKey(peerID, domain string) string { | ||
| return peerID + ":" + domain | ||
| } | ||
|
|
||
| // TrackExpose registers a new active expose session. Returns true if the expose | ||
| // was already tracked (duplicate). | ||
| func (t *exposeTracker) TrackExpose(peerID, domain, accountID string) bool { | ||
| key := exposeKey(peerID, domain) | ||
| _, loaded := t.activeExposes.LoadOrStore(key, &trackedExpose{ | ||
| domain: domain, | ||
| accountID: accountID, | ||
| peerID: peerID, | ||
| lastRenewed: time.Now(), | ||
| }) | ||
| return loaded | ||
| } | ||
|
|
||
| // 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. | ||
| 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() | ||
| 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 | ||
| } | ||
|
|
||
| // CheckPeerExposeLimitWithLock atomically checks whether the peer can create a new expose. | ||
| // Returns true if the peer is within the limit. | ||
| func (t *exposeTracker) CheckPeerExposeLimitWithLock(peerID string) bool { | ||
| t.exposeCreateMu.Lock() | ||
| defer t.exposeCreateMu.Unlock() | ||
| return t.CountPeerExposes(peerID) < maxExposesPerPeer | ||
| } | ||
|
|
||
| // 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 | ||
| expose.mu.Unlock() | ||
|
|
||
| if expired { | ||
| if _, deleted := t.activeExposes.LoadAndDelete(key); deleted { | ||
| log.Infof("reaping expired expose session for peer %s, domain %s", expose.peerID, expose.domain) | ||
| if err := t.manager.deleteServiceFromPeer(context.Background(), expose.accountID, expose.peerID, expose.domain, true); err != nil { | ||
| log.Errorf("failed to delete expired peer-exposed service for domain %s: %v", expose.domain, err) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| 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.