コード例 #1
0
ファイル: doorman_client.go プロジェクト: youtube/doorman
func manipulateCapacity(res doorman.Resource, current float64, id string) {
	clientRequested := new(expvar.Float)
	for range time.Tick(*interval) {
		r := rand.Float64()
		log.V(2).Infof("r=%v decreaseChance=%v increaseChance=%v", r, *decreaseChance, *increaseChance)
		switch {
		case r < *decreaseChance:
			current -= *step
			log.Infof("client %v will request less: %v.", id, current)
		case r < *decreaseChance+*increaseChance:
			log.Infof("client %v will request more: %v.", id, current)
			current += *step
		default:
			log.V(2).Infof("client %v not changing requested capacity", id)
			continue
		}
		if current > *maxCapacity {
			current = *maxCapacity
		}
		if current < *minCapacity {
			current = *minCapacity
		}
		log.Infof("client %v will request %v", id, current)
		if err := res.Ask(current); err != nil {
			log.Errorf("res.Ask(%v): %v", current, err)
			askErrors.Add(1)
			continue
		}
		clientRequested.Set(current)
		requested.Set(id, clientRequested)
	}
}
コード例 #2
0
ファイル: ratelimiter_test.go プロジェクト: youtube/doorman
func singleThreadedWait(capacity float64, rl RateLimiter, res doorman.Resource, numLoops int) (time.Duration, error) {
	ctx := context.Background()
	res.Capacity() <- capacity

	start := time.Now()
	for i := 0; i < numLoops; i++ {
		err := rl.Wait(ctx)
		if err != nil {
			return 0, err
		}
	}

	return time.Since(start), nil
}