mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
Disconnect from subscriptions before auto-selecting servers from them
to limit the number of connected servers and avoid hitting the limit
This commit is contained in:
2 files changed
+49
-22
No files matched your search
+11
-3
@@ -225,6 +225,13 @@ func updateSubscriptions() {
|
||||
subs := configure.GetSubscriptions()
|
||||
lenSubs := len(subs)
|
||||
control := make(chan struct{}, 2) //并发限制同时更新2个订阅
|
||||
// Disconnect from subscriptions before auto-selecting servers from them
|
||||
// to limit the number of connected servers and avoid hitting the limit
|
||||
shouldDisconnect := true
|
||||
err := service.AutoSelectServersFromSubscriptions(shouldDisconnect)
|
||||
if err != nil {
|
||||
log.Error("[AutoSelect] Failed to disconnect servers from subscriptions -- err: %v", err)
|
||||
}
|
||||
wg := new(sync.WaitGroup)
|
||||
for i := 0; i < lenSubs; i++ {
|
||||
wg.Add(1)
|
||||
@@ -241,9 +248,10 @@ func updateSubscriptions() {
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
err := service.AutoSelectServersFromSubscriptions()
|
||||
if err != nil {
|
||||
log.Error("[AutoSelect] Failed to auto-select servers from subscriptions -- err: %v", err)
|
||||
shouldDisconnect = false
|
||||
err2 := service.AutoSelectServersFromSubscriptions(shouldDisconnect)
|
||||
if err2 != nil {
|
||||
log.Error("[AutoSelect] Failed to auto-select servers from subscriptions -- err: %v", err2)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ func ModifySubscriptionRemark(subscription touch.Subscription) (err error) {
|
||||
return configure.SetSubscription(subscription.ID-1, raw)
|
||||
}
|
||||
|
||||
func SelectServersFromSubscription(index int) (err error) {
|
||||
func SelectServersFromSubscription(index int, shouldDisconnect bool) (err error) {
|
||||
var subscriptionServer configure.Which
|
||||
subscriptionServer.TYPE = "subscriptionServer"
|
||||
subscriptionServer.Sub = index // Subscription IDs start with 0
|
||||
@@ -295,28 +295,47 @@ func SelectServersFromSubscription(index int) (err error) {
|
||||
continue
|
||||
}
|
||||
|
||||
err := Connect(&subscriptionServer)
|
||||
if err == nil {
|
||||
log.Info("[AutoSelect] Automatically selected server: %v", serverName)
|
||||
if shouldDisconnect {
|
||||
err := Disconnect(subscriptionServer, true)
|
||||
if err == nil {
|
||||
log.Info("[AutoSelect] Disconnected from server: %v", serverName)
|
||||
} else {
|
||||
log.Error("[AutoSelect] Failed to disconnect from server: %v", serverName)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
log.Error("[AutoSelect] Failed to connect to server: %v", serverName)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AutoSelectServersFromSubscriptions() (err error) {
|
||||
for i := 0; i < configure.GetLenSubscriptions(); i++ {
|
||||
subscription := configure.GetSubscription(i)
|
||||
if subscription.AutoSelect == true {
|
||||
log.Info("[AutoSelect] Automatically selecting servers from subscription: %v", subscription.Address)
|
||||
err := SelectServersFromSubscription(i)
|
||||
if err != nil {
|
||||
log.Error("[AutoSelect] Failed to select servers from subscription: %v", subscription.Address)
|
||||
err := Connect(&subscriptionServer)
|
||||
if err == nil {
|
||||
log.Info("[AutoSelect] Automatically selected server: %v", serverName)
|
||||
} else {
|
||||
log.Error("[AutoSelect] Failed to connect to server: %v", serverName)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AutoSelectServersFromSubscriptions(shouldDisconnect bool) (err error) {
|
||||
for i := 0; i < configure.GetLenSubscriptions(); i++ {
|
||||
subscription := configure.GetSubscription(i)
|
||||
if subscription.AutoSelect {
|
||||
if shouldDisconnect {
|
||||
log.Info("[AutoSelect] Automatically disconnecting servers from subscription: %v", subscription.Address)
|
||||
err := SelectServersFromSubscription(i, true)
|
||||
if err != nil {
|
||||
log.Error("[AutoSelect] Failed to disconnect servers from subscription: %v", subscription.Address)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
log.Info("[AutoSelect] Automatically selecting servers from subscription: %v", subscription.Address)
|
||||
err := SelectServersFromSubscription(i, false)
|
||||
if err != nil {
|
||||
log.Error("[AutoSelect] Failed to select servers from subscription: %v", subscription.Address)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user