tests/wireprotosimplecache.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 04 Sep 2019 03:20:55 +0200
changeset 43037 142deb539ccf
parent 42814 2c4f656c8e9f
child 43076 2372284d9457
permissions -rw-r--r--
sidedata: register the flag processors if the repository allows for it Differential Revision: https://phab.mercurial-scm.org/D6893
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# wireprotosimplecache.py - Extension providing in-memory wire protocol cache
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
from __future__ import absolute_import
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
from mercurial import (
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
    extensions,
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
    registrar,
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
    util,
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    14
    wireprotoserver,
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
    wireprototypes,
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
    wireprotov2server,
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
)
42813
268662aac075 interfaces: create a new folder for interfaces and move repository.py in it
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40218
diff changeset
    18
from mercurial.interfaces import (
268662aac075 interfaces: create a new folder for interfaces and move repository.py in it
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40218
diff changeset
    19
    repository,
42814
2c4f656c8e9f interfaceutil: move to interfaces/
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42813
diff changeset
    20
    util as interfaceutil,
42813
268662aac075 interfaces: create a new folder for interfaces and move repository.py in it
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40218
diff changeset
    21
)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
from mercurial.utils import (
40023
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
    23
    stringutil,
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
CACHE = None
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
configtable = {}
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
configitem = registrar.configitem(configtable)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    31
configitem(b'simplecache', b'cacheapi',
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    32
           default=False)
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    33
configitem(b'simplecache', b'cacheobjects',
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
           default=False)
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    35
configitem(b'simplecache', b'redirectsfile',
40023
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
    36
           default=None)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    38
# API handler that makes cached keys available.
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    39
def handlecacherequest(rctx, req, res, checkperm, urlparts):
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    40
    if rctx.repo.ui.configbool(b'simplecache', b'cacheobjects'):
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    41
        res.status = b'500 Internal Server Error'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    42
        res.setbodybytes(b'cacheobjects not supported for api server')
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    43
        return
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    44
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    45
    if not urlparts:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    46
        res.status = b'200 OK'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    47
        res.headers[b'Content-Type'] = b'text/plain'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    48
        res.setbodybytes(b'simple cache server')
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    49
        return
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    50
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    51
    key = b'/'.join(urlparts)
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    52
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    53
    if key not in CACHE:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    54
        res.status = b'404 Not Found'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    55
        res.headers[b'Content-Type'] = b'text/plain'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    56
        res.setbodybytes(b'key not found in cache')
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    57
        return
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    58
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    59
    res.status = b'200 OK'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    60
    res.headers[b'Content-Type'] = b'application/mercurial-cbor'
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    61
    res.setbodybytes(CACHE[key])
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    62
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    63
def cachedescriptor(req, repo):
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    64
    return {}
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    65
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    66
wireprotoserver.API_HANDLERS[b'simplecache'] = {
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    67
    b'config': (b'simplecache', b'cacheapi'),
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    68
    b'handler': handlecacherequest,
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    69
    b'apidescriptor': cachedescriptor,
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    70
}
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    71
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
@interfaceutil.implementer(repository.iwireprotocolcommandcacher)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
class memorycacher(object):
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    74
    def __init__(self, ui, command, encodefn, redirecttargets, redirecthashes,
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    75
                 req):
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
        self.ui = ui
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
        self.encodefn = encodefn
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    78
        self.redirecttargets = redirecttargets
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    79
        self.redirecthashes = redirecthashes
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
    80
        self.req = req
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
        self.key = None
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    82
        self.cacheobjects = ui.configbool(b'simplecache', b'cacheobjects')
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    83
        self.cacheapi = ui.configbool(b'simplecache', b'cacheapi')
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        self.buffered = []
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    86
        ui.log(b'simplecache', b'cacher constructed for %s\n', command)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
    def __enter__(self):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
        return self
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
    def __exit__(self, exctype, excvalue, exctb):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
        if exctype:
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
    93
            self.ui.log(b'simplecache', b'cacher exiting due to error\n')
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    95
    def adjustcachekeystate(self, state):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
        # Needed in order to make tests deterministic. Don't copy this
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
        # pattern for production caches!
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
        del state[b'repo']
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
    def setcachekey(self, key):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
        self.key = key
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
        return True
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
    def lookup(self):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
        if self.key not in CACHE:
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   106
            self.ui.log(b'simplecache', b'cache miss for %s\n', self.key)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   107
            return None
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
        entry = CACHE[self.key]
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   110
        self.ui.log(b'simplecache', b'cache hit for %s\n', self.key)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   112
        redirectable = True
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   113
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   114
        if not self.cacheapi:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   115
            redirectable = False
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   116
        elif not self.redirecttargets:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   117
            redirectable = False
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   118
        else:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   119
            clienttargets = set(self.redirecttargets)
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   120
            ourtargets = set(t[b'name'] for t in loadredirecttargets(self.ui))
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   121
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   122
            # We only ever redirect to a single target (for now). So we don't
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   123
            # need to store which target matched.
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   124
            if not clienttargets & ourtargets:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   125
                redirectable = False
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   126
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   127
        if redirectable:
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   128
            paths = self.req.dispatchparts[:-3]
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   129
            paths.append(b'simplecache')
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   130
            paths.append(self.key)
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   131
40170
f7ff5b4fe745 tests: use baseurl instead of advertisedbaseurl
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40025
diff changeset
   132
            url = b'%s/%s' % (self.req.baseurl, b'/'.join(paths))
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   133
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   134
            #url = b'http://example.com/%s' % self.key
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   135
            self.ui.log(b'simplecache', b'sending content redirect for %s to '
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   136
                                        b'%s\n', self.key, url)
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   137
            response = wireprototypes.alternatelocationresponse(
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   138
                url=url,
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   139
                mediatype=b'application/mercurial-cbor')
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   140
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   141
            return {b'objs': [response]}
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   142
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   143
        if self.cacheobjects:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
            return {
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   145
                b'objs': entry,
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   146
            }
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   147
        else:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   148
            return {
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   149
                b'objs': [wireprototypes.encodedresponse(entry)],
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   150
            }
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   151
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   152
    def onobject(self, obj):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   153
        if self.cacheobjects:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   154
            self.buffered.append(obj)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   155
        else:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
            self.buffered.extend(self.encodefn(obj))
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   157
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   158
        yield obj
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   159
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   160
    def onfinished(self):
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   161
        self.ui.log(b'simplecache', b'storing cache entry for %s\n', self.key)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   162
        if self.cacheobjects:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   163
            CACHE[self.key] = self.buffered
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   164
        else:
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   165
            CACHE[self.key] = b''.join(self.buffered)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   166
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   167
        return []
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   168
40025
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   169
def makeresponsecacher(orig, repo, proto, command, args, objencoderfn,
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   170
                       redirecttargets, redirecthashes):
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   171
    return memorycacher(repo.ui, command, objencoderfn, redirecttargets,
b099e6032f38 wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40023
diff changeset
   172
                        redirecthashes, proto._req)
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   173
40023
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   174
def loadredirecttargets(ui):
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   175
    path = ui.config(b'simplecache', b'redirectsfile')
40023
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   176
    if not path:
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   177
        return []
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   178
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   179
    with open(path, 'rb') as fh:
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   180
        s = fh.read()
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   181
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   182
    return stringutil.evalpythonliteral(s)
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   183
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   184
def getadvertisedredirecttargets(orig, repo, proto):
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   185
    return loadredirecttargets(repo.ui)
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   186
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   187
def extsetup(ui):
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   188
    global CACHE
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   189
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   190
    CACHE = util.lrucachedict(10000)
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   191
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   192
    extensions.wrapfunction(wireprotov2server, b'makeresponsecacher',
40021
c537144fdbef wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   193
                            makeresponsecacher)
40218
b27bcf81071b py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40170
diff changeset
   194
    extensions.wrapfunction(wireprotov2server, b'getadvertisedredirecttargets',
40023
10cf8b116dd8 wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40021
diff changeset
   195
                            getadvertisedredirecttargets)