author | Raphaël Gomès <rgomes@octobus.net> |
Tue, 29 Dec 2020 10:53:45 +0100 | |
changeset 46187 | 95d6f31e88db |
parent 44452 | 9d2b2df2c2ba |
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 |
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 |
) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
22 |
from mercurial.utils import stringutil |
40021
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 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
29 |
configitem(b'simplecache', b'cacheapi', default=False) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
30 |
configitem(b'simplecache', b'cacheobjects', default=False) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
31 |
configitem(b'simplecache', b'redirectsfile', default=None) |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
33 |
# 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
|
34 |
def handlecacherequest(rctx, req, res, checkperm, urlparts): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
39 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
40 |
if not urlparts: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
41 |
res.status = b'200 OK' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
42 |
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
|
43 |
res.setbodybytes(b'simple cache server') |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
44 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
45 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
46 |
key = b'/'.join(urlparts) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
47 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
48 |
if key not in CACHE: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
49 |
res.status = b'404 Not Found' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
return |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
53 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
54 |
res.status = b'200 OK' |
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'application/mercurial-cbor' |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
56 |
res.setbodybytes(CACHE[key]) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
57 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
58 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
59 |
def cachedescriptor(req, repo): |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
60 |
return {} |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
61 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
62 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
63 |
wireprotoserver.API_HANDLERS[b'simplecache'] = { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
64 |
b'config': (b'simplecache', b'cacheapi'), |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
65 |
b'handler': handlecacherequest, |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
66 |
b'apidescriptor': cachedescriptor, |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
67 |
} |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
68 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
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): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
72 |
def __init__( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
73 |
self, ui, command, encodefn, redirecttargets, redirecthashes, req |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
74 |
): |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 |
self.ui = ui |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 |
self.encodefn = encodefn |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
77 |
self.redirecttargets = redirecttargets |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
78 |
self.redirecthashes = redirecthashes |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
79 |
self.req = req |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 |
self.key = None |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
81 |
self.cacheobjects = ui.configbool(b'simplecache', b'cacheobjects') |
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
82 |
self.cacheapi = ui.configbool(b'simplecache', b'cacheapi') |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 |
self.buffered = [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
84 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
85 |
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
|
86 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 |
def __enter__(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 |
return self |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 |
def __exit__(self, exctype, excvalue, exctb): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
if exctype: |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
92 |
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
|
93 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 |
def adjustcachekeystate(self, state): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 |
# 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
|
96 |
# pattern for production caches! |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
97 |
del state[b'repo'] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 |
def setcachekey(self, key): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 |
self.key = key |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
101 |
return True |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
102 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
103 |
def lookup(self): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
104 |
if self.key not in CACHE: |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
105 |
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
|
106 |
return None |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
107 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
108 |
entry = CACHE[self.key] |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
109 |
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
|
110 |
|
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
111 |
redirectable = True |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
112 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
113 |
if not self.cacheapi: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
114 |
redirectable = False |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
115 |
elif not self.redirecttargets: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
116 |
redirectable = False |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
117 |
else: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
118 |
clienttargets = set(self.redirecttargets) |
44452
9d2b2df2c2ba
cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
119 |
ourtargets = {t[b'name'] for t in loadredirecttargets(self.ui)} |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
120 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
121 |
# 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
|
122 |
# need to store which target matched. |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
123 |
if not clienttargets & ourtargets: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
124 |
redirectable = False |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
125 |
|
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
126 |
if redirectable: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
127 |
paths = self.req.dispatchparts[:-3] |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
128 |
paths.append(b'simplecache') |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
129 |
paths.append(self.key) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
130 |
|
40170
f7ff5b4fe745
tests: use baseurl instead of advertisedbaseurl
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
131 |
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
|
132 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
133 |
# url = b'http://example.com/%s' % self.key |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
134 |
self.ui.log( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
135 |
b'simplecache', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
136 |
b'sending content redirect for %s to ' b'%s\n', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
137 |
self.key, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
138 |
url, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
139 |
) |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
140 |
response = wireprototypes.alternatelocationresponse( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
141 |
url=url, mediatype=b'application/mercurial-cbor' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
142 |
) |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
143 |
|
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
144 |
return {b'objs': [response]} |
40025
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40023
diff
changeset
|
145 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
146 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
147 |
return { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
148 |
b'objs': entry, |
40021
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 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
151 |
return { |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
152 |
b'objs': [wireprototypes.encodedresponse(entry)], |
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
153 |
} |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
154 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
155 |
def onobject(self, obj): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
156 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
157 |
self.buffered.append(obj) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
158 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
159 |
self.buffered.extend(self.encodefn(obj)) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
160 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
161 |
yield obj |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
162 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
163 |
def onfinished(self): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
164 |
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
|
165 |
if self.cacheobjects: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
166 |
CACHE[self.key] = self.buffered |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
167 |
else: |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
168 |
CACHE[self.key] = b''.join(self.buffered) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
169 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
170 |
return [] |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
171 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
172 |
|
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
173 |
def makeresponsecacher( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
174 |
orig, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
175 |
repo, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
176 |
proto, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
177 |
command, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
178 |
args, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
179 |
objencoderfn, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
180 |
redirecttargets, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
181 |
redirecthashes, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
182 |
): |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
183 |
return memorycacher( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
184 |
repo.ui, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
185 |
command, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
186 |
objencoderfn, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
187 |
redirecttargets, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
188 |
redirecthashes, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
189 |
proto._req, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
190 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
191 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
192 |
|
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
193 |
def loadredirecttargets(ui): |
40218
b27bcf81071b
py3: byteify tests/wireprotosimplecache.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents:
40170
diff
changeset
|
194 |
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
|
195 |
if not path: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
196 |
return [] |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
197 |
|
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
198 |
with open(path, 'rb') as fh: |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
199 |
s = fh.read() |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
200 |
|
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
201 |
return stringutil.evalpythonliteral(s) |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
202 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
203 |
|
40023
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
204 |
def getadvertisedredirecttargets(orig, repo, proto): |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
205 |
return loadredirecttargets(repo.ui) |
10cf8b116dd8
wireprotov2: advertise redirect targets in capabilities
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
206 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
207 |
|
40021
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
208 |
def extsetup(ui): |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
209 |
global CACHE |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
210 |
|
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
211 |
CACHE = util.lrucachedict(10000) |
c537144fdbef
wireprotov2: support response caching
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
212 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
213 |
extensions.wrapfunction( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
214 |
wireprotov2server, b'makeresponsecacher', makeresponsecacher |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
215 |
) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
216 |
extensions.wrapfunction( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
217 |
wireprotov2server, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
218 |
b'getadvertisedredirecttargets', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
219 |
getadvertisedredirecttargets, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42814
diff
changeset
|
220 |
) |