Explorar o código

Add optional load-balance strategy

riolurs hai 5 días
pai
achega
06427a3ccb
Modificáronse 2 ficheiros con 23 adicións e 1 borrados
  1. 2 0
      README-cn.md
  2. 21 1
      src/config/binding.h

+ 2 - 0
README-cn.md

@@ -955,6 +955,8 @@ custom_proxy_group=🇯🇵 日本延迟最低`url-test`(日|JP)`http://www.gsta
 # 表示创建一个叫 🇯🇵 日本延迟最低 的 url-test 策略组,并向其中添加名字含'日','JP'的节点,每隔300秒测试一次,测速超时为5s
 custom_proxy_group=负载均衡`load-balance`.*`http://www.gstatic.com/generate_204`300,,100
 # 表示创建一个叫 负载均衡 的 load-balance 策略组,并向其中添加所有的节点,每隔300秒测试一次,切换节点的延迟容差为100ms
+custom_proxy_group=负载均衡`load-balance`.*`http://www.gstatic.com/generate_204`300,,100`round-robin
+# 表示创建一个叫 负载均衡 的 load-balance 策略组,使用 round-robin 策略(默认使用 consistent-hashing)
 custom_proxy_group=🇯🇵 JP`select`沪日`日本`[]🇯🇵 日本延迟最低
 # 表示创建一个叫 🇯🇵 JP 的 select 策略组,并向其中**依次**添加名字含'沪日','日本'的节点,以及引用上述所创建的 🇯🇵 日本延迟最低 策略组
 custom_proxy_group=节点选择`select`(^(?!.*(美国|日本)).*)

+ 21 - 1
src/config/binding.h

@@ -239,9 +239,29 @@ namespace INIBinding
 
                 if(conf.Type == ProxyGroupType::URLTest || conf.Type == ProxyGroupType::LoadBalance || conf.Type == ProxyGroupType::Fallback)
                 {
+                    // - url-test/fallback: ...`<url>`<times>`
+                    // - load-balance:     ...`<url>`<times>`[<strategy>]
                     if(rules_upper_bound < 5)
                         continue;
-                    rules_upper_bound -= 2;
+
+                    bool has_strategy = false;
+                    if(conf.Type == ProxyGroupType::LoadBalance && rules_upper_bound >= 6)
+                    {
+                        const String &maybeStrategy = vArray[rules_upper_bound - 1];
+                        switch(hash_(maybeStrategy))
+                        {
+                        case "consistent-hashing"_hash:
+                            conf.Strategy = BalanceStrategy::ConsistentHashing;
+                            has_strategy = true;
+                            break;
+                        case "round-robin"_hash:
+                            conf.Strategy = BalanceStrategy::RoundRobin;
+                            has_strategy = true;
+                            break;
+                        }
+                    }
+
+                    rules_upper_bound -= (has_strategy ? 3U : 2U);
                     conf.Url = vArray[rules_upper_bound];
                     parseGroupTimes(vArray[rules_upper_bound + 1], &conf.Interval, &conf.Timeout, &conf.Tolerance);
                 }