# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 2016-03-02 10:10:06
# Node ID ea400a4f32e65ba9b54d9e88918a07185e3e5e6a
# Parent  9fec3cb8d12877c0ff8fac7c9f3b0bce8254bc0a

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 symbolic link is created at the original
address pointing to the new address.

The address is intentionally 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
@@ -35,6 +35,7 @@ Config
 
   [chgserver]
   idletimeout = 3600 # seconds, after which an idle server will exit
+  skiphash = False   # whether to skip config or env change checks
 """
 
 from __future__ import absolute_import
@@ -516,6 +517,9 @@ class _requesthandler(SocketServer.Strea
 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  # default 1 hour
@@ -581,6 +585,7 @@ class chgunixservice(commandserver.unixs
         # 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
@@ -589,9 +594,25 @@ class chgunixservice(commandserver.unixs
         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)
+        util.rename(tempaddress, self.baseaddress)
+
     def _reloadconfig(self, signum, frame):
         self.ui = self.server.ui = _renewui(self.ui)