Skip to content
Browse files

Fix to empty a socket after sending releaselock.

With this, the subsequent part won't receive two messages at a time.
Also changed to use fileobject to read line from socket.
  • Loading branch information...
1 parent aea133b commit 8beac65ba5b1a630f56ca6b486ae6b0da129888c Satoshi Tanimoto committed Jun 4, 2012
Showing with 12 additions and 8 deletions.
  1. +12 −8 ddlockclient/__init__.py
View
20 ddlockclient/__init__.py
@@ -39,7 +39,9 @@ def fail(msg):
sock = self.client.get_sock(addr)
if not sock:
continue
- sock.send("releaselock lock=%s\r\n" % eurl(self.name))
+ sock['socket'].send("releaselock lock=%s\r\n" \
+ % eurl(self.name))
+ sock['file'].readline()
raise DDLockError(msg)
for server in servers:
@@ -52,8 +54,8 @@ def fail(msg):
if not sock:
continue
- sock.send("trylock lock=%s\r\n" % eurl(self.name))
- data = sock.recv(1024)
+ sock['socket'].send("trylock lock=%s\r\n" % eurl(self.name))
+ data = sock['file'].readline()
if not re.search(r'^ok\b', data, re.I):
fail("%s: '%s' %s\n" % (server, self.name, repr(data)))
@@ -73,13 +75,14 @@ def release(self):
continue
data = None
try:
- sock.send("releaselock lock=%s\r\n" % eurl(self.name))
- data = sock.recv(1024)
+ sock['socket'].send("releaselock lock=%s\r\n" \
+ % eurl(self.name))
+ data = sock['file'].readline()
except:
pass
if data and not re.search(r'^ok\b', data, re.I):
- raise DDLockError("releaselock (%s): %s" % (sock.getpeername(),
- repr(data)))
+ raise DDLockError("releaselock (%s): %s" \
+ % (sock['socket'].getpeername(), repr(data)))
count += 1
return count
@@ -117,14 +120,15 @@ def get_sock(self, addr):
port = int(host_port[1]) if len(host_port) > 1 else DEFAULT_PORT
sock = self.sockcache.get("%s:%s" % (host, port))
- if sock and sock.getpeername():
+ if sock and sock['socket'].getpeername():
return sock
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(1)
sock.connect((host, port))
+ sock = {'socket': sock, 'file': sock.makefile()}
self.sockcache[addr] = sock
return sock

0 comments on commit 8beac65

Please sign in to comment.
Something went wrong with that request. Please try again.