[PATCH 01 of 10] chgserver: mangle server address to include confighash

Jun Wu quark at fb.com
Wed Mar 2 10:44:03 UTC 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1456913406 0
#      Wed Mar 02 10:10:06 2016 +0000
# Node ID b8522c5e995a8a49c53ad68cbd215fb50bebfbf8
# Parent  d8d4dccea8d6ad5ba738f14485ed18ec61c2b807
chgserver: mangle server address to include confighash

Before this patch, chgserver will use the address provided by the client. The
new design is one server per confighash. This patch appends "-$confighash" to
the address the client provides. To maintain the compatibility and make sure
the client can connect to the server, a symbol link is created at the original
address pointing to the new address.

The address is intensionally mangled at the server, instead of being pre-
calculated by some other process (eg. a previous server). In this way, we can
avoid file system race conditions.

diff --git a/hgext/chgserver.py b/hgext/chgserver.py
--- a/hgext/chgserver.py
+++ b/hgext/chgserver.py
@@ -505,6 +505,9 @@
 def _tempaddress(address):
     return '%s.%d.tmp' % (address, os.getpid())
 
+def _hashaddress(address, hashstr):
+    return '%s-%s' % (address, hashstr)
+
 class AutoExitMixIn:  # use old-style to comply with SocketServer design
     lastactive = time.time()
     idletimeout = 3600  # defualt 1 hour
@@ -570,6 +573,7 @@
         # drop options set for "hg serve --cmdserver" command
         self.ui.setconfig('progress', 'assume-tty', None)
         signal.signal(signal.SIGHUP, self._reloadconfig)
+        self._inithashstate()
         class cls(AutoExitMixIn, SocketServer.ForkingMixIn,
                   SocketServer.UnixStreamServer):
             ui = self.ui
@@ -578,9 +582,25 @@
         self.server.idletimeout = self.ui.configint(
             'chgserver', 'idletimeout', self.server.idletimeout)
         self.server.startautoexitthread()
+        self._createsymlink()
         # avoid writing "listening at" message to stdout before attachio
         # request, which calls setvbuf()
 
+    def _inithashstate(self):
+        self.baseaddress = self.address
+        if self.ui.configbool('chgserver', 'skiphash', False):
+            self.hashstate = None
+            return
+        self.hashstate = hashstate.fromui(self.ui)
+        self.address = _hashaddress(self.address, self.hashstate.confighash)
+
+    def _createsymlink(self):
+        if self.baseaddress == self.address:
+            return
+        tempaddress = _tempaddress(self.baseaddress)
+        os.symlink(self.address, tempaddress)
+        os.rename(tempaddress, self.baseaddress)
+
     def _reloadconfig(self, signum, frame):
         self.ui = self.server.ui = _renewui(self.ui)
 


More information about the Mercurial-devel mailing list