dtoaillwk759656786 2014-09-24 11:32
浏览 24
已采纳

Go算法以预定义比例遍历服务器

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条回答 默认 最新

  • dongxuxian6930 2014-09-24 12:19
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应