benoitc/tproxy — Issue #4: patch
Author: Thomas-Mangin
Created: 2011-11-29 15:21:36+00:00
State: open

Hello,

We found two issues when testing tproxy. Please find enclosed a quick patch to each, you may want to reformat or reorganise the code though.

First old __import__(name) syntax was deprecated, the code was changed to use the imp library.
Second The class Worker is created before the fork. On my local version of gevent, it was causing the version in the parent, instead of the child. The new code - only lightly tested - create a small class to store the information required which does not inherit from StreamServer

Regards

Thomas

diff -u /Users/thomas/source/others/proxy/tproxy/tproxy/arbiter.py tproxy/tproxy/arbiter.py
--- /Users/thomas/source/others/proxy/tproxy/tproxy/arbiter.py	2011-11-29 15:10:36.000000000 +0000
+++ tproxy/tproxy/arbiter.py	2011-11-29 14:50:44.000000000 +0000
@@ -19,7 +19,15 @@
from .pidfile import Pidfile
from .proxy import tcp_listener
from .worker import Worker
+from .workertmp import WorkerTmp

+class WorkerInfo(object):
+ def __init__(self, age, ppid, listener, cfg, script,tmp):
+ self.name = cfg.name
+ self.age = age
+ self.ppid = ppid
+ self.cfg = cfg
+ self.tmp = tmp

class HaltServer(Exception):
@@ -386,14 +394,17 @@

def spawn_worker(self):
self.worker_age += 1
- worker = Worker(self.worker_age, self.pid, self.LISTENER, self.cfg,
- self.script)
+ tmpworker = WorkerTmp(self.cfg)
+ worker = WorkerInfo(self.worker_age, self.pid, self.LISTENER, self.cfg,
+ self.script,tmpworker)
pid = os.fork()
if pid != 0:
self.WORKERS[pid] = worker
return

# Process Child
+ worker = Worker(self.worker_age, self.pid, self.LISTENER, self.cfg,
+ self.script,tmpworker)
worker_pid = os.getpid()
try:
self.log.info("Booting worker with pid: %s" % worker_pid)
diff -u /Users/thomas/source/others/proxy/tproxy/tproxy/worker.py tproxy/tproxy/worker.py
--- /Users/thomas/source/others/proxy/tproxy/tproxy/worker.py	2011-11-29 15:10:36.000000000 +0000
+++ tproxy/tproxy/worker.py	2011-11-29 14:50:25.000000000 +0000
@@ -14,7 +14,6 @@

from . import util
from .proxy import ProxyServer
-from .workertmp import WorkerTmp

class Worker(ProxyServer):

@@ -25,7 +24,7 @@

PIPE = []

- def __init__(self, age, ppid, listener, cfg, script):
+ def __init__(self, age, ppid, listener, cfg, script,tmp):
ProxyServer.__init__(self, listener, script,
spawn=Pool(cfg.worker_connections))

@@ -45,7 +44,7 @@
self.age = age
self.ppid = ppid
self.cfg = cfg
- self.tmp = WorkerTmp(cfg)
+ self.tmp = tmp
self.booted = False
self.log = logging.getLogger(__name__)

diff -u /Users/thomas/source/others/proxy/tproxy/tproxy/tools.py tproxy/tproxy/tools.py
--- /Users/thomas/source/others/proxy/tproxy/tproxy/tools.py	2011-11-29 15:10:36.000000000 +0000
+++ tproxy/tproxy/tools.py	2011-11-29 15:14:27.000000000 +0000
@@ -3,6 +3,7 @@
# This file is part of tproxy released under the MIT license.
# See the NOTICE for more information.

+import imp

try:
from importlibe import import_module
@@ -40,5 +41,4 @@
break
level += 1
name = _resolve_name(name[level:], package, level)
- __import__(name)
- return sys.modules[name]
+ return imp.load_source(name,name)