Discussion:
[PATCH] Added option to avoid retrying to connect if socket is already in use
Thomas P Jakobsen
2010-01-19 22:18:59 UTC
Permalink
# HG changeset patch
# User Thomas P Jakobsen <***@cs.au.dk>
# Date 1263939336 -3600
# Node ID 0b1e6cf3f57efe5a3b320897da636a6e2ebb2bdf
# Parent efa1983063d62a8115949a7e63d5ad4e5a23a791
Added option to avoid retrying to connect if socket is already in use.

In some situations it turns out to be more convenient to have VIFF
throw an exception rather than keep retrying to connect to a socket
with exponentially increasing delays. This is now possible using the
new command line parameter --no-socket-retry.

diff -r efa1983063d6 -r 0b1e6cf3f57e viff/runtime.py
--- a/viff/runtime.py Thu Jan 14 11:36:13 2010 +0100
+++ b/viff/runtime.py Tue Jan 19 23:15:36 2010 +0100
@@ -525,6 +525,10 @@
help="Collect and print profiling information.")
group.add_option("--track-memory", action="store_true",
help="Track memory usage over time.")
+ group.add_option("--no-socket-retry", action="store_false",
+ dest="socket_retry", default=True,
+ help="Fail rather than keep retrying to connect "
+ "if port is already in use.")

try:
# Using __import__ since we do not use the module, we are
@@ -1060,18 +1064,21 @@
connect = lambda host, port: reactor.connectTCP(host, port, factory)

port = players[id].port
- runtime.port = None
- delay = 2
- while runtime.port is None:
- # We keep trying to listen on the port, but with an
- # exponentially increasing delay between each attempt.
- try:
- runtime.port = listen(port)
- except CannotListenError, e:
- delay *= 1 + rand.random()
- print "Error listening on port %d: %s" % (port, e.socketError[1])
- print "Will try again in %d seconds" % delay
- time.sleep(delay)
+ if options and options.socket_retry:
+ runtime.port = None
+ delay = 2
+ while runtime.port is None:
+ # We keep trying to listen on the port, but with an
+ # exponentially increasing delay between each attempt.
+ try:
+ runtime.port = listen(port)
+ except CannotListenError, e:
+ delay *= 1 + rand.random()
+ print "Error listening on port %d: %s" % (port, e.socketError[1])
+ print "Will try again in %d seconds" % delay
+ time.sleep(delay)
+ else:
+ runtime.port = listen(port)
print "Listening on port %d" % port

for peer_id, player in players.iteritems():
Martin Geisler
2010-01-19 23:11:52 UTC
Permalink
Post by Thomas P Jakobsen
# HG changeset patch
# Date 1263939336 -3600
# Node ID 0b1e6cf3f57efe5a3b320897da636a6e2ebb2bdf
# Parent efa1983063d62a8115949a7e63d5ad4e5a23a791
Added option to avoid retrying to connect if socket is already in use.
In some situations it turns out to be more convenient to have VIFF
throw an exception rather than keep retrying to connect to a socket
with exponentially increasing delays. This is now possible using the
new command line parameter --no-socket-retry.
Sure, that's a nice idea.
Post by Thomas P Jakobsen
diff -r efa1983063d6 -r 0b1e6cf3f57e viff/runtime.py
--- a/viff/runtime.py Thu Jan 14 11:36:13 2010 +0100
+++ b/viff/runtime.py Tue Jan 19 23:15:36 2010 +0100
@@ -525,6 +525,10 @@
help="Collect and print profiling information.")
group.add_option("--track-memory", action="store_true",
help="Track memory usage over time.")
+ group.add_option("--no-socket-retry", action="store_false",
+ dest="socket_retry", default=True,
+ help="Fail rather than keep retrying to connect "
+ "if port is already in use.")
# Using __import__ since we do not use the module, we are
@@ -1060,18 +1064,21 @@
connect = lambda host, port: reactor.connectTCP(host, port, factory)
port = players[id].port
- runtime.port = None
- delay = 2
- # We keep trying to listen on the port, but with an
- # exponentially increasing delay between each attempt.
- runtime.port = listen(port)
You should be able to achieve the same by inserting:

if options and options.no_socket_retry:
raise

(I renamed the destination for the flag)
Post by Thomas P Jakobsen
- delay *= 1 + rand.random()
- print "Error listening on port %d: %s" % (port, e.socketError[1])
- print "Will try again in %d seconds" % delay
- time.sleep(delay)
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Thomas P Jakobsen
2010-01-20 07:56:15 UTC
Permalink
     raise
You're right. I'll send a modified patch soon.

Regards,
Thomas
Thomas P Jakobsen
2010-01-20 07:56:34 UTC
Permalink
# HG changeset patch
# User Thomas P Jakobsen <***@cs.au.dk>
# Date 1263974081 -3600
# Node ID 2324d01c74e2c35b5bb6cb4d7bda1304a376b066
# Parent efa1983063d62a8115949a7e63d5ad4e5a23a791
Added option to avoid retrying to connect if socket is already in use.

In some situations it turns out to be more convenient to have VIFF
throw an exception rather than keep retrying to connect to a socket
with exponentially increasing delays. This is now possible using the
new command line parameter --no-socket-retry.

diff -r efa1983063d6 -r 2324d01c74e2 viff/runtime.py
--- a/viff/runtime.py Thu Jan 14 11:36:13 2010 +0100
+++ b/viff/runtime.py Wed Jan 20 08:54:41 2010 +0100
@@ -525,6 +525,9 @@
help="Collect and print profiling information.")
group.add_option("--track-memory", action="store_true",
help="Track memory usage over time.")
+ group.add_option("--no-socket-retry", action="store_true",
+ default=False, help="Fail rather than keep retrying "
+ "to connect if port is already in use.")

try:
# Using __import__ since we do not use the module, we are
@@ -1068,6 +1071,8 @@
try:
runtime.port = listen(port)
except CannotListenError, e:
+ if options and options.no_socket_retry:
+ raise
delay *= 1 + rand.random()
print "Error listening on port %d: %s" % (port, e.socketError[1])
print "Will try again in %d seconds" % delay
Martin Geisler
2010-01-20 11:26:57 UTC
Permalink
Post by Thomas P Jakobsen
# HG changeset patch
# Date 1263974081 -3600
# Node ID 2324d01c74e2c35b5bb6cb4d7bda1304a376b066
# Parent efa1983063d62a8115949a7e63d5ad4e5a23a791
Added option to avoid retrying to connect if socket is already in use.
Nice, please put it in yourself! :-)
--
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
Loading...