Skip to content

Commit 06427a3

Browse files
committed
Add optional load-balance strategy
1 parent 3eaf8d7 commit 06427a3

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

README-cn.md

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

src/config/binding.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,29 @@ namespace INIBinding
239239

240240
if(conf.Type == ProxyGroupType::URLTest || conf.Type == ProxyGroupType::LoadBalance || conf.Type == ProxyGroupType::Fallback)
241241
{
242+
// - url-test/fallback: ...`<url>`<times>`
243+
// - load-balance: ...`<url>`<times>`[<strategy>]
242244
if(rules_upper_bound < 5)
243245
continue;
244-
rules_upper_bound -= 2;
246+
247+
bool has_strategy = false;
248+
if(conf.Type == ProxyGroupType::LoadBalance && rules_upper_bound >= 6)
249+
{
250+
const String &maybeStrategy = vArray[rules_upper_bound - 1];
251+
switch(hash_(maybeStrategy))
252+
{
253+
case "consistent-hashing"_hash:
254+
conf.Strategy = BalanceStrategy::ConsistentHashing;
255+
has_strategy = true;
256+
break;
257+
case "round-robin"_hash:
258+
conf.Strategy = BalanceStrategy::RoundRobin;
259+
has_strategy = true;
260+
break;
261+
}
262+
}
263+
264+
rules_upper_bound -= (has_strategy ? 3U : 2U);
245265
conf.Url = vArray[rules_upper_bound];
246266
parseGroupTimes(vArray[rules_upper_bound + 1], &conf.Interval, &conf.Timeout, &conf.Tolerance);
247267
}

0 commit comments

Comments
 (0)