mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-16 06:22:59 +00:00
use the same connection check everywhere
avoids inconsistencies in error handling
This commit is contained in:
@@ -4,9 +4,7 @@
|
|||||||
# Distributed under the terms of the Modified BSD License.
|
# Distributed under the terms of the Modified BSD License.
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import errno
|
|
||||||
import json
|
import json
|
||||||
import socket
|
|
||||||
|
|
||||||
from tornado import gen
|
from tornado import gen
|
||||||
from tornado.log import app_log
|
from tornado.log import app_log
|
||||||
@@ -26,7 +24,7 @@ from sqlalchemy import create_engine
|
|||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
random_port, url_path_join, wait_for_server, wait_for_http_server,
|
random_port, url_path_join, wait_for_server, wait_for_http_server,
|
||||||
new_token, hash_token, compare_token,
|
new_token, hash_token, compare_token, can_connect,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -113,25 +111,7 @@ class Server(Base):
|
|||||||
|
|
||||||
def is_up(self):
|
def is_up(self):
|
||||||
"""Is the server accepting connections?"""
|
"""Is the server accepting connections?"""
|
||||||
try:
|
return can_connect(self.ip or '127.0.0.1', self.port)
|
||||||
socket.create_connection((self.ip or '127.0.0.1', self.port))
|
|
||||||
except socket.error as e:
|
|
||||||
if e.errno == errno.ENETUNREACH:
|
|
||||||
try:
|
|
||||||
socket.create_connection((self.ip or '127.0.0.1', self.port))
|
|
||||||
except socket.error as e:
|
|
||||||
if e.errno == errno.ECONNREFUSED:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
elif e.errno == errno.ECONNREFUSED:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class Proxy(Base):
|
class Proxy(Base):
|
||||||
|
@@ -30,22 +30,32 @@ def random_port():
|
|||||||
ISO8601_ms = '%Y-%m-%dT%H:%M:%S.%fZ'
|
ISO8601_ms = '%Y-%m-%dT%H:%M:%S.%fZ'
|
||||||
ISO8601_s = '%Y-%m-%dT%H:%M:%SZ'
|
ISO8601_s = '%Y-%m-%dT%H:%M:%SZ'
|
||||||
|
|
||||||
|
def can_connect(ip, port):
|
||||||
|
"""Check if we can connect to an ip:port
|
||||||
|
|
||||||
|
return True if we can connect, False otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
socket.create_connection((ip, port))
|
||||||
|
except socket.error as e:
|
||||||
|
if e.errno != errno.ECONNREFUSED:
|
||||||
|
app_log.error("Unexpected error connecting to %s:%i %s",
|
||||||
|
ip, port, e
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def wait_for_server(ip, port, timeout=10):
|
def wait_for_server(ip, port, timeout=10):
|
||||||
"""wait for any server to show up at ip:port"""
|
"""wait for any server to show up at ip:port"""
|
||||||
loop = ioloop.IOLoop.current()
|
loop = ioloop.IOLoop.current()
|
||||||
tic = loop.time()
|
tic = loop.time()
|
||||||
while loop.time() - tic < timeout:
|
while loop.time() - tic < timeout:
|
||||||
try:
|
if can_connect(ip, port):
|
||||||
socket.create_connection((ip, port))
|
|
||||||
except socket.error as e:
|
|
||||||
if e.errno != errno.ECONNREFUSED:
|
|
||||||
app_log.error("Unexpected error waiting for %s:%i %s",
|
|
||||||
ip, port, e
|
|
||||||
)
|
|
||||||
yield gen.sleep(0.1)
|
|
||||||
else:
|
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
yield gen.sleep(0.1)
|
||||||
raise TimeoutError("Server at {ip}:{port} didn't respond in {timeout} seconds".format(
|
raise TimeoutError("Server at {ip}:{port} didn't respond in {timeout} seconds".format(
|
||||||
**locals()
|
**locals()
|
||||||
))
|
))
|
||||||
|
Reference in New Issue
Block a user