author | Gregory Szorc <gregory.szorc@gmail.com> |
Thu, 30 Nov 2017 21:19:46 -0500 | |
branch | stable |
changeset 37833 | 6614cac550ae |
parent 37633 | 33a6eee08db2 |
child 41335 | b81ca9a3f4e4 |
permissions | -rw-r--r-- |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
1 |
# test-batching.py - tests for transparent command batching |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
2 |
# |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch> |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
4 |
# |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
7 |
|
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
8 |
from __future__ import absolute_import, print_function |
28800
544908ae36ce
test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents:
28732
diff
changeset
|
9 |
|
37633
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
10 |
import contextlib |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
11 |
|
28800
544908ae36ce
test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents:
28732
diff
changeset
|
12 |
from mercurial import ( |
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
13 |
localrepo, |
37614
a81d02ea65db
wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33766
diff
changeset
|
14 |
wireprotov1peer, |
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
15 |
|
28731
f8872b507cd3
py3: use absolute_import in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
25912
diff
changeset
|
16 |
) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
17 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
18 |
# equivalent of repo.repository |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
19 |
class thing(object): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
20 |
def hello(self): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
21 |
return "Ready." |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
22 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
23 |
# equivalent of localrepo.localrepository |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
24 |
class localthing(thing): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
25 |
def foo(self, one, two=None): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
26 |
if one: |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
27 |
return "%s and %s" % (one, two,) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
28 |
return "Nope" |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
29 |
def bar(self, b, a): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
30 |
return "%s und %s" % (b, a,) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
31 |
def greet(self, name=None): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
32 |
return "Hello, %s" % name |
37633
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
33 |
|
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
34 |
@contextlib.contextmanager |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
35 |
def commandexecutor(self): |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
36 |
e = localrepo.localcommandexecutor(self) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
37 |
try: |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
38 |
yield e |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
39 |
finally: |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
40 |
e.close() |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
41 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
42 |
# usage of "thing" interface |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
43 |
def use(it): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
44 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
45 |
# Direct call to base method shared between client and server. |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
46 |
print(it.hello()) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
47 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
48 |
# Direct calls to proxied methods. They cause individual roundtrips. |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
49 |
print(it.foo("Un", two="Deux")) |
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
50 |
print(it.bar("Eins", "Zwei")) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
51 |
|
33766
4c706037adef
wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33765
diff
changeset
|
52 |
# Batched call to a couple of proxied methods. |
4c706037adef
wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33765
diff
changeset
|
53 |
|
37633
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
54 |
with it.commandexecutor() as e: |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
55 |
ffoo = e.callcommand('foo', {'one': 'One', 'two': 'Two'}) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
56 |
fbar = e.callcommand('bar', {'b': 'Eins', 'a': 'Zwei'}) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
57 |
fbar2 = e.callcommand('bar', {'b': 'Uno', 'a': 'Due'}) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
58 |
|
37633
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
59 |
print(ffoo.result()) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
60 |
print(fbar.result()) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
61 |
print(fbar2.result()) |
33766
4c706037adef
wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33765
diff
changeset
|
62 |
|
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
63 |
# local usage |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
64 |
mylocal = localthing() |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
65 |
print() |
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
66 |
print("== Local") |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
67 |
use(mylocal) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
68 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
69 |
# demo remoting; mimicks what wireproto and HTTP/SSH do |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
70 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
71 |
# shared |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
72 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
73 |
def escapearg(plain): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
74 |
return (plain |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
75 |
.replace(':', '::') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
76 |
.replace(',', ':,') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
77 |
.replace(';', ':;') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
78 |
.replace('=', ':=')) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
79 |
def unescapearg(escaped): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
80 |
return (escaped |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
81 |
.replace(':=', '=') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
82 |
.replace(':;', ';') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
83 |
.replace(':,', ',') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
84 |
.replace('::', ':')) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
85 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
86 |
# server side |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
87 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
88 |
# equivalent of wireproto's global functions |
14764
a7d5816087a9
classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents:
14621
diff
changeset
|
89 |
class server(object): |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
90 |
def __init__(self, local): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
91 |
self.local = local |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
92 |
def _call(self, name, args): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
93 |
args = dict(arg.split('=', 1) for arg in args) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
94 |
return getattr(self, name)(**args) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
95 |
def perform(self, req): |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
96 |
print("REQ:", req) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
97 |
name, args = req.split('?', 1) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
98 |
args = args.split('&') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
99 |
vals = dict(arg.split('=', 1) for arg in args) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
100 |
res = getattr(self, name)(**vals) |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
101 |
print(" ->", res) |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
102 |
return res |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
103 |
def batch(self, cmds): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
104 |
res = [] |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
105 |
for pair in cmds.split(';'): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
106 |
name, args = pair.split(':', 1) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
107 |
vals = {} |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
108 |
for a in args.split(','): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
109 |
if a: |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
110 |
n, v = a.split('=') |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
111 |
vals[n] = unescapearg(v) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
112 |
res.append(escapearg(getattr(self, name)(**vals))) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
113 |
return ';'.join(res) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
114 |
def foo(self, one, two): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
115 |
return mangle(self.local.foo(unmangle(one), unmangle(two))) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
116 |
def bar(self, b, a): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
117 |
return mangle(self.local.bar(unmangle(b), unmangle(a))) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
118 |
def greet(self, name): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
119 |
return mangle(self.local.greet(unmangle(name))) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
120 |
myserver = server(mylocal) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
121 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
122 |
# local side |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
123 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
124 |
# equivalent of wireproto.encode/decodelist, that is, type-specific marshalling |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
125 |
# here we just transform the strings a bit to check we're properly en-/decoding |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
126 |
def mangle(s): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
127 |
return ''.join(chr(ord(c) + 1) for c in s) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
128 |
def unmangle(s): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
129 |
return ''.join(chr(ord(c) - 1) for c in s) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
130 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
131 |
# equivalent of wireproto.wirerepository and something like http's wire format |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
132 |
class remotething(thing): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
133 |
def __init__(self, server): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
134 |
self.server = server |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
135 |
def _submitone(self, name, args): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
136 |
req = name + '?' + '&'.join(['%s=%s' % (n, v) for n, v in args]) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
137 |
return self.server.perform(req) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
138 |
def _submitbatch(self, cmds): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
139 |
req = [] |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
140 |
for name, args in cmds: |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
141 |
args = ','.join(n + '=' + escapearg(v) for n, v in args) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
142 |
req.append(name + ':' + args) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
143 |
req = ';'.join(req) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
144 |
res = self._submitone('batch', [('cmds', req,)]) |
33766
4c706037adef
wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33765
diff
changeset
|
145 |
for r in res.split(';'): |
4c706037adef
wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33765
diff
changeset
|
146 |
yield r |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
147 |
|
37633
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
148 |
@contextlib.contextmanager |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
149 |
def commandexecutor(self): |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
150 |
e = wireprotov1peer.peerexecutor(self) |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
151 |
try: |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
152 |
yield e |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
153 |
finally: |
33a6eee08db2
wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37615
diff
changeset
|
154 |
e.close() |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
155 |
|
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
156 |
@wireprotov1peer.batchable |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
157 |
def foo(self, one, two=None): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
158 |
encargs = [('one', mangle(one),), ('two', mangle(two),)] |
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
159 |
encresref = wireprotov1peer.future() |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
160 |
yield encargs, encresref |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
161 |
yield unmangle(encresref.value) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
162 |
|
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
163 |
@wireprotov1peer.batchable |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
164 |
def bar(self, b, a): |
37615
f3dc8239e3a9
peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37614
diff
changeset
|
165 |
encresref = wireprotov1peer.future() |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
166 |
yield [('b', mangle(b),), ('a', mangle(a),)], encresref |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
167 |
yield unmangle(encresref.value) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
168 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
169 |
# greet is coded directly. It therefore does not support batching. If it |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
170 |
# does appear in a batch, the batch is split around greet, and the call to |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
171 |
# greet is done in its own roundtrip. |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
172 |
def greet(self, name=None): |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
173 |
return unmangle(self._submitone('greet', [('name', mangle(name),)])) |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
174 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
175 |
# demo remote usage |
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
176 |
|
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
177 |
myproxy = remotething(myserver) |
28732
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
178 |
print() |
e2b118592c63
py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents:
28731
diff
changeset
|
179 |
print("== Remote") |
14621
84094c0d2724
wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff
changeset
|
180 |
use(myproxy) |