コード例 #1
0
ファイル: gce.go プロジェクト: General-Beck/worker
func (p *gceProvider) apiRateLimit(ctx gocontext.Context) error {
	metrics.Gauge("travis.worker.vm.provider.gce.rate-limit.queue", int64(p.rateLimitQueueDepth))
	startWait := time.Now()
	defer metrics.TimeSince("travis.worker.vm.provider.gce.rate-limit", startWait)

	atomic.AddUint64(&p.rateLimitQueueDepth, 1)
	// This decrements the counter, see the docs for atomic.AddUint64
	defer atomic.AddUint64(&p.rateLimitQueueDepth, ^uint64(0))

	errCount := 0

	for {
		ok, err := p.rateLimiter.RateLimit("gce-api", p.rateLimitMaxCalls, p.rateLimitDuration)
		if err != nil {
			errCount++
			if errCount >= 5 {
				context.CaptureError(ctx, err)
				context.LoggerFromContext(ctx).WithField("err", err).Info("rate limiter errored 5 times")
				return err
			}
		} else {
			errCount = 0
		}
		if ok {
			return nil
		}

		// Sleep for up to 1 second
		time.Sleep(time.Millisecond * time.Duration(mathrand.Intn(1000)))
	}
}
コード例 #2
0
ファイル: gce.go プロジェクト: dongjoon-hyun/worker
func (p *gceProvider) apiRateLimit() {
	atomic.AddUint64(&p.rateLimitQueueDepth, 1)
	metrics.Gauge("travis.worker.vm.provider.gce.rate-limit.queue", int64(p.rateLimitQueueDepth))
	startWait := time.Now()
	<-p.rateLimiter.C
	metrics.TimeSince("travis.worker.vm.provider.gce.rate-limit", startWait)
	// This decrements the counter, see the docs for atomic.AddUint64
	atomic.AddUint64(&p.rateLimitQueueDepth, ^uint64(0))
}