Golang implementation of weighted polling scheduling algorithm

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package main import ("FMT" "Time") var Slavedns = map[int]map[string]interface{}{0: {"Conne Ctstring ":" 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)}}

Source: http://studygolang.com/articles/8924

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.