Patchwork [4,of,4,RFC] chgcache: add the background preloading thread

login
register
mail settings
Submitter Jun Wu
Date Feb. 9, 2017, 1:41 a.m.
Message ID <79adf3722f1b0f602e94.1486604464@x1c>
Download mbox | patch
Permalink /patch/18359/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - Feb. 9, 2017, 1:41 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1486604289 28800
#      Wed Feb 08 17:38:09 2017 -0800
# Node ID 79adf3722f1b0f602e9461f13fa940a25dbfce56
# Parent  88c498ad9318df115d8408412a38edbac7d92a6a
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 79adf3722f1b
chgcache: add the background preloading thread

This patch adds the background preloading thread that runs in the chg master
process. It reads and drains the IPC channel, but does not actually preload
anything yet.

Patch

diff --git a/hgext/chgcache.py b/hgext/chgcache.py
--- a/hgext/chgcache.py
+++ b/hgext/chgcache.py
@@ -14,4 +14,5 @@  from __future__ import absolute_import
 import socket
 import sys
+import threading
 import time
 
@@ -77,4 +78,5 @@  class socketipc(object):
 
 _ipc = socketipc()
+_repocaches = {} # {repopath: (mtime, cache)}
 
 # -- only used by the forked worker process -----------------------------------
@@ -94,4 +96,19 @@  def _runcommand(orig, self):
     return result
 
+# -- only used by the master process ------------------------------------------
+
+def _backgroundpreloader(interval=0.5):
+    while True:
+        try:
+            atime, path = _ipc.recv().split(' ', 1)
+        except Exception: # format error
+            pass
+        else:
+            if path in _repocaches and _repocaches[path][0] >= atime:
+                # the repo cache is up-to-date
+                continue
+            now = time.time()
+            _repocaches[path] = (now, None)
+
 # -----------------------------------------------------------------------------
 
@@ -109,2 +126,8 @@  def extsetup(ui):
     caps = chgserver.chgcmdserver.capabilities
     caps['runcommand'] = extensions.bind(_runcommand, caps['runcommand'])
+
+    # start the background preloader
+    t = threading.Thread(target=_backgroundpreloader,
+                         name='chgcache-backgroundpreloader')
+    t.daemon = True
+    t.start()