##// END OF EJS Templates
histedit: py3 fixes for curses mode...
histedit: py3 fixes for curses mode Differential Revision: https://phab.mercurial-scm.org/D8150

File last commit:

r43346:2372284d default
r44892:9b8e13a0 stable
Show More
wireprotosimplecache.py
220 lines | 5.8 KiB | text/x-python | PythonLexer
/ tests / wireprotosimplecache.py
Gregory Szorc
wireprotov2: support response caching...
r40057 # wireprotosimplecache.py - Extension providing in-memory wire protocol cache
#
# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
from mercurial import (
extensions,
registrar,
util,
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 wireprotoserver,
Gregory Szorc
wireprotov2: support response caching...
r40057 wireprototypes,
wireprotov2server,
)
Pulkit Goyal
interfaces: create a new folder for interfaces and move repository.py in it...
r43078 from mercurial.interfaces import (
repository,
Pulkit Goyal
interfaceutil: move to interfaces/...
r43079 util as interfaceutil,
Pulkit Goyal
interfaces: create a new folder for interfaces and move repository.py in it...
r43078 )
Augie Fackler
formatting: blacken the codebase...
r43346 from mercurial.utils import stringutil
Gregory Szorc
wireprotov2: support response caching...
r40057
CACHE = None
configtable = {}
configitem = registrar.configitem(configtable)
Augie Fackler
formatting: blacken the codebase...
r43346 configitem(b'simplecache', b'cacheapi', default=False)
configitem(b'simplecache', b'cacheobjects', default=False)
configitem(b'simplecache', b'redirectsfile', default=None)
Gregory Szorc
wireprotov2: support response caching...
r40057
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 # API handler that makes cached keys available.
def handlecacherequest(rctx, req, res, checkperm, urlparts):
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 if rctx.repo.ui.configbool(b'simplecache', b'cacheobjects'):
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 res.status = b'500 Internal Server Error'
res.setbodybytes(b'cacheobjects not supported for api server')
return
if not urlparts:
res.status = b'200 OK'
res.headers[b'Content-Type'] = b'text/plain'
res.setbodybytes(b'simple cache server')
return
key = b'/'.join(urlparts)
if key not in CACHE:
res.status = b'404 Not Found'
res.headers[b'Content-Type'] = b'text/plain'
res.setbodybytes(b'key not found in cache')
return
res.status = b'200 OK'
res.headers[b'Content-Type'] = b'application/mercurial-cbor'
res.setbodybytes(CACHE[key])
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 def cachedescriptor(req, repo):
return {}
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 wireprotoserver.API_HANDLERS[b'simplecache'] = {
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 b'config': (b'simplecache', b'cacheapi'),
b'handler': handlecacherequest,
b'apidescriptor': cachedescriptor,
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 }
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
wireprotov2: support response caching...
r40057 @interfaceutil.implementer(repository.iwireprotocolcommandcacher)
class memorycacher(object):
Augie Fackler
formatting: blacken the codebase...
r43346 def __init__(
self, ui, command, encodefn, redirecttargets, redirecthashes, req
):
Gregory Szorc
wireprotov2: support response caching...
r40057 self.ui = ui
self.encodefn = encodefn
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 self.redirecttargets = redirecttargets
self.redirecthashes = redirecthashes
self.req = req
Gregory Szorc
wireprotov2: support response caching...
r40057 self.key = None
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 self.cacheobjects = ui.configbool(b'simplecache', b'cacheobjects')
self.cacheapi = ui.configbool(b'simplecache', b'cacheapi')
Gregory Szorc
wireprotov2: support response caching...
r40057 self.buffered = []
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 ui.log(b'simplecache', b'cacher constructed for %s\n', command)
Gregory Szorc
wireprotov2: support response caching...
r40057
def __enter__(self):
return self
def __exit__(self, exctype, excvalue, exctb):
if exctype:
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 self.ui.log(b'simplecache', b'cacher exiting due to error\n')
Gregory Szorc
wireprotov2: support response caching...
r40057
def adjustcachekeystate(self, state):
# Needed in order to make tests deterministic. Don't copy this
# pattern for production caches!
del state[b'repo']
def setcachekey(self, key):
self.key = key
return True
def lookup(self):
if self.key not in CACHE:
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 self.ui.log(b'simplecache', b'cache miss for %s\n', self.key)
Gregory Szorc
wireprotov2: support response caching...
r40057 return None
entry = CACHE[self.key]
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 self.ui.log(b'simplecache', b'cache hit for %s\n', self.key)
Gregory Szorc
wireprotov2: support response caching...
r40057
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 redirectable = True
if not self.cacheapi:
redirectable = False
elif not self.redirecttargets:
redirectable = False
else:
clienttargets = set(self.redirecttargets)
ourtargets = set(t[b'name'] for t in loadredirecttargets(self.ui))
# We only ever redirect to a single target (for now). So we don't
# need to store which target matched.
if not clienttargets & ourtargets:
redirectable = False
if redirectable:
paths = self.req.dispatchparts[:-3]
paths.append(b'simplecache')
paths.append(self.key)
Gregory Szorc
tests: use baseurl instead of advertisedbaseurl...
r40206 url = b'%s/%s' % (self.req.baseurl, b'/'.join(paths))
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061
Augie Fackler
formatting: blacken the codebase...
r43346 # url = b'http://example.com/%s' % self.key
self.ui.log(
b'simplecache',
b'sending content redirect for %s to ' b'%s\n',
self.key,
url,
)
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061 response = wireprototypes.alternatelocationresponse(
Augie Fackler
formatting: blacken the codebase...
r43346 url=url, mediatype=b'application/mercurial-cbor'
)
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 return {b'objs': [response]}
Gregory Szorc
wireprotov2: server support for sending content redirects...
r40061
Gregory Szorc
wireprotov2: support response caching...
r40057 if self.cacheobjects:
return {
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 b'objs': entry,
Gregory Szorc
wireprotov2: support response caching...
r40057 }
else:
return {
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 b'objs': [wireprototypes.encodedresponse(entry)],
Gregory Szorc
wireprotov2: support response caching...
r40057 }
def onobject(self, obj):
if self.cacheobjects:
self.buffered.append(obj)
else:
self.buffered.extend(self.encodefn(obj))
yield obj
def onfinished(self):
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 self.ui.log(b'simplecache', b'storing cache entry for %s\n', self.key)
Gregory Szorc
wireprotov2: support response caching...
r40057 if self.cacheobjects:
CACHE[self.key] = self.buffered
else:
CACHE[self.key] = b''.join(self.buffered)
return []
Augie Fackler
formatting: blacken the codebase...
r43346
def makeresponsecacher(
orig,
repo,
proto,
command,
args,
objencoderfn,
redirecttargets,
redirecthashes,
):
return memorycacher(
repo.ui,
command,
objencoderfn,
redirecttargets,
redirecthashes,
proto._req,
)
Gregory Szorc
wireprotov2: support response caching...
r40057
Gregory Szorc
wireprotov2: advertise redirect targets in capabilities...
r40059 def loadredirecttargets(ui):
Pulkit Goyal
py3: byteify tests/wireprotosimplecache.py...
r40254 path = ui.config(b'simplecache', b'redirectsfile')
Gregory Szorc
wireprotov2: advertise redirect targets in capabilities...
r40059 if not path:
return []
with open(path, 'rb') as fh:
s = fh.read()
return stringutil.evalpythonliteral(s)
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
wireprotov2: advertise redirect targets in capabilities...
r40059 def getadvertisedredirecttargets(orig, repo, proto):
return loadredirecttargets(repo.ui)
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
wireprotov2: support response caching...
r40057 def extsetup(ui):
global CACHE
CACHE = util.lrucachedict(10000)
Augie Fackler
formatting: blacken the codebase...
r43346 extensions.wrapfunction(
wireprotov2server, b'makeresponsecacher', makeresponsecacher
)
extensions.wrapfunction(
wireprotov2server,
b'getadvertisedredirecttargets',
getadvertisedredirecttargets,
)