HTTP Proxy : receive call on local socket takes a lot of time

Rémi babedoudi at yahoo.fr
Sat Jul 9 14:55:37 CEST 2011


Hi all,

I am currently working on a HTTP Proxy. For maximum flexibility, I am 
implementing the proxy at a low level : I am using the SocketServer library.

The server itself is very simple:

class MyTCPServer(SocketServer.TCPServer):
     allow_reuse_address = 1

and the handler looks like:

class MyTCPHandler(SocketServer.BaseRequestHandler):

     def handle(self):
         # Prints ip and port
         print "\n####  " + str(self.client_address) + " ####"

         requestParser = HTTPRequestParser()

         while True:
             # Get packet
             data = self.request.recv(4096)

             if data == '':
                 break

             # Parse request packet
             if requestParser.got_new_chunk(data):
                 break

         someStuff = ""
         self.request.send(someStuff)


This is working fine, but I have a small performance issue.
The proxy is targeted by Firefox. Sometimes the first receive call on 
the input socket takes a lot of time (up to 20s sometimes), which should 
not be as this is a communication of two local sockets. This is 
occurring maybe every 30 requests or so.

When looking at the logs I noticed that this weird behavior happens when 
the client port is not contiguous to the previous ones. For example, 
handling the fourth request takes a lot of time:

####  ('127.0.0.1', 49704) ####
####  ('127.0.0.1', 49705) ####
####  ('127.0.0.1', 49706) ####
####  ('127.0.0.1', 49674) ####


Do you have any idea what the problem could be ? I tried to manually 
close self.request request, but I still have the problem. Is it related 
to "allow_reuse_address = 1" ?

Thanks for your help !

Rémi



More information about the Python-list mailing list