Skip to content

Use internal rate limiters for all source requests

Created by: ryanslade

The internal rate limiters are currently only used for calls made through ChangesetSource: https://github.com/sourcegraph/sourcegraph/blob/094141b4de59ee0c2fb4a9e25e5fbb35154bebe5/cmd/repo-updater/repos/sources.go#L61

UPDATE

The eventual approach ended up being a bit different.

Rate limiting was moved into the client code once the registry was made global. All rate limiting is blocking with the assumption that if a user requires it to bail out early they will use a context with a duration.

ORIGINAL PLAN BELOW

We should be able to get a rate limiter for any source given it's external service config, but we can specialise the rate limiters to either wait or fail when we are past our limit. That way they can be used in both background syncers and calls that needs to fail fast.

I propose we do the following:

  1. Make the syncer cache global. Downside are the general downsides of global state. Upside is that it is easy to get a rate limiter anywhere we create a source.
  2. Create two wrappers around rate.Limiter that share this interface:
type RateLimiter interface {
  Limit(ctx context.Context, cost int) error
}

The wrapper used in Source will fail fast The wrapper used in ChangesetSource will block

  1. Make calls to rate limiter in all methods of both Source and ChangesetSource