Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions pistil/tcp/gevent_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ def run(self):
except:
pass


if hasattr(gevent.core, 'dns_shutdown'):

def init_process(self):
#gevent 0.13 and older doesn't reinitialize dns for us after forking
#here's the workaround
gevent.core.dns_shutdown(fail_requests=1)
gevent.core.dns_init()
super(TcpGeventWorker, self).init_process()

try:
gevent.core
except AttributeError:
pass
else:
if hasattr(gevent.core, 'dns_shutdown'):

def init_process(self):
#gevent 0.13 and older doesn't reinitialize dns for us after forking
#here's the workaround
gevent.core.dns_shutdown(fail_requests=1)
gevent.core.dns_init()
super(TcpGeventWorker, self).init_process()

33 changes: 20 additions & 13 deletions pistil/tcp/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import socket
import sys
import time
import stat

from pistil import util

log = logging.getLogger(__name__)

class BaseSocket(object):

def __init__(self, conf, fd=None):
def __init__(self, addr, conf, fd=None):
self.conf = conf
self.address = util.parse_address(conf.get('address',
('127.0.0.1', 8000)))
self.address = addr
if fd is None:
sock = socket.socket(self.FAMILY, socket.SOCK_STREAM)
else:
Expand Down Expand Up @@ -73,15 +73,21 @@ def __str__(self):
class UnixSocket(BaseSocket):

FAMILY = socket.AF_UNIX
def __init__(self, conf, fd=None):

def __init__(self, addr, conf, fd=None):
if fd is None:
try:
os.remove(conf.address)
except OSError:
pass
super(UnixSocket, self).__init__(conf, fd=fd)

st = os.stat(addr)
except OSError as e:
if e.args[0] != errno.ENOENT:
raise
else:
if stat.S_ISSOCK(st.st_mode):
os.remove(addr)
else:
raise ValueError("%r is not a socket" % addr)
super(UnixSocket, self).__init__(addr, conf, fd=fd)

def __str__(self):
return "unix:%s" % self.address

Expand All @@ -104,7 +110,8 @@ def create_socket(conf):
a TypeError is raised.
"""
# get it only once
addr = conf.get("address", ('127.0.0.1', 8000))
addr = util.parse_address(conf.get('address',
('127.0.0.1', 8000)))

if isinstance(addr, tuple):
if util.is_ipv6(addr[0]):
Expand All @@ -119,7 +126,7 @@ def create_socket(conf):
if 'PISTIL_FD' in os.environ:
fd = int(os.environ.pop('PISTIL_FD'))
try:
return sock_type(conf, fd=fd)
return sock_type(addr, conf, fd=fd)
except socket.error, e:
if e[0] == errno.ENOTCONN:
log.error("PISTIL_FD should refer to an open socket.")
Expand All @@ -132,7 +139,7 @@ def create_socket(conf):

for i in range(5):
try:
return sock_type(conf)
return sock_type(addr, conf)
except socket.error, e:
if e[0] == errno.EADDRINUSE:
log.error("Connection in use: %s", str(addr))
Expand Down