[Twisted-Python] Time out on client connections

Moshe Zadka moshez at zadka.com
Sun Sep 9 05:44:27 MDT 2001


Here's a patch, for your review:
(against 0.10.1, but should apply cleanly)

diff -urN Twisted/twisted/internet/main.py changed/twisted/internet/main.py
--- Twisted/twisted/internet/main.py	Mon Aug 20 14:10:14 2001
+++ changed/twisted/internet/main.py	Sun Sep  9 14:27:01 2001
@@ -179,10 +179,12 @@
             run()
             threadable.dispatcher.disown(self)
 
+tempDelayed = delay.Delayed() # A delay for non-peristent delayed actions
+tempDelayed.ticks = 1
 reads = {}
 writes = {}
 running = None
-delayeds = [task.theScheduler]
+delayeds = [tempDelayed, task.theScheduler]
 if threadable.threaded:
     delayeds.append(threadtask.theScheduler)
 shutdowns = []
diff -urN Twisted/twisted/internet/tcp.py changed/twisted/internet/tcp.py
--- Twisted/twisted/internet/tcp.py	Wed Aug 29 13:13:55 2001
+++ changed/twisted/internet/tcp.py	Sun Sep  9 14:41:32 2001
@@ -115,7 +115,7 @@
 class Client(Connection):
     """A client for TCP (and similiar) sockets.
     """
-    def __init__(self, host, port, protocol):
+    def __init__(self, host, port, protocol, timeout=None):
         """Initialize the client, setting up its socket, and request to connect.
         """
         if host == 'unix':
@@ -143,6 +143,13 @@
         self.doWrite = self.doConnect
         self.doConnect()
         self.logstr = self.protocol.__class__.__name__+",client"
+	if timeout is not None:
+	    main.tempDelayed.later(self.failIfNotConnected, timeout)
+
+    def failIfNotConnected(self):
+        if not self.connected:
+	    self.protocol.connectionFailed()
+	    self.loseConnection()
 
     def createInternetSocket(self):
         """(internal) Create an AF_INET socket.






More information about the Twisted-Python mailing list