//LoadZone 读取ip地址段,并保存到rbtree中 func (ipdisp *IPDisp) LoadZone(conf string) (err error) { ipdisp.rbtree = rbtree.NewWith(Comparator) var flines []string flines, err = file2string(conf) if err != nil { return } zoneids := ipdisp.zoneID ipdisp.zoneMax = 1 for _, fline := range flines { zone := Zone{} ipinfo := strings.Split(fline, ";") if len(ipinfo) != 2 { continue } zone.name = ipinfo[1] ips := strings.Split(ipinfo[0], "/") if len(ips) != 2 { continue } m, err := strconv.Atoi(ips[1]) if err != nil { continue } zone.ipmin = InetNetwork(ips[0]) mask := (1 << (32 - uint32(m))) - 1 zone.ipmax = zone.ipmin | uint32(mask) if v, ok := zoneids[zone.name]; ok == true { zone.id = v } else { zoneids[zone.name] = ipdisp.zoneMax zone.id = ipdisp.zoneMax ipdisp.zoneMax++ } ipdisp.rbtree.Put(zone) //fmt.Printf("%v %v %v\n",ipzone.ipmin,ipzone.ipmax,ipzone.Zone) } return }
//LoadNode 加载每个host的node配置 func (ipdisp *IPDisp) LoadNode(conf string, vhostname string) (err error) { var flines []string flines, err = file2string(conf) if err != nil { return } ipdisp.vhosts[vhostname] = &Vhost{} vhost := ipdisp.vhosts[vhostname] vhost.nodeID = make(map[string]int) vhost.defaultNode = 0 vhost.reqcount = 0 nodeid := -1 var cnode *Node for _, fline := range flines { flen := len(fline) if flen < 3 || fline[0] == '#' { continue } if fline[0] == '[' && fline[flen-1] == ']' { //获取节点名称,初始化节点配置 //if nodeID > -1 { // cnode.servers[cnode.servercount-1].next = cnode.servers[0] //} nodename := string(fline[1 : flen-1]) cnode = &Node{} cnode.name = nodename cnode.status = 0 cnode.servercount = 0 cnode.balance = 'r' cnode.id = nodeid + 1 cnode.reqmin = 0 cnode.reqlastmin = 0 cnode.reqcount = 0 cnode.freebw = 20 cnode.sw = make([]int, swMAX) cnode.serverID = make(map[string]int) cnode.swtree = rbtree.NewWith(Comparator) vhost.nodes = append(vhost.nodes, cnode) nodeid++ vhost.nodeID[nodename] = nodeid } else { if nodeid == -1 { continue } cfline := string(fline) cf := strings.Split(cfline, "=") if len(cf) != 2 { continue } switch cf[0] { case "server": server := &Server{} server.status = 0 server.weight = 0 server.id = 0 sinfo := strings.Split(cf[1], " ") server.ip = sinfo[0] sinfolen := len(sinfo) switch sinfolen { case 2: server.id, err = strconv.Atoi(sinfo[1]) case 3: server.id, err = strconv.Atoi(sinfo[1]) server.weightstr = sinfo[2] case 4: server.id, err = strconv.Atoi(sinfo[1]) server.weightstr = sinfo[2] server.status = serverstat[sinfo[3]] } /* swcount = swcount + server.weight */ cnode.servers = append(cnode.servers, server) if cnode.servercount == 0 { cnode.curserver = server } else { cnode.servers[cnode.servercount-1].next = server } cnode.serverID[server.ip] = server.id cnode.servercount++ case "bw": cnode.bw, err = strconv.Atoi(cf[1]) case "maxbw": cnode.maxbw, err = strconv.Atoi(cf[1]) case "freebw": cnode.freebw, err = strconv.Atoi(cf[1]) case "overflow2node": cnode.overflow2node = cf[1] case "status": cnode.status = serverstat[cf[1]] case "default": vhost.defaultNode = nodeid case "balance": switch cf[1][0] { case 'h': cnode.balance = cf[1][0] case 'r': cnode.balance = cf[1][0] case 'w': cnode.balance = cf[1][0] case 'A': cnode.balance = cf[1][0] case 'a': cnode.balance = cf[1][0] default: err = errors.New(cnode.name + ": balance config is invalid") return } } } } for _, node := range vhost.nodes { var ok bool node.overflow2nodeid, ok = vhost.nodeID[node.overflow2node] if ok == false { node.overflow2nodeid = -1 } //使server数组中,server.next首尾相接 node.servers[node.servercount-1].next = node.servers[0] //添加特定均衡方式:o(only one),代表只有一个server if len(node.servers) == 1 { node.balance = 'o' } err = node.initbalance() } return }