Permalink
Browse files

fixed the socket behavior

  • Loading branch information...
1 parent eed4d2d commit c9992b455e756ee2b9eb32a3b979bb3966aece93 @tarekziade tarekziade committed Aug 2, 2012
Showing with 57 additions and 39 deletions.
  1. +17 −6 vaurien/handlers.py
  2. +5 −1 vaurien/run.py
  3. +32 −21 vaurien/server.py
  4. +3 −10 vaurien/tests/vaurien.ini
  5. +0 −1 vaurien/util.py
View
@@ -2,7 +2,8 @@
def normal(source, dest, to_backend, name, settings, server):
- dest.sendall(server.get_data(source))
+ request = server.get_data(source)
+ dest.sendall(request)
def delay(source, dest, to_backend, name, settings, server):
@@ -15,12 +16,22 @@ def delay(source, dest, to_backend, name, settings, server):
def errors(source, dest, to_backend, name, settings, server):
"""Throw errors on the socket"""
+ if to_backend:
+ server.get_data(source)
+ # XXX find how to handle errors (which errors should we send)
+ #
+ # depends on the protocol
+ dest.sendall("YEAH")
+
+
+def hang(source, dest, to_backend, name, settings, server):
+ """Reads the packets that have been sent."""
+ # consume the socket and hang
server.get_data(source)
- # XXX find how to handle errors (which errors should we send)
- dest.sendall("YEAH")
+ while True:
+ gevent.sleep(1.)
def blackout(source, dest, to_backend, name, settings, server):
- """just drop the packets that had been sent"""
- # consume the socket. That's it
- server.get_data(source)
+ """Don't do anything -- the sockets get closed"""
+ return
View
@@ -101,7 +101,11 @@ def main():
key = key[len('vaurien.'):]
prefix = 'vaurien.'
- value = getattr(args, key, None)
+ try:
+ value = getattr(args, key)
+ except AttributeError:
+ value = None
+
if value is not None:
settings[prefix + key] = value
View
@@ -1,3 +1,6 @@
+import time
+import errno
+import socket
import gevent
import random
@@ -70,12 +73,22 @@ def initialize_choices(self):
self.handlers[name] = handler
def handle(self, source, address):
+ source.setblocking(0)
dest = create_connection(self.dest)
+ dest.setblocking(0)
handler_name = random.choice(self.choices)
handler = self.handlers[handler_name]
self.statsd_incr(handler_name)
- gevent.spawn(self.weirdify, handler, handler_name, source, dest, True)
- gevent.spawn(self.weirdify, handler, handler_name, dest, source, False)
+ try:
+ back = gevent.spawn(self.weirdify, handler, handler_name, source,
+ dest, True)
+ forth = gevent.spawn(self.weirdify, handler, handler_name, dest,
+ source, False)
+ back.join()
@xni

xni Oct 25, 2012

As I understand, this behaviour is suitable for REST interfaces, query-response protocols. Database connections behaves in other way, after data is send, server may ping client, but proxy is not responding properly, I think.

+ forth.join()
+ finally:
+ source.close()
+ dest.close()
def statsd_incr(self, counter):
if self._statsd:
@@ -91,25 +104,23 @@ def weirdify(self, handler, handler_name, source, dest, to_backend):
"""
self._logger.debug('starting weirdify %s' % to_backend)
try:
- while self.running:
- # chose what we want to do.
- try:
- settings = self.settings.getsection('handler.%s' %
- handler_name)
- handler(source=source, dest=dest, to_backend=to_backend,
- name=handler_name, server=self, settings=settings)
- except ValueError:
- return
+ settings = self.settings.getsection('handlers:%s' %
+ handler_name)
+ handler(source=source, dest=dest, to_backend=to_backend,
+ name=handler_name, server=self, settings=settings)
finally:
- source.close()
- dest.close()
self._logger.debug('exiting weirdify %s' % to_backend)
- def get_data(self, source):
- try:
- data = source.recv(int(self.settings['vaurien.bufsize']))
- if not data:
- raise ValueError
- return data
- except error: # socket.error
- raise ValueError
+ def get_data(self, source, delay=.2):
+ bufsize = int(self.settings['vaurien.bufsize'])
+ data = ''
+ start = time.time()
+
+ while time.time() - start < delay:
+ try:
+ data += source.recv(bufsize)
+ except socket.error, err:
+ if err.args[0] == errno.EWOULDBLOCK:
+ pass
+
+ return data
View
@@ -1,11 +1,4 @@
[vaurien]
-host = 0.0.0.0
-port = 8080
-proxied_host = 0.0.0.0
-proxied_port = 8888
-
-behavior = 10:delay, 80:normal
-
-[handler:delay]
-callable = vaurien.callables.delay
-handlers.delay.sleep = 10
+local = 0.0.0.0:8181
+distant = 0.0.0.0:8080
+behavior = 100:hang
View
@@ -1,5 +1,4 @@
import sys
-
from gevent.socket import gethostbyname

0 comments on commit c9992b4

Please sign in to comment.