コード例 #1
0
ファイル: round_robin.go プロジェクト: TykTechnologies/tyk
func (r *RoundRobin) SetMax(rp *tykcommon.HostList) {
	if r.max = rp.Len() - 1; r.max < 0 {
		r.max = 0
	}

	// Can't have a new list substituted that's shorter
	if r.cur > r.max {
		r.cur = 0
	}
	if r.pos > r.max {
		r.pos = 0
	}
}
コード例 #2
0
func GetNextTarget(targetData *tykcommon.HostList, spec *APISpec, tryCount int) string {
	if spec.Proxy.EnableLoadBalancing {
		log.Debug("[PROXY] [LOAD BALANCING] Load balancer enabled, getting upstream target")
		// Use a HostList
		spec.RoundRobin.SetMax(targetData)

		pos := spec.RoundRobin.GetPos()
		if pos > (targetData.Len() - 1) {
			// problem
			spec.RoundRobin.SetMax(targetData)
			pos = 0
		}

		gotHost, err := targetData.GetIndex(pos)
		if err != nil {
			log.Error("[PROXY] [LOAD BALANCING] ", err)
			return gotHost
		}

		thisHost := EnsureTransport(gotHost)

		// Check hosts against uptime tests
		if spec.Proxy.CheckHostAgainstUptimeTests {
			if !GlobalHostChecker.IsHostDown(thisHost) {
				// Don't overdo it
				if tryCount < targetData.Len() {
					// Host is down, skip
					return GetNextTarget(targetData, spec, tryCount+1)
				}
				log.Error("[PROXY] [LOAD BALANCING] All hosts seem to be down, all uptime tests are failing!")
			}
		}

		return thisHost
	}
	// Use standard target - might still be service data
	log.Debug("TARGET DATA:", targetData)

	gotHost, err := targetData.GetIndex(0)
	if err != nil {
		log.Error("[PROXY] ", err)
		return gotHost
	}
	return EnsureTransport(gotHost)
}