// NewAerospike returns a new Aerospike client. func NewAerospike() (as *aerospike.Client, err error) { var target string if AerospikeFromConsulFlag.Get() { for retry := 0; retry < 15; retry++ { target, _, err = scale.DereferenceService("aerospike") if err == nil { break } if err == scale.ErrServiceNotFound || strings.Contains(err.Error(), "network is unreachable") || strings.Contains(err.Error(), "no such host") { time.Sleep(1 * time.Second) continue } return nil, err } if err != nil { return nil, err } } else { target = AerospikeFixedAddrFlag.Get() } ipPort := strings.Split(target, ":") port, err := strconv.Atoi(ipPort[1]) if err != nil { return nil, err } for retry := 0; retry < 15; retry++ { as, err = aerospike.NewClient(ipPort[0], port) if err == nil { return as, nil } if strings.Contains(err.Error(), "Failed to connect") { time.Sleep(1 * time.Second) continue } return nil, err } return nil, err }
func (finder *Finder) getInstance( env, service, servingID string, version int64) ( levInstTarget *LevInstTarget, targetNode string, levInstResource *scale.Resource, isNewInstance bool, err error) { // First, try DNS-based as it is faster and provides proper // load-balancing. hostAddr, targetNode, err := scale.DereferenceService(servingID) if err == scale.ErrServiceNotFound { // Then try resource-based. This has the disadvantage that it always // picks the same instance (the one registered as a resource). levInstResourceID := makeLevInstResourceID(env, service, version) levInstResource, err = scale.DereferenceResource( leverutil.ServiceFlag.Get(), levInstResourceID) if err != nil { finder.logger.WithFields("err", err).Error( "Could not dereference Lever instance") return nil, "", nil, false, err } if levInstResource == nil { // No instance found for that service. Bring up a fresh one. return finder.newInstance(env, service, version) } levInstTarget = new(LevInstTarget) err = json.Unmarshal([]byte(levInstResource.GetTarget()), levInstTarget) if err != nil { finder.logger.WithFields("err", err).Panic("Failed to decode json") } finder.logger.WithFields( "leverEnv", env, "leverService", service, "leverInstanceID", levInstTarget.InstanceID, ).Debug("Reusing instance") return levInstTarget, levInstResource.GetTargetNode(), levInstResource, false, nil } else if err != nil { finder.logger.WithFields("err", err).Error("DNS lookup failed") return nil, "", nil, false, err } return &LevInstTarget{HostAddr: hostAddr}, targetNode, nil, false, nil }