func (p *proxy) lookup(request *http.Request) *route.Pool { uri := route.Uri(hostWithoutPort(request)) // Choose backend using host alone return p.registry.Lookup(uri) }
func (r *RouteRegistry) GenerateUriMap() (map[route.Uri]*route.Pool, bool) { byUriTmp = make(map[route.Uri]*route.Pool) rc := RedisConnPool.Get() defer rc.Close() uriList, err := redis.Strings(rc.Do("KEYS", "*")) if err != nil { r.logger.Error(err.Error()) return nil, false } for _, uriString := range uriList { uriType := route.Uri(strings.Split(uriString, "/")[1]) if uriType == "rs" { uri := route.Uri(strings.Split(uriString, "/")[2]) uri = uri.ToLower() pool, found := byUriTmp[uri] if !found { pool = route.NewPool(r.reloadUriInterval / 5) byUriTmp[uri] = pool } addresslist, err := redis.Strings(rc.Do("LRANGE", uriString, "0", "-1")) if err != nil { r.logger.Error(err.Error()) return nil, false } for _, address := range addresslist { host := strings.Split(address, ":")[0] p, _ := strconv.Atoi(strings.Split(address, ":")[1]) port := uint16(p) endpoint := route.NewEndpoint(host, port, nil) pool.Put(endpoint) } } } for _, uriString := range uriList { uriType := route.Uri(strings.Split(uriString, "/")[1]) if uriType == "cname" { cname := route.Uri(strings.Split(uriString, "/")[2]) cname = cname.ToLower() _, found := byUriTmp[cname] if !found { u, err := redis.String(rc.Do("GET", uriString)) if err != nil { r.logger.Error(err.Error()) return nil, false } uri := route.Uri(strings.Split(u, "/")[2]) uri = uri.ToLower() pool, found := byUriTmp[uri] if !found { r.logger.Warnf("cname %s points to uri %s, but uri %s do not have rs", cname, uri, uri) } else { byUriTmp[cname] = pool } } else { r.logger.Warnf("cname %s has rs", cname) } } } return byUriTmp, true }