mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Implement throttle for API calls
This commit is contained in:
@ -23,12 +23,13 @@ import stat
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from functools import wraps
|
||||||
from glob import glob
|
from glob import glob
|
||||||
from os.path import (abspath, basename, dirname, expanduser, isdir, isfile,
|
from os.path import (abspath, basename, dirname, expanduser, isdir, isfile,
|
||||||
join, normpath, splitdrive)
|
join, normpath, splitdrive)
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from time import sleep
|
from time import sleep, time
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
@ -149,6 +150,25 @@ class memoized(object):
|
|||||||
self.cache = {}
|
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):
|
def singleton(cls):
|
||||||
""" From PEP-318 http://www.python.org/dev/peps/pep-0318/#examples """
|
""" From PEP-318 http://www.python.org/dev/peps/pep-0318/#examples """
|
||||||
_instances = {}
|
_instances = {}
|
||||||
@ -336,8 +356,8 @@ def parse_conf_multi_values(items):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def change_filemtime(path, time):
|
def change_filemtime(path, mtime):
|
||||||
os.utime(path, (time, time))
|
os.utime(path, (mtime, mtime))
|
||||||
|
|
||||||
|
|
||||||
def is_ci():
|
def is_ci():
|
||||||
@ -471,6 +491,7 @@ def _api_request_session():
|
|||||||
return requests.Session()
|
return requests.Session()
|
||||||
|
|
||||||
|
|
||||||
|
@throttle(500)
|
||||||
def _get_api_result(
|
def _get_api_result(
|
||||||
url, # pylint: disable=too-many-branches
|
url, # pylint: disable=too-many-branches
|
||||||
params=None,
|
params=None,
|
||||||
|
Reference in New Issue
Block a user