lib/config, lib/connections: Add optional connection limits (fixes #7176) (#7223)

This adds two new configuration options:

    // The number of connections at which we stop trying to connect to more
    // devices, zero meaning no limit. Does not affect incoming connections.
    ConnectionLimitEnough int

    // The maximum number of connections which we will allow in total, zero
    // meaning no limit. Affects incoming connections and prevents
    // attempting outgoing connections.
    ConnectionLimitMax int

These can be used to limit the number of concurrent connections in
various ways.
This commit is contained in:
Jakob Borg
2021-01-11 15:14:44 +01:00
committed by GitHub
parent e6f0ed65be
commit 0b193b76c2
13 changed files with 555 additions and 218 deletions
+22
View File
@@ -47,6 +47,14 @@ func (opts *OptionsConfiguration) prepare(guiPWIsSet bool) {
}
}
}
// Negative limits are meaningless, zero means unlimited.
if opts.ConnectionLimitEnough < 0 {
opts.ConnectionLimitEnough = 0
}
if opts.ConnectionLimitMax < 0 {
opts.ConnectionLimitMax = 0
}
}
// RequiresRestartOnly returns a copy with only the attributes that require
@@ -178,3 +186,17 @@ func (opts OptionsConfiguration) FeatureFlag(name string) bool {
return false
}
// LowestConnectionLimit is the lower of ConnectionLimitEnough or
// ConnectionLimitMax, or whichever of them is actually set if only one of
// them is set. It's the point where we should stop dialling.
func (opts OptionsConfiguration) LowestConnectionLimit() int {
limit := opts.ConnectionLimitEnough
if limit == 0 || (opts.ConnectionLimitMax != 0 && opts.ConnectionLimitMax < limit) {
// It doesn't really make sense to set Max lower than Enough but
// someone might do it while experimenting and it's easy for us to
// do the right thing.
limit = opts.ConnectionLimitMax
}
return limit
}