英文:
Go algorithm for looping through servers in predefined ratio
问题
我正在尝试创建一个算法,可以按照预定义的比例在后端服务器中循环执行操作。
例如,我有两个后端服务器:
type server struct {
addr string
ratio float64
counter int64
}
// s2 是一个强大的服务器,可以处理比 s1 多 3 倍的请求 *编辑
s1 := &server{":3000", 0.25}
s2 := &server{":3001", 0.75}
func nextServer() {
server := next() // 简单的 goroutine,用于在 s1 和 s2 之间选择下一个服务器
N := server.counter / i
if float64(N) > server.ratio {
// 重复调用这个函数
return nextServer()
}
server.counter += 1
}
for i := 0; i < 1000; i++ {
nextServer()
}
- s1 的计数器(处理的请求)为 250
- s2 是一个庞大的服务器,计数器为 750
这只是我所拥有的内容的一个非常简单的实现,但当 i 大约为 10000 时,它在 nextServer() 中不断循环,因为 N 总是大于 server.ratio。
只要 i 大约为 5000,它就能完美运行。但我认为有更好的算法可以按照比例进行循环。
如何使这个算法简单而稳定?
英文:
I am tying to make a algorithm that can loop true things, backend server in my case by a pre defined ratio.
for example I got 2 backend servers
type server struct {
addr string
ratio float64
counter int64
}
// s2 is a beast and may handle 3 times the requests then s1 *edit
s1 := &server{":3000", 0.25}
s2 := &server{":3001", 0.75}
func nextServer() {
server := next() // simple goroutine that provides the next server between s1 and s2
N := server.counter / i
if float64(N) > server.ratio {
//repeat this function
return nextServer()
}
server.counter += 1
}
for i := 0; i < 1000; i++ {
nextServer()
}
- s1 has 250 as counter (requests handled)
- s2 is huge so he has 750 as counter (requests handled)
this is a very simple implementation of what I got but when i is like 10000, it keeps looping in
nextServer() cause N is always > server.ratio.
as long as i is around 5000 it works perfect. but I think there are better algorithms for looping in ratios.
How to make this simple and solid?
答案1
得分: 3
像这样吗?
package main
import (
"fmt"
"math/rand"
)
type server struct {
addr string
ratio float64
}
var servers []server
func nextServer() *server {
rndFloat := rand.Float64() // 在0.0-1.0之间选择一个随机数
ratioSum := 0.0
for _, srv := range servers {
ratioSum += srv.ratio // 将列表中所有先前服务器的比率相加
if ratioSum >= rndFloat { // 如果比率总和超过随机数
return &srv // 返回该服务器
}
}
return nil // 不应该到达这里
}
func main() {
servers = []server{server{"0.25", 0.25}, server{"0.50", 0.50},
server{"0.10", 0.10}, server{"0.15", 0.15}}
counts := make(map[string]int, len(servers))
for i := 0; i < 100; i++ {
srv := nextServer()
counts[srv.addr] += 1
}
fmt.Println(counts)
}
例如,输出结果为:
map[0.50:56 0.15:15 0.25:24 0.10:5]
英文:
Something like this?
package main
import (
"fmt"
"math/rand"
)
type server struct {
addr string
ratio float64
}
var servers []server
func nextServer() *server {
rndFloat := rand.Float64() //pick a random number between 0.0-1.0
ratioSum := 0.0
for _, srv := range servers {
ratioSum += srv.ratio //sum ratios of all previous servers in list
if ratioSum >= rndFloat { //if ratiosum rises above the random number
return &srv //return that server
}
}
return nil //should not come here
}
func main() {
servers = []server{server{"0.25", 0.25}, server{"0.50", 0.50},
server{"0.10", 0.10}, server{"0.15", 0.15}}
counts := make(map[string]int, len(servers))
for i := 0; i < 100; i++ {
srv := nextServer()
counts[srv.addr] += 1
}
fmt.Println(counts)
}
Yields for example:
map[0.50:56 0.15:15 0.25:24 0.10:5]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论