cache can_connect for 3 seconds
This commit is contained in:
parent
647a8b95bc
commit
da30a40fd6
2 changed files with 20 additions and 3 deletions
|
@ -13,12 +13,14 @@ from tornado.ioloop import PeriodicCallback
|
||||||
import settings
|
import settings
|
||||||
import state
|
import state
|
||||||
from tor_request import get_opener
|
from tor_request import get_opener
|
||||||
|
from utils import time_cache
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def can_connect(data):
|
@time_cache(3)
|
||||||
|
def can_connect(**data):
|
||||||
try:
|
try:
|
||||||
opener = get_opener(data['id'])
|
opener = get_opener(data['id'])
|
||||||
headers = {
|
headers = {
|
||||||
|
@ -171,6 +173,6 @@ class LocalNodes(dict):
|
||||||
|
|
||||||
def get_data(self, user_id):
|
def get_data(self, user_id):
|
||||||
data = self.get(user_id)
|
data = self.get(user_id)
|
||||||
if data and can_connect(data):
|
if data and can_connect(**data):
|
||||||
return data
|
return data
|
||||||
return None
|
return None
|
||||||
|
|
15
oml/utils.py
15
oml/utils.py
|
@ -5,6 +5,7 @@ from datetime import datetime
|
||||||
from io import StringIO, BytesIO
|
from io import StringIO, BytesIO
|
||||||
from PIL import Image, ImageFile
|
from PIL import Image, ImageFile
|
||||||
import base64
|
import base64
|
||||||
|
import functools
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
@ -581,3 +582,17 @@ def iexists(path):
|
||||||
|
|
||||||
def same_path(f1, f2):
|
def same_path(f1, f2):
|
||||||
return unicodedata.normalize('NFC', f1) == unicodedata.normalize('NFC', f2)
|
return unicodedata.normalize('NFC', f1) == unicodedata.normalize('NFC', f2)
|
||||||
|
|
||||||
|
def time_cache(max_age, maxsize=128, typed=False):
|
||||||
|
def _decorator(fn):
|
||||||
|
@functools.lru_cache(maxsize=maxsize, typed=typed)
|
||||||
|
def _new(*args, __time_salt, **kwargs):
|
||||||
|
return fn(*args, **kwargs)
|
||||||
|
|
||||||
|
@functools.wraps(fn)
|
||||||
|
def _wrapped(*args, **kwargs):
|
||||||
|
return _new(*args, **kwargs, __time_salt=int(time.time() / max_age))
|
||||||
|
|
||||||
|
return _wrapped
|
||||||
|
|
||||||
|
return _decorator
|
||||||
|
|
Loading…
Reference in a new issue