From f019c97ce8ee9700773d87ee2686877ac0a3d1f2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 14 Apr 2021 23:53:47 +0300 Subject: [PATCH] connections: normalize weights, make config a bit more human-oriented Current scheme requires people to calculate exact percentages for their nodes. People are bad with pecentage calculations and it's really inconvenient if you have more than 3-4 nodes. What I'd like to have is ability to specify weights like weights, not percentages: .._0_WEIGHT=3 .._1_WEIGHT=1 .._2_WEIGHT=1 and let the gateway calculate things for me. --- README.md | 2 +- connections/pool.go | 5 ++--- settings.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6e214cc..8a394c5 100644 --- a/README.md +++ b/README.md @@ -80,5 +80,5 @@ HTTP_GW_WEB_MAX_REQUEST_BODY_SIZE=4194304 - maximum request body size, server r Peers preset: HTTP_GW_PEERS__ADDRESS = string -HTTP_GW_PEERS__WEIGHT = 0..1 (float) +HTTP_GW_PEERS__WEIGHT = float ``` diff --git a/connections/pool.go b/connections/pool.go index 256acfa..f8b8f57 100644 --- a/connections/pool.go +++ b/connections/pool.go @@ -3,7 +3,6 @@ package connections import ( "context" "crypto/ecdsa" - "math" "math/rand" "sync" "time" @@ -40,8 +39,8 @@ func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) ( for _, w := range pb.weights { totalWeight += w } - if math.Abs(totalWeight-1.0) >= 1e-4 { - return nil, errors.New("total weight must be equal to unity") + for i, w := range pb.weights { + pb.weights[i] = w / totalWeight } var cons = make([]*grpc.ClientConn, len(pb.addresses)) for i, address := range pb.addresses { diff --git a/settings.go b/settings.go index 32326cd..4e4dabb 100644 --- a/settings.go +++ b/settings.go @@ -183,7 +183,7 @@ func settings() *viper.Viper { fmt.Println() fmt.Printf("%s_%s_[N]_ADDRESS = string\n", Prefix, strings.ToUpper(cfgPeers)) - fmt.Printf("%s_%s_[N]_WEIGHT = 0..1 (float)\n", Prefix, strings.ToUpper(cfgPeers)) + fmt.Printf("%s_%s_[N]_WEIGHT = float\n", Prefix, strings.ToUpper(cfgPeers)) os.Exit(0) case version != nil && *version: