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.
This commit is contained in:
Roman Khimov 2021-04-14 23:53:47 +03:00 committed by Roman Khimov
parent 0d21ca382f
commit f019c97ce8
3 changed files with 4 additions and 5 deletions

View file

@ -80,5 +80,5 @@ HTTP_GW_WEB_MAX_REQUEST_BODY_SIZE=4194304 - maximum request body size, server r
Peers preset: Peers preset:
HTTP_GW_PEERS_<N>_ADDRESS = string HTTP_GW_PEERS_<N>_ADDRESS = string
HTTP_GW_PEERS_<N>_WEIGHT = 0..1 (float) HTTP_GW_PEERS_<N>_WEIGHT = float
``` ```

View file

@ -3,7 +3,6 @@ package connections
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"math"
"math/rand" "math/rand"
"sync" "sync"
"time" "time"
@ -40,8 +39,8 @@ func (pb *PoolBuilder) Build(ctx context.Context, options *PoolBuilderOptions) (
for _, w := range pb.weights { for _, w := range pb.weights {
totalWeight += w totalWeight += w
} }
if math.Abs(totalWeight-1.0) >= 1e-4 { for i, w := range pb.weights {
return nil, errors.New("total weight must be equal to unity") pb.weights[i] = w / totalWeight
} }
var cons = make([]*grpc.ClientConn, len(pb.addresses)) var cons = make([]*grpc.ClientConn, len(pb.addresses))
for i, address := range pb.addresses { for i, address := range pb.addresses {

View file

@ -183,7 +183,7 @@ func settings() *viper.Viper {
fmt.Println() fmt.Println()
fmt.Printf("%s_%s_[N]_ADDRESS = string\n", Prefix, strings.ToUpper(cfgPeers)) 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) os.Exit(0)
case version != nil && *version: case version != nil && *version: