-
-
Notifications
You must be signed in to change notification settings - Fork 532
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
[client] Limit P2P attempts and restart on specific events #2657
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
be42b0b
Limit P2P attempts and restart on specific events
lixmal 120710f
Add missing const
lixmal a261d07
Handle error
lixmal 6c60d04
Move code to separate file
lixmal be41a23
Fix return
lixmal 34b7fb5
Add back sleep
lixmal 1fc1cbe
Add missing comment
lixmal 7fc8180
Don't consider relay candidates
lixmal efe3a04
Update client/internal/peer/conn_monitor.go
lixmal ecc0629
Use Address only
lixmal c9f99da
Update client/internal/peer/conn_monitor.go
lixmal 77ec0b0
Move conn monitor to own struct
lixmal 0682483
Remove unused method
lixmal c11d807
Rename file
lixmal 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 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 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,212 @@ | ||
package peer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pion/ice/v3" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/netbirdio/netbird/client/internal/stdnet" | ||
) | ||
|
||
const ( | ||
signalerMonitorPeriod = 5 * time.Second | ||
candidatesMonitorPeriod = 5 * time.Minute | ||
candidateGatheringTimeout = 5 * time.Second | ||
) | ||
|
||
type ConnMonitor struct { | ||
signaler *Signaler | ||
iFaceDiscover stdnet.ExternalIFaceDiscover | ||
config ConnConfig | ||
relayDisconnected chan bool | ||
iCEDisconnected chan bool | ||
reconnectCh chan struct{} | ||
currentCandidates []ice.Candidate | ||
candidatesMu sync.Mutex | ||
} | ||
|
||
func NewConnMonitor(signaler *Signaler, iFaceDiscover stdnet.ExternalIFaceDiscover, config ConnConfig, relayDisconnected, iCEDisconnected chan bool) (*ConnMonitor, <-chan struct{}) { | ||
reconnectCh := make(chan struct{}, 1) | ||
cm := &ConnMonitor{ | ||
signaler: signaler, | ||
iFaceDiscover: iFaceDiscover, | ||
config: config, | ||
relayDisconnected: relayDisconnected, | ||
iCEDisconnected: iCEDisconnected, | ||
reconnectCh: reconnectCh, | ||
} | ||
return cm, reconnectCh | ||
} | ||
|
||
func (cm *ConnMonitor) Start(ctx context.Context) { | ||
signalerReady := make(chan struct{}, 1) | ||
go cm.monitorSignalerReady(ctx, signalerReady) | ||
|
||
localCandidatesChanged := make(chan struct{}, 1) | ||
go cm.monitorLocalCandidatesChanged(ctx, localCandidatesChanged) | ||
|
||
for { | ||
select { | ||
case changed := <-cm.relayDisconnected: | ||
if !changed { | ||
continue | ||
} | ||
log.Debugf("Relay state changed, triggering reconnect") | ||
cm.triggerReconnect() | ||
|
||
case changed := <-cm.iCEDisconnected: | ||
if !changed { | ||
continue | ||
} | ||
log.Debugf("ICE state changed, triggering reconnect") | ||
cm.triggerReconnect() | ||
|
||
case <-signalerReady: | ||
log.Debugf("Signaler became ready, triggering reconnect") | ||
cm.triggerReconnect() | ||
|
||
case <-localCandidatesChanged: | ||
log.Debugf("Local candidates changed, triggering reconnect") | ||
cm.triggerReconnect() | ||
|
||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (cm *ConnMonitor) monitorSignalerReady(ctx context.Context, signalerReady chan<- struct{}) { | ||
if cm.signaler == nil { | ||
return | ||
} | ||
|
||
ticker := time.NewTicker(signalerMonitorPeriod) | ||
defer ticker.Stop() | ||
|
||
lastReady := true | ||
for { | ||
select { | ||
case <-ticker.C: | ||
currentReady := cm.signaler.Ready() | ||
if !lastReady && currentReady { | ||
select { | ||
case signalerReady <- struct{}{}: | ||
default: | ||
} | ||
} | ||
lastReady = currentReady | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (cm *ConnMonitor) monitorLocalCandidatesChanged(ctx context.Context, localCandidatesChanged chan<- struct{}) { | ||
ufrag, pwd, err := generateICECredentials() | ||
if err != nil { | ||
log.Warnf("Failed to generate ICE credentials: %v", err) | ||
return | ||
} | ||
|
||
ticker := time.NewTicker(candidatesMonitorPeriod) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
if err := cm.handleCandidateTick(ctx, localCandidatesChanged, ufrag, pwd); err != nil { | ||
log.Warnf("Failed to handle candidate tick: %v", err) | ||
} | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (cm *ConnMonitor) handleCandidateTick(ctx context.Context, localCandidatesChanged chan<- struct{}, ufrag string, pwd string) error { | ||
log.Debugf("Gathering ICE candidates") | ||
|
||
transportNet, err := newStdNet(cm.iFaceDiscover, cm.config.ICEConfig.InterfaceBlackList) | ||
if err != nil { | ||
log.Errorf("failed to create pion's stdnet: %s", err) | ||
} | ||
|
||
agent, err := newAgent(cm.config, transportNet, candidateTypesP2P(), ufrag, pwd) | ||
if err != nil { | ||
return fmt.Errorf("create ICE agent: %w", err) | ||
} | ||
defer func() { | ||
if err := agent.Close(); err != nil { | ||
log.Warnf("Failed to close ICE agent: %v", err) | ||
} | ||
}() | ||
|
||
gatherDone := make(chan struct{}) | ||
err = agent.OnCandidate(func(c ice.Candidate) { | ||
log.Tracef("Got candidate: %v", c) | ||
if c == nil { | ||
close(gatherDone) | ||
} | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("set ICE candidate handler: %w", err) | ||
} | ||
|
||
if err := agent.GatherCandidates(); err != nil { | ||
return fmt.Errorf("gather ICE candidates: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(ctx, candidateGatheringTimeout) | ||
defer cancel() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return fmt.Errorf("wait for gathering: %w", ctx.Err()) | ||
case <-gatherDone: | ||
} | ||
|
||
candidates, err := agent.GetLocalCandidates() | ||
if err != nil { | ||
return fmt.Errorf("get local candidates: %w", err) | ||
} | ||
log.Tracef("Got candidates: %v", candidates) | ||
|
||
if changed := cm.updateCandidates(candidates); changed { | ||
select { | ||
case localCandidatesChanged <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cm *ConnMonitor) updateCandidates(newCandidates []ice.Candidate) bool { | ||
cm.candidatesMu.Lock() | ||
defer cm.candidatesMu.Unlock() | ||
|
||
if len(cm.currentCandidates) != len(newCandidates) { | ||
cm.currentCandidates = newCandidates | ||
return true | ||
} | ||
|
||
for i, candidate := range cm.currentCandidates { | ||
if candidate.Address() != newCandidates[i].Address() { | ||
cm.currentCandidates = newCandidates | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (cm *ConnMonitor) triggerReconnect() { | ||
select { | ||
case cm.reconnectCh <- struct{}{}: | ||
default: | ||
} | ||
} |
This file contains 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 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
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.
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.
Does it make no sense to move this file into a separate structure? I do not see any dependency from the conn that is not exchangeable.
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.
Sonar will complain :)
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.
for what?
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.
That the file is too large
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.
I can not follow you. If you move everything from conn_monitor.go to monitor.go and in it create a
Then we can get a better separated code structure that is better testable by unit tests.
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.
I have moved it to a separate struct. But not its own package yet because that requires more refactoring on the other components
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.
This looks nice! I will review it again.