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

login
register
mail settings
Submitter Jun Wu
Date March 2, 2016, 10:44 a.m.
Message ID <b8522c5e995a8a49c53a.1456915443@x1c>
Download mbox | patch
Permalink /patch/13522/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - March 2, 2016, 10:44 a.m.
# HG changeset patch
# User Jun Wu <quark@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.
timeless - March 2, 2016, 2:27 p.m.
Jun Wu <quark@fb.com> wrote:
> the client can connect to the server, a symbol link is created at the original

symbolic

> address pointing to the new address.
>
> The address is intensionally mangled at the server, instead of being pre-

intentionally
Yuya Nishihara - March 3, 2016, 2:21 p.m.
On Wed, 2 Mar 2016 10:44:03 +0000, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@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

Reordered the patch 1 and 2 to avoid test failure at this revision, and pushed
to the clowncopter, thanks.

> +    def _createsymlink(self):
> +        if self.baseaddress == self.address:
> +            return
> +        tempaddress = _tempaddress(self.baseaddress)
> +        os.symlink(self.address, tempaddress)
> +        os.rename(tempaddress, self.baseaddress)

Changed to use util.rename().
Yuya Nishihara - March 3, 2016, 4:23 p.m.
On Wed, 2 Mar 2016 10:44:03 +0000, Jun Wu wrote:
> +    def _createsymlink(self):
> +        if self.baseaddress == self.address:
> +            return
> +        tempaddress = _tempaddress(self.baseaddress)
> +        os.symlink(self.address, tempaddress)

FWIW, I prefer making a symlink of relative path to make sure it never escapes
from the socket directory.

Patch

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)