[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[minion-cvs] Improve client flock() situation



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria.mit.edu:/tmp/cvs-serv22538/lib/mixminion

Modified Files:
	ClientMain.py Common.py 
Log Message:
Improve client flock() situation

Index: ClientMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/ClientMain.py,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -d -r1.61 -r1.62
--- ClientMain.py	17 Feb 2003 14:40:34 -0000	1.61
+++ ClientMain.py	17 Feb 2003 15:02:25 -0000	1.62
@@ -48,8 +48,13 @@
 def clientLock():
     """Acquire the client lock."""
     assert _CLIENT_LOCKFILE is not None
-    _CLIENT_LOCKFILE.acquire(blocking=1)
-
+    pidStr = str(os.getpid())
+    try:
+        _CLIENT_LOCKFILE.acquire(blocking=0, contents=pidStr)
+    except IOError:
+        LOG.info("Waiting for pid %s", _CLIENT_LOCKFILE.getContents())
+        _CLIENT_LOCKFILE.acquire(blocking=1, contents=pidStr)
+    
 def clientUnlock():
     """Release the client lock."""    
     _CLIENT_LOCKFILE.release()
@@ -1207,22 +1212,28 @@
         """Insert the 32K packet 'message' (to be delivered to 'routing')
            into the pool.  Return the handle of the newly inserted packet."""
         clientLock()
-        f, handle = self.prng.openNewFile(self.dir, "pkt_", 1)
-        cPickle.dump(("PACKET-0", message, routing,
-                      previousMidnight(time.time())), f, 1)
-        f.close()
-        return handle
+        try:
+            f, handle = self.prng.openNewFile(self.dir, "pkt_", 1)
+            cPickle.dump(("PACKET-0", message, routing,
+                          previousMidnight(time.time())), f, 1)
+            f.close()
+            return handle
+        finally:
+            clientUnlock()
     
     def getHandles(self):
         """Return a list of the handles of all messages currently in the
            pool."""
         clientLock()
-        fnames = os.listdir(self.dir)
-        handles = []
-        for fname in fnames:
-            if fname.startswith("pkt_"):
-                handles.append(fname[4:])
-        return handles
+        try:
+            fnames = os.listdir(self.dir)
+            handles = []
+            for fname in fnames:
+                if fname.startswith("pkt_"):
+                    handles.append(fname[4:])
+                return handles
+        finally:
+            clientUnlock()
 
     def getPacket(self, handle):
         """Given a handle, return a 3-tuple of the corresponding
@@ -1389,8 +1400,7 @@
         #XXXX004 write unit tests
         if now is None:
             now = time.time()
-        clientLock()
-        surbLog = self.openSURBLog()
+        surbLog = self.openSURBLog() # implies lock
         try:
             surb = surbLog.findUnusedSURB(surbList, verbose=1, now=now)
             if surb is None:
@@ -1403,8 +1413,7 @@
             surbLog.markSURBUsed(surb)
             return msg, servers[0]
         finally:
-            surbLog.close()
-            clientUnlock()
+            surbLog.close() #implies unlock
 
     def openSURBLog(self):
         """Return a new, open SURBLog object for this client; it must be closed

Index: Common.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Common.py,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -d -r1.62 -r1.63
--- Common.py	16 Feb 2003 04:50:55 -0000	1.62
+++ Common.py	17 Feb 2003 15:02:25 -0000	1.63
@@ -919,6 +919,14 @@
         self.count = 0
         self.fd = None
 
+    def getContents(self):
+        """Return the contents of the lock file."""
+        try:
+            f = open(self.filename, 'r')
+            return f.read()
+        finally:
+            f.close()
+
     def acquire(self, contents="", blocking=0):
         """Acquire this lock.  If we're acquiring the lock for the first time,
            write 'contents' to the lockfile.  If 'blocking' is true, wait until