From 541993c06bc62d52842f0397ca4052fa3254c573 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 15 Aug 2017 22:57:20 +0300 Subject: [PATCH] Implement throttle for API calls --- platformio/util.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/platformio/util.py b/platformio/util.py index 12a0f5be..07b2b33d 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -23,12 +23,13 @@ import stat import subprocess import sys from contextlib import contextmanager +from functools import wraps from glob import glob from os.path import (abspath, basename, dirname, expanduser, isdir, isfile, join, normpath, splitdrive) from shutil import rmtree from threading import Thread -from time import sleep +from time import sleep, time import click import requests @@ -149,6 +150,25 @@ class memoized(object): self.cache = {} +class throttle(object): + + def __init__(self, limit): + self.limit = limit # milliseconds + self.last = 0 + + def __call__(self, fn): + + @wraps(fn) + def wrapper(*args, **kwargs): + diff = int(round((time() - self.last) * 1000)) + if diff < self.limit: + sleep((self.limit - diff) * 0.001) + self.last = time() + return fn(*args, **kwargs) + + return wrapper + + def singleton(cls): """ From PEP-318 http://www.python.org/dev/peps/pep-0318/#examples """ _instances = {} @@ -336,8 +356,8 @@ def parse_conf_multi_values(items): ] -def change_filemtime(path, time): - os.utime(path, (time, time)) +def change_filemtime(path, mtime): + os.utime(path, (mtime, mtime)) def is_ci(): @@ -471,6 +491,7 @@ def _api_request_session(): return requests.Session() +@throttle(500) def _get_api_result( url, # pylint: disable=too-many-branches params=None,