Mercurial > hg
annotate tests/wireprotosimplecache.py @ 44950:f9734b2d59cc
py3: make stdout line-buffered if connected to a TTY
Status messages that are to be shown on the terminal should be written to the
file descriptor before anything further is done, to keep the user updated.
One common way to achieve this is to make stdout line-buffered if it is
connected to a TTY. This is done on Python 2 (except on Windows, where libc,
which the CPython 2 streams depend on, does not properly support this).
Python 3 rolls it own I/O streams. On Python 3, buffered binary streams can't be
set line-buffered. The previous code (added in 227ba1afcb65) incorrectly
assumed that on Python 3, pycompat.stdout (sys.stdout.buffer) is already
line-buffered. However the interpreter initializes it with a block-buffered
stream or an unbuffered stream (when the -u option or the PYTHONUNBUFFERED
environment variable is set), never with a line-buffered stream.
One example where the current behavior is unacceptable is when running
`hg pull https://www.mercurial-scm.org/repo/hg` on Python 3, where the line
"pulling from https://www.mercurial-scm.org/repo/hg" does not appear on the
terminal before the hg process blocks while waiting for the server.
Various approaches to fix this problem are possible, including:
1. Weaken the contract of procutil.stdout to not give any guarantees about
buffering behavior. In this case, users of procutil.stdout need to be
changed to do enough flushes. In particular,
1. either ui must insert enough flushes for ui.write() and friends, or
2. ui.write() and friends get split into flushing and fully buffered
methods, or
3. users of ui.write() and friends must flush explicitly.
2. Make stdout unbuffered.
3. Make stdout line-buffered. Since Python 3 does not natively support that for
binary streams, we must implement it ourselves.
(2.) is problematic because using unbuffered I/O changes the performance
characteristics significantly compared to line-buffered (which is used on
Python 2) and this would be a regression.
(1.2.) and (1.3) are a substantial amount of work. It’s unclear whether the
added complexity would be justified, given that raw performance doesn’t matter
that much when writing to a terminal much faster than the user could read it.
(1.1.) pushes complexity into the ui class instead of separating the concern of
how stdout is buffered. Other users of procutil.stdout would still need to take
care of the flushes.
This patch implements (3.). The general performance considerations are very
similar to (1.1.). The extra method invocation and method forwarding add a
little more overhead if the class is used. In exchange, it doesn’t add overhead
if not used.
For the benchmarks, I compared the previous implementation (incorrect on Python
3), (1.1.), (3.) and (2.). The command was chosen so that the streams were
configured as if they were writing to a TTY, but actually write to a pager,
which is also the default:
HGRCPATH=/dev/null python3 ./hg --cwd ~/vcs/mozilla-central --time --pager yes --config pager.pager='cat > /dev/null' status --all
previous:
time: real 7.880 secs (user 7.290+0.050 sys 0.580+0.170)
time: real 7.830 secs (user 7.220+0.070 sys 0.590+0.140)
time: real 7.800 secs (user 7.210+0.050 sys 0.570+0.170)
(1.1.) using Yuya Nishihara’s patch:
time: real 9.860 secs (user 8.670+0.350 sys 1.160+0.830)
time: real 9.540 secs (user 8.430+0.370 sys 1.100+0.770)
time: real 9.830 secs (user 8.630+0.370 sys 1.180+0.840)
(3.) using this patch:
time: real 9.580 secs (user 8.480+0.350 sys 1.090+0.770)
time: real 9.670 secs (user 8.480+0.330 sys 1.170+0.860)
time: real 9.640 secs (user 8.500+0.350 sys 1.130+0.810)
(2.) using a previous patch by me:
time: real 10.480 secs (user 8.850+0.720 sys 1.590+1.500)
time: real 10.490 secs (user 8.750+0.750 sys 1.710+1.470)
time: real 10.240 secs (user 8.600+0.700 sys 1.590+1.510)
As expected, there’s no difference on Python 2, as exactly the same code paths
are used:
previous:
time: real 6.950 secs (user 5.870+0.330 sys 1.070+0.770)
time: real 7.040 secs (user 6.040+0.360 sys 0.980+0.750)
time: real 7.070 secs (user 5.950+0.360 sys 1.100+0.760)
this patch:
time: real 7.010 secs (user 5.900+0.390 sys 1.070+0.730)
time: real 7.000 secs (user 5.850+0.350 sys 1.120+0.760)
time: real 7.000 secs (user 5.790+0.380 sys 1.170+0.710)
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Wed, 10 Jun 2020 13:02:39 +0200 |
parents | 9d2b2df2c2ba |
children |
rev | line source |
---|---|
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 ) |