/// <summary> /// Returns a throttler for a specific service endpoint URL /// </summary> /// <param name="serviceEndpoint">URL of the mechanical turk service endpoint</param> /// <param name="capacity">Number of requests the throttler permits all at once /// (bucket capacity)</param> /// <param name="rate">Number of requests the throttler allows per second /// (average long term)</param> /// <returns>A <see cref="IRequestThrottler"/> instance</returns> public static LeakyBucketRequestThrottler GetInstance(string serviceEndpoint, int capacity, int rate) { if (serviceEndpoint == null) { throw new ArgumentNullException("serviceEndpoint", "Endpoint URL may not be null"); } if (capacity <= 0) { throw new ArgumentException("Capacity must be bigger than zero", "capacity"); } if (rate > capacity) { throw new ArgumentException("Rate must be bigger than capacity", "rate"); } LeakyBucketRequestThrottler ret = null; string key = string.Format("{0}{1}{2}", serviceEndpoint, capacity, rate); if (instances.ContainsKey(key)) { ret = instances[key]; } else { lock (instances) { if (instances.ContainsKey(key)) { ret = instances[key]; } else { MTurkLog.Debug("Throttling requests to {0} (Capacity: {1}. Rate: {2}/sec)", serviceEndpoint, capacity, rate); ret = new LeakyBucketRequestThrottler(serviceEndpoint, capacity, rate); instances[key] = ret; } } } return(ret); }
private void Dispose(bool disposing) { if (disposing) { lock (instances) { string key = string.Format("{0}{1}{2}", endpoint, capacity, ratePerSecond); LeakyBucketRequestThrottler throttler = instances[key]; MTurkLog.Debug("Disposing {0}", throttler); if (timerRefill != null) { timerRefill.Dispose(); } instances.Remove(key); } } }
/// <summary> /// Returns a throttler for a specific service endpoint URL /// </summary> /// <param name="serviceEndpoint">URL of the mechanical turk service endpoint</param> /// <param name="capacity">Number of requests the throttler permits all at once /// (bucket capacity)</param> /// <param name="rate">Number of requests the throttler allows per second /// (average long term)</param> /// <returns>A <see cref="IRequestThrottler"/> instance</returns> public static LeakyBucketRequestThrottler GetInstance(string serviceEndpoint, int capacity, int rate) { if (serviceEndpoint == null) { throw new ArgumentNullException("serviceEndpoint", "Endpoint URL may not be null"); } if (capacity <= 0) { throw new ArgumentException("Capacity must be bigger than zero", "capacity"); } if (rate > capacity) { throw new ArgumentException("Rate must be bigger than capacity", "rate"); } LeakyBucketRequestThrottler ret = null; string key = string.Format("{0}{1}{2}", serviceEndpoint, capacity, rate); if (instances.ContainsKey(key)) { ret = instances[key]; } else { lock (instances) { if (instances.ContainsKey(key)) { ret = instances[key]; } else { MTurkLog.Debug("Throttling requests to {0} (Capacity: {1}. Rate: {2}/sec)", serviceEndpoint, capacity, rate); ret = new LeakyBucketRequestThrottler(serviceEndpoint, capacity, rate); instances[key] = ret; } } } return ret; }