Yuya Nishihara avatar Yuya Nishihara committed b9437dd

hgext: add option to shut down workers periodically

Comments (0)

Files changed (1)

hgext/chgsupport.py

 
     [chg]
     maxworkers = 2  # the number of the worker processes which run cmdserver
+    #maxrequests = n  # reload the worker after the specified num of requests
 
 If `maxworkers = 0`, it serves the worker functionality on the master process.
 This mode is only available for debugging purpose.
+
+`maxrequests = 1` may be useful if some extension changes global data and
+causes unexpected behavior.
 """
 
-import errno, os, signal, socket, struct, tempfile
+import errno, itertools, os, signal, socket, struct, tempfile
 from mercurial import cmdutil, commands, commandserver, dispatch, error, \
                       extensions, i18n, scmutil, util
 from mercurial.i18n import _
         self._ui = ui
         self._sockpath = sockpath
         self._maxworkers = self._ui.configint('chg', 'maxworkers', 2)
+        self._maxrequests = self._ui.configint('chg', 'maxrequests')
 
     def init(self):
         util.setsignalhandler()
                     master = self._servemaster()
                 if not master:
                     self._initworkerui()
-                    while True:
-                        self._serveworker()
+                    for n in itertools.count(1):
+                        self._serveworker(n)
             except (error.SignalInterrupt, KeyboardInterrupt):
                 if master:
                     self._killworkers()
         self._workerui.fixconfig()
         _wrapui(self._workerui)
 
-    def _serveworker(self):
+    def _serveworker(self, nreqs):
         conn, _addr = self._sock.accept()
         try:
-            self._ui.note('accepted connection (pid=%d)\n' % os.getpid())
+            self._ui.note('accepted connection (pid=%d, nreqs=%d)\n'
+                          % (os.getpid(), nreqs))
             fin = conn.makefile('rb')
             fout = conn.makefile('wb')
             try:
         finally:
             conn.close()
 
+        if nreqs == self._maxrequests:
+            raise TaintedWorkerError('served %d requests' % nreqs)
+
     def _runcmdserver(self, fin, fout):
         ui = self._workerui.copy()
         sv = chgcmdserver(self._ui, ui, nullrepo(ui), fin, fout,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.