type RateLimiter … type RateLimiterFactoryFunc … type RateLimitedSafeWaitGroup … // Add adds delta, which may be negative, similar to sync.WaitGroup. // If Add with a positive delta happens after Wait, it will return error, // which prevent unsafe Add. func (wg *RateLimitedSafeWaitGroup) Add(delta int) error { … } // Done decrements the WaitGroup counter, rate limiting is applied only // when the wait group is in waiting mode. func (wg *RateLimitedSafeWaitGroup) Done() { … } // Wait blocks until the WaitGroup counter is zero or a hard limit has elapsed. // It returns the number of active request(s) accounted for at the time Wait // has been invoked, number of request(s) that have drianed (done using the // wait group immediately before Wait returns). // Ideally, the both numbers returned should be equal, to indicate that all // request(s) using the wait group have released their lock. func (wg *RateLimitedSafeWaitGroup) Wait(limiterFactory RateLimiterFactoryFunc) (int, int, error) { … }