Reduce overhead of internet predicate. (#595)
* Reduce overhead of internet predicate. * Use 8.8.8.8 instead of google-public-dns-a.google.com save a DNS lookup. * Use time.perf_counter instead of time.time(). Allow user to configure check frequency.
This commit is contained in:
parent
63485566f1
commit
351738f33d
@ -6,6 +6,8 @@ import string
|
|||||||
import inspect
|
import inspect
|
||||||
from threading import Timer, RLock
|
from threading import Timer, RLock
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
def lchop(string, prefix):
|
def lchop(string, prefix):
|
||||||
"""Removes a prefix from string
|
"""Removes a prefix from string
|
||||||
@ -357,8 +359,9 @@ class internet:
|
|||||||
"""
|
"""
|
||||||
Checks for internet connection by connecting to a server.
|
Checks for internet connection by connecting to a server.
|
||||||
|
|
||||||
Used server is determined by the `address` class variable which consists of
|
Used server can be configured via the `address` class variable which expects a tuple in the form
|
||||||
server host name and port number.
|
(hostname/IP, port). The rate at which the connection status is checked
|
||||||
|
can be configured via the `check_frequency` class variable (in seconds).
|
||||||
|
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
|
|
||||||
@ -367,11 +370,24 @@ class internet:
|
|||||||
:py:func:`require`
|
:py:func:`require`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
address = ("google-public-dns-a.google.com", 53)
|
address = ("8.8.8.8", 53)
|
||||||
|
|
||||||
|
check_frequency = 1
|
||||||
|
last_checked = time.perf_counter() - check_frequency
|
||||||
|
status = False
|
||||||
|
|
||||||
def __new__(cls):
|
def __new__(cls):
|
||||||
|
now = time.perf_counter()
|
||||||
|
elapsed = now - internet.last_checked
|
||||||
|
if elapsed > internet.check_frequency:
|
||||||
|
internet.last_checked = now
|
||||||
|
internet.status = internet.check()
|
||||||
|
return internet.status
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def check():
|
||||||
try:
|
try:
|
||||||
socket.create_connection(cls.address, 1).close()
|
socket.create_connection(internet.address, 1).close()
|
||||||
return True
|
return True
|
||||||
except (OSError, socket.gaierror):
|
except (OSError, socket.gaierror):
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user