This is a creation in Article, where the information may have evolved or changed.
Recently looking at Golang connection MySQL database Example (Go-mysql-driver), but think connected database will certainly have the need to connect more from the library, and each from the library weight will be different, reference << weight Polling scheduling algorithm (Weighted Round-robin scheduling)>> This article wrote a golang version of the weight polling scheduling algorithm, please advise, not many said directly on the code:
Package main import ("FMT" "Time") var Slavedns = map[int]map[string]interface{}{0: {"c Onnectstring ":" Root@tcp (172.16.0.164:3306)/shiqu_tools?charset=utf8 "," Weight ": 2}, 1: {" connectstring ":" Root@tcp ( 172.16.0.165:3306)/shiqu_tools?charset=utf8 "," Weight ": 4}, 2: {" connectstring ":" Root@tcp (172.16.0.166:3306)/shiqu _tools?charset=utf8 "," Weight ": 8},} var i int =-1//= Last server selected var cw int = 0//Indicates the weight value of the current dispatch var gcd int = 2//Current weight greatest common divisor such as 2,4,8 's greatest common divisor is: 2 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 weigh T, _: = 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)}}
100 times The result is: