sentinelhub.download.rate_limit

Module implementing rate limiting logic for Sentinel Hub service

class sentinelhub.download.rate_limit.PolicyType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Enum defining different types of policies

PROCESSING_UNITS = 'PROCESSING_UNITS'
REQUESTS = 'REQUESTS'
class sentinelhub.download.rate_limit.SentinelHubRateLimit(num_processes=1, minimum_wait_time=0.05, maximum_wait_time=60.0)[source]

Bases: object

Class implementing rate limiting logic of Sentinel Hub service

It has 2 public methods:

  • register_next - tells if next download can start or if not, what is the wait before it can be asked again

  • update - updates expectations according to headers obtained from download

The rate limiting object is collecting information about the status of rate limiting policy buckets from Sentinel Hub service. According to this information and a feedback from download requests it adapts expectations about when the next download attempt will be possible.

Parameters:
  • num_processes (int) – Number of parallel download processes running.

  • minimum_wait_time (float) – Minimum wait time between two consecutive download requests in seconds.

  • maximum_wait_time (float) – Maximum wait time between two consecutive download requests in seconds.

RETRY_HEADER = 'Retry-After'
UNITS_SPENT_HEADER = 'X-ProcessingUnits-Spent'
register_next()[source]

Determines if next download request can start or not by returning the waiting time in seconds.

Return type:

float

update(headers, *, default)[source]

Update the next possible download time if the service has responded with the rate limit.

Parameters:
  • headers (dict) – The headers that (may) contain information about waiting times.

  • default (float) – The default waiting time (in milliseconds) when retrying after getting a TOO_MANY_REQUESTS response without appropriate retry headers.

Return type:

None

class sentinelhub.download.rate_limit.PolicyBucket(policy_type, policy_payload)[source]

Bases: object

A class representing Sentinel Hub policy bucket

Parameters:
  • policy_type (PolicyType) – A type of policy

  • policy_payload (Dict[str, Any]) – A dictionary of policy parameters

count_cost_per_second(elapsed_time, new_content)[source]

Calculates the cost per second for the bucket given the elapsed time and the new content.

In the calculation it assumes that during the elapsed time bucket was being filled all the time - i.e. it assumes the bucket has never been full for a non-zero amount of time in the elapsed time period.

Parameters:
  • elapsed_time (float) –

  • new_content (float) –

Return type:

float

get_wait_time(elapsed_time, process_num, cost_per_request, requests_completed, buffer_cost=0.5)[source]

Expected time a user would have to wait for this bucket

Parameters:
  • elapsed_time (float) –

  • process_num (int) –

  • cost_per_request (float) –

  • requests_completed (int) –

  • buffer_cost (float) –

Return type:

float

is_request_bucket()[source]

Checks if bucket counts requests

Return type:

bool

is_fixed()[source]

Checks if bucket has a fixed number of requests

Return type:

bool