class GeocoderThrottle in Geocoder 8.3
Provides a throttle mecanism for geocoder requests.
Hierarchy
- class \Drupal\geocoder\GeocoderThrottle implements GeocoderThrottleInterface
Expanded class hierarchy of GeocoderThrottle
1 string reference to 'GeocoderThrottle'
1 service uses GeocoderThrottle
File
- src/
GeocoderThrottle.php, line 11
Namespace
Drupal\geocoderView source
class GeocoderThrottle implements GeocoderThrottleInterface {
/**
* The throttle mecanism.
*
* @var \Stiphle\Throttle\LeakyBucket
*/
protected $throttle;
/**
* Constructs a new throttle service.
*
* The storage must be instantiated once and reused to work correctly.
*/
public function __construct() {
$this->throttle = new LeakyBucket();
// TODO For now, we use a per-process storage, which means that requests
// sent at the same time by another process (like another user on the
// website) will be throttled separately, so that the actual limit of the
// provider could still be reached.
// In common use cases, it's not a problem because bulk geocoding is handled
// by one process, such as a drush command.
// But it could be improved by using a more shared and persistent storage
// that would fit more use cases.
$this->throttle
->setStorage(new Process());
}
/**
* {@inheritdoc}
*/
public function waitForAvailability(string $key, array $throttle_info = []) {
// Use throttle info if set.
if (isset($throttle_info['limit']) && isset($throttle_info['period'])) {
// The throttle mechanism uses milliseconds, so we convert the argument
// and convert back the result as sleep() uses seconds.
$time_to_wait = $this->throttle
->throttle($key, $throttle_info['limit'], $throttle_info['period'] * 1000);
sleep($time_to_wait / 1000);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GeocoderThrottle:: |
protected | property | The throttle mecanism. | |
GeocoderThrottle:: |
public | function |
Sleeps until the throttle rate is not reached anymore. Overrides GeocoderThrottleInterface:: |
|
GeocoderThrottle:: |
public | function | Constructs a new throttle service. |