author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Tue, 26 Mar 2019 14:04:33 +0100 | |
changeset 42031 | d31d8c5279c6 |
parent 40218 | b27bcf81071b |
child 42813 | 268662aac075 |
permissions | -rw-r--r-- |
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 |
repository, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
util, |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
15 |
wireprotoserver, |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 |
wireprototypes, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
wireprotov2server, |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 |
) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 |
from mercurial.utils import ( |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 |
interfaceutil, |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
21 |
stringutil, |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 |
) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 |
CACHE = None |
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 |
configtable = {} |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 |
configitem = registrar.configitem(configtable) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
29 |
configitem(b'simplecache', b'cacheapi', |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
30 |
default=False) |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
31 |
configitem(b'simplecache', b'cacheobjects', |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
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'redirectsfile', |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
34 |
default=None) |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
36 |
# 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
|
37 |
def handlecacherequest(rctx, req, res, checkperm, urlparts): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
42 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
43 |
if not urlparts: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
44 |
res.status = b'200 OK' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
45 |
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
|
46 |
res.setbodybytes(b'simple cache server') |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
47 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
48 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
49 |
key = b'/'.join(urlparts) |
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 |
if key not in CACHE: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
52 |
res.status = b'404 Not Found' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
56 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
57 |
res.status = b'200 OK' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
58 |
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
|
59 |
res.setbodybytes(CACHE[key]) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
60 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
61 |
def cachedescriptor(req, repo): |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
62 |
return {} |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
63 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
64 |
wireprotoserver.API_HANDLERS[b'simplecache'] = { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
65 |
b'config': (b'simplecache', b'cacheapi'), |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
66 |
b'handler': handlecacherequest, |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
67 |
b'apidescriptor': cachedescriptor, |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
68 |
} |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
69 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 |
@interfaceutil.implementer(repository.iwireprotocolcommandcacher) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
class memorycacher(object): |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
72 |
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
|
73 |
req): |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 |
self.ui = ui |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 |
self.encodefn = encodefn |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
76 |
self.redirecttargets = redirecttargets |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
77 |
self.redirecthashes = redirecthashes |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
78 |
self.req = req |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 |
self.key = None |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
80 |
self.cacheobjects = ui.configbool(b'simplecache', b'cacheobjects') |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
81 |
self.cacheapi = ui.configbool(b'simplecache', b'cacheapi') |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
self.buffered = [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
84 |
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
|
85 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
86 |
def __enter__(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 |
return self |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 |
def __exit__(self, exctype, excvalue, exctb): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 |
if exctype: |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
91 |
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
|
92 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 |
def adjustcachekeystate(self, state): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 |
# 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
|
95 |
# pattern for production caches! |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 |
del state[b'repo'] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
97 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 |
def setcachekey(self, key): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 |
self.key = key |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 |
return True |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
101 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
102 |
def lookup(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
103 |
if self.key not in CACHE: |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
104 |
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
|
105 |
return None |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
106 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
107 |
entry = CACHE[self.key] |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
108 |
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
|
109 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
110 |
redirectable = True |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
111 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
112 |
if not self.cacheapi: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
113 |
redirectable = False |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
114 |
elif not self.redirecttargets: |
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 |
else: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
117 |
clienttargets = set(self.redirecttargets) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
118 |
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
|
119 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
120 |
# 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
|
121 |
# need to store which target matched. |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
122 |
if not clienttargets & ourtargets: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
123 |
redirectable = False |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
124 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
125 |
if redirectable: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
126 |
paths = self.req.dispatchparts[:-3] |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
127 |
paths.append(b'simplecache') |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
128 |
paths.append(self.key) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
129 |
|
40170
f7ff5b4fe745
tests: use baseurl instead of advertisedbaseurl
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
130 |
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
|
131 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
132 |
#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
|
133 |
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
|
134 |
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
|
135 |
response = wireprototypes.alternatelocationresponse( |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
136 |
url=url, |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
137 |
mediatype=b'application/mercurial-cbor') |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
138 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
139 |
return {b'objs': [response]} |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
140 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
141 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
142 |
return { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
143 |
b'objs': entry, |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
144 |
} |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
145 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
146 |
return { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
147 |
b'objs': [wireprototypes.encodedresponse(entry)], |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
148 |
} |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
149 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
150 |
def onobject(self, obj): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
151 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
152 |
self.buffered.append(obj) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
153 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
154 |
self.buffered.extend(self.encodefn(obj)) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
155 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
156 |
yield 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 |
def onfinished(self): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
159 |
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
|
160 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
161 |
CACHE[self.key] = self.buffered |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
162 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
163 |
CACHE[self.key] = b''.join(self.buffered) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
164 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
165 |
return [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
166 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
167 |
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
|
168 |
redirecttargets, redirecthashes): |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
169 |
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
|
170 |
redirecthashes, proto._req) |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
171 |
|
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
172 |
def loadredirecttargets(ui): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
173 |
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
|
174 |
if not path: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
175 |
return [] |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
176 |
|
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
177 |
with open(path, 'rb') as fh: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
178 |
s = fh.read() |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
179 |
|
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
180 |
return stringutil.evalpythonliteral(s) |
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 |
def getadvertisedredirecttargets(orig, repo, proto): |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
183 |
return loadredirecttargets(repo.ui) |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
184 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
185 |
def extsetup(ui): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
186 |
global CACHE |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
187 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
188 |
CACHE = util.lrucachedict(10000) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
189 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
190 |
extensions.wrapfunction(wireprotov2server, b'makeresponsecacher', |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
191 |
makeresponsecacher) |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
192 |
extensions.wrapfunction(wireprotov2server, b'getadvertisedredirecttargets', |
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
193 |
getadvertisedredirecttargets) |