From 0c3a25a8b5064de2b4cd3a08b9044dafed14f5d1 Mon Sep 17 00:00:00 2001 From: pat-s Date: Wed, 14 May 2025 01:17:57 +0000 Subject: [PATCH] fix: add missing loadbalancing policies for EngineGroup connections (#7799) This is a follow-up PR to #7212 (unreleased) in which some load balancing policy options were missed. - `WeightRoundRobin` follows the same logic as `WeightRandomPolicy` WRT to weight assignment. - `LeastConn` has no options related docs PR: https://codeberg.org/forgejo/docs/pulls/1109 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7799 Reviewed-by: Gusted Co-authored-by: pat-s Co-committed-by: pat-s --- modules/setting/database.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/setting/database.go b/modules/setting/database.go index b5131d3782..96859e4cf4 100644 --- a/modules/setting/database.go +++ b/modules/setting/database.go @@ -191,8 +191,29 @@ func BuildLoadBalancePolicy(settings *DatabaseSettings, slaveEngines []*xorm.Eng } } policy = xorm.WeightRandomPolicy(weights) + case "WeightRoundRobin": + var weights []int + if settings.LoadBalanceWeights != "" { + for part := range strings.SplitSeq(settings.LoadBalanceWeights, ",") { + w, err := strconv.Atoi(strings.TrimSpace(part)) + if err != nil { + w = 1 // use a default weight if conversion fails + } + weights = append(weights, w) + } + } + // If no valid weights were provided, default each slave to weight 1 + if len(weights) == 0 { + weights = make([]int, len(slaveEngines)) + for i := range weights { + weights[i] = 1 + } + } + policy = xorm.WeightRoundRobinPolicy(weights) case "RoundRobin": policy = xorm.RoundRobinPolicy() + case "LeastConn": + policy = xorm.LeastConnPolicy() default: policy = xorm.RandomPolicy() }