omnata-plugin-runtime 0.2.79__py3-none-any.whl → 0.2.81__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1711,7 +1711,9 @@ def managed_inbound_processing(concurrency: int):
1711
1711
  )
1712
1712
 
1713
1713
  if self._sync_request.development_mode is True:
1714
- concurrency = 1 # disable concurrency when running in development mode, it interferes with pyvcr
1714
+ concurrency_to_use = 1 # disable concurrency when running in development mode, it interferes with pyvcr
1715
+ else:
1716
+ concurrency_to_use = concurrency
1715
1717
  # if self._sync_request.api_limits is None:
1716
1718
  # raise ValueError('To use the managed_inbound_processing decorator, API constraints must be defined. These can be provided in the response to the connect method')
1717
1719
  if len(method_args) == 0:
@@ -1734,9 +1736,9 @@ def managed_inbound_processing(concurrency: int):
1734
1736
  streams_queue.put(stream)
1735
1737
 
1736
1738
  tasks = []
1737
- logger.info(f"Creating {concurrency} worker(s) for retrieving records")
1739
+ logger.info(f"Creating {concurrency_to_use} worker(s) for retrieving records")
1738
1740
 
1739
- for i in range(concurrency):
1741
+ for i in range(concurrency_to_use):
1740
1742
  # the dataframe/generator was put on the queue, so we remove it from the method args
1741
1743
  task = threading.Thread(
1742
1744
  target=__managed_inbound_processing_worker,
@@ -132,11 +132,19 @@ class ApiLimits(SubscriptableBaseModel):
132
132
  ):
133
133
  longest_wait = rate_limit_state.wait_until
134
134
  for request_rate in self.request_rates:
135
+ print(request_rate)
135
136
  if rate_limit_state.previous_request_timestamps is not None and len(rate_limit_state.previous_request_timestamps) > 0:
137
+ previous_request_timestamps = rate_limit_state.get_relevant_history(request_rate)
138
+ print(f"previous_request_timestamps: {len(previous_request_timestamps)}")
136
139
  request_index = request_rate.request_count - 1
137
- if request_index > len(rate_limit_state.previous_request_timestamps) - 1:
138
- request_index = len(rate_limit_state.previous_request_timestamps) - 1
139
- timestamp_at_horizon = rate_limit_state.previous_request_timestamps[request_index]
140
+ if request_index > len(previous_request_timestamps) - 1:
141
+ continue # we have not yet made enough requests to hit this rate limit
142
+ request_index = len(previous_request_timestamps) - 1
143
+ timestamp_at_horizon = previous_request_timestamps[request_index]
144
+ print("timestamp_at_horizon: " + str(timestamp_at_horizon))
145
+ now = datetime.datetime.now().astimezone(datetime.timezone.utc)
146
+ seconds_since_horizon = (timestamp_at_horizon - now).total_seconds()
147
+ print("seconds_since_horizon: " + str(seconds_since_horizon))
140
148
  next_allowed_request = timestamp_at_horizon + datetime.timedelta(
141
149
  seconds=request_rate.number_of_seconds()
142
150
  )
@@ -257,6 +265,29 @@ class RateLimitState(SubscriptableBaseModel):
257
265
  ts for ts in self.previous_request_timestamps if ts > irrelevance_horizon
258
266
  ]
259
267
 
268
+ def get_relevant_history(self, request_rate: RequestRateLimit = None):
269
+ """
270
+ Returns the previous requests which are relevant to a specific rate limiting window.
271
+ First, we prune out timestamps which are older than the time window.
272
+ We also only need to keep timestamps up until the request count for the rate limit.
273
+ For example:
274
+ - Rate limit is 10 requests per 10 seconds
275
+ - If (somehow, for the sake of argument) there were 20 requests in the last 10 seconds,
276
+ we only need to keep the most recent 10 to base our calculation on
277
+ """
278
+ if request_rate is None:
279
+ return self.previous_request_timestamps
280
+ longest_window_seconds = request_rate.number_of_seconds()
281
+ irrelevance_horizon = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
282
+ seconds=longest_window_seconds
283
+ )
284
+
285
+ pruned = [ts for ts in self.previous_request_timestamps if ts > irrelevance_horizon]
286
+ if len(pruned) > request_rate.request_count:
287
+ return pruned[:request_rate.request_count]
288
+ else:
289
+ return pruned
290
+
260
291
 
261
292
  class RequestRateLimit(SubscriptableBaseModel):
262
293
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omnata-plugin-runtime
3
- Version: 0.2.79
3
+ Version: 0.2.81
4
4
  Summary: Classes and common runtime components for building and running Omnata Plugins
5
5
  Author: James Weakley
6
6
  Author-email: james.weakley@omnata.com
@@ -3,10 +3,10 @@ omnata_plugin_runtime/api.py,sha256=vKq7goVPX5cPQ9CVN9l8RmpJDcqDwS5y9v1IWhWjBbk,
3
3
  omnata_plugin_runtime/configuration.py,sha256=NICse7qMtReIYY4ZCu8ng4QDJ4rFP0g3mZwc8m1Xl54,32247
4
4
  omnata_plugin_runtime/forms.py,sha256=_KqSMQG749wImLKxPZh3B3doTZMbP5jDvF6BhQNkPCM,17375
5
5
  omnata_plugin_runtime/logging.py,sha256=Q6eSqrr3SzwfVAg4r4sV1dlxeNS_PzOtZfieoWUEOZQ,3232
6
- omnata_plugin_runtime/omnata_plugin.py,sha256=gyGZdDBP-qHL3-4WTP4zJwo2w4vdqBtiC2QTnOnbfxU,86008
6
+ omnata_plugin_runtime/omnata_plugin.py,sha256=eshPKK9I4o1kVKXIJoihwNuje-uOnDXA7x5UlzVv_i8,86096
7
7
  omnata_plugin_runtime/plugin_entrypoints.py,sha256=dV_JOEWffdkAkofFYJMAsM_-gE9OpM2-paI2dsh5Rzo,24352
8
- omnata_plugin_runtime/rate_limiting.py,sha256=QO8VB1H8al6a8-ydohUmL0c5JynXG2bulmuPRs2-2-Y,14910
9
- omnata_plugin_runtime-0.2.79.dist-info/LICENSE,sha256=IMF9i4xIpgCADf0U-V1cuf9HBmqWQd3qtI3FSuyW4zE,26526
10
- omnata_plugin_runtime-0.2.79.dist-info/METADATA,sha256=kbh70pWeKBiR_3q7ebH0sqQVoE-pIWd9bLXWHv1T4QU,1086
11
- omnata_plugin_runtime-0.2.79.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
12
- omnata_plugin_runtime-0.2.79.dist-info/RECORD,,
8
+ omnata_plugin_runtime/rate_limiting.py,sha256=Ur_TjtpemAEMQxxoKq4NeR1w0t1OQkyq6ANgbmOGlXQ,16642
9
+ omnata_plugin_runtime-0.2.81.dist-info/LICENSE,sha256=IMF9i4xIpgCADf0U-V1cuf9HBmqWQd3qtI3FSuyW4zE,26526
10
+ omnata_plugin_runtime-0.2.81.dist-info/METADATA,sha256=fskFBfEwVeyauJHnZ2Q8y25R038BrfsLxzBi7mKnmPE,1086
11
+ omnata_plugin_runtime-0.2.81.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
12
+ omnata_plugin_runtime-0.2.81.dist-info/RECORD,,