1. 程式人生 > >golang實現權重輪詢排程演算法(Weighted Round-Robin Scheduling)

golang實現權重輪詢排程演算法(Weighted Round-Robin Scheduling)

參考<<權重輪詢排程演算法(Weighted Round-Robin Scheduling)>>這篇文章寫了一個golang版本的權重輪詢排程演算法,請大家指教,不多說了直接上程式碼:

package main

import (
	"fmt"
	"time"
)

var slaveDns = map[int]map[string]interface{}{
	0: {"connectstring": "[email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8", "weight": 8},
	1: {"connectstring": "
[email protected]
(172.16.0.165:3306)/shiqu_tools?charset=utf8", "weight": 4}, 2: {"connectstring": "[email protected](172.16.0.166:3306)/shiqu_tools?charset=utf8", "weight": 2}, } var i int = -1 //表示上一次選擇的伺服器 var cw int = 0 //表示當前排程的權值 var gcd int = 2 //當前所有權重的最大公約數 比如 2,4,8 的最大公約數為:2 /* 演算法思路: cw為當前的權重,遍歷每個伺服器,如果伺服器的權值大於cw,則該伺服器執行一次,一輪輪詢之後,cw-gcd; 之後重複上訴步驟 */ func getDns() string { for { i = (i + 1) % len(slaveDns) if i == 0 { cw = cw - gcd if cw <= 0 { cw = getMaxWeight() if cw == 0 { return "" } } } if weight, _ := slaveDns[i]["weight"].(int); weight >= cw { return slaveDns[i]["connectstring"].(string) } } } func getMaxWeight() int { max := 0 for _, v := range slaveDns { if weight, _ := v["weight"].(int); weight >= max { max = weight } } return max } func main() { note := map[string]int{} s_time := time.Now().Unix() for i := 0; i < 100; i++ { s := getDns() fmt.Println(s) if note[s] != 0 { note[s]++ } else { note[s] = 1 } } e_time := time.Now().Unix() fmt.Println("total time: ", e_time-s_time) fmt.Println("--------------------------------------------------") for k, v := range note { fmt.Println(k, " ", v) } }

執行結果:

C:/go/bin/go.exe build [D:/PROGRAM/Golang/src/study/roundRobin]
成功: 程序退出程式碼 0.
D:/PROGRAM/Golang/src/study/roundRobin/roundRobin.exe  [D:/PROGRAM/Golang/src/study/roundRobin]
[email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8
[email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8
[email protected]
(172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.165:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.165:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.166:3306)/shiqu_tools?charset=utf8 。。。(中間部分省略) [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.165:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.165:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.166:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 total time: 0 -------------------------------------------------- [email protected](172.16.0.164:3306)/shiqu_tools?charset=utf8 58 [email protected](172.16.0.165:3306)/shiqu_tools?charset=utf8 28 [email protected](172.16.0.166:3306)/shiqu_tools?charset=utf8 14 成功: 程序退出程式碼 0.