本文整理汇总了Python中tchannel.tornado.TChannel类的典型用法代码示例。如果您正苦于以下问题:Python TChannel类的具体用法?Python TChannel怎么用?Python TChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send
def send(self, arg1, arg2, arg3,
headers=None,
traceflag=None,
retry_limit=None,
ttl=None):
arg1, arg2, arg3 = map(maybe_stream, [arg1, arg2, arg3])
endpoint = yield read_full(arg1)
headers = headers or {}
headers.setdefault('as', self.arg_scheme)
vcr_request = proxy.Request(
serviceName=self.service.encode('utf-8'),
hostPort=self.hostport,
knownPeers=self.original_tchannel.peers.hosts,
endpoint=endpoint,
headers=(yield read_full(arg2)),
body=(yield read_full(arg3)),
argScheme=getattr(proxy.ArgScheme, self.arg_scheme.upper()),
transportHeaders=[
proxy.TransportHeader(bytes(k), bytes(v))
for k, v in headers.items()
],
)
# TODO what to do with traceflag, attempt-times, ttl
# TODO catch protocol errors
from tchannel import TChannel
tchannel = TChannel('proxy-client')
with force_reset():
vcr_response_future = tchannel.thrift(
proxy.VCRProxy.send(vcr_request),
hostport=self.vcr_hostport,
)
try:
vcr_response = yield vcr_response_future
except proxy.RemoteServiceError as e:
raise TChannelError.from_code(
e.code,
description=(
"The remote service threw a protocol error: %s" %
e.message
)
)
response = Response(
code=vcr_response.body.code,
argstreams=[
maybe_stream(endpoint),
maybe_stream(vcr_response.body.headers),
maybe_stream(vcr_response.body.body),
],
# TODO headers=vcr_response.transportHeaders,
)
raise gen.Return(response)
示例2: test_retry_on_error_success
def test_retry_on_error_success():
endpoint = b'tchannelretrytest'
tchannel = chain(2, endpoint)
tchannel_success = TChannel(name='test', hostport='localhost:0')
tchannel_success.register(endpoint, 'raw', handler_success)
tchannel_success.listen()
tchannel.peers.get(tchannel_success.hostport)
with (
patch(
'tchannel.tornado.Request.should_retry_on_error',
autospec=True)
) as mock_should_retry_on_error:
mock_should_retry_on_error.return_value = True
response = yield tchannel.request(
score_threshold=0
).send(
endpoint,
"test",
"test",
headers={
're': RetryType.CONNECTION_ERROR_AND_TIMEOUT,
},
ttl=0.01,
attempt_times=3,
retry_delay=0.01,
)
header = yield response.get_header()
body = yield response.get_body()
assert body == "success"
assert header == ""
示例3: test_zipkin_trace
def test_zipkin_trace(trace_server):
endpoint = b'endpoint1'
zipkin_tracer = ZipkinTraceHook(dst=trace_buf)
tchannel = TChannel(name='test')
tchannel.hooks.register(zipkin_tracer)
hostport = 'localhost:%d' % trace_server.port
response = yield tchannel.request(hostport).send(InMemStream(endpoint),
InMemStream(hostport),
InMemStream(),
traceflag=True)
header = yield response.get_header()
body = yield response.get_body()
assert header == "from handler1"
assert body == "from handler2"
traces = []
for trace in trace_buf.getvalue().split("\n"):
if trace:
traces.append(json.loads(trace))
trace_id = traces[0][0][u'trace_id']
for trace in traces:
assert trace_id == trace[0][u'trace_id']
if trace[0][u'name'] == u'endpoint2':
parent_span_id = trace[0][u'parent_span_id']
else:
span_id = trace[0][u'span_id']
assert parent_span_id == span_id
示例4: test_retry_on_error_success
def test_retry_on_error_success(mock_should_retry_on_error):
mock_should_retry_on_error.return_value = True
endpoint = b'tchannelretrytest'
tchannel = yield chain(2, endpoint)
hook = MyTestHook()
tchannel.hooks.register(hook)
tchannel_success = TChannel(name='test', hostport='localhost:0')
tchannel_success.register(endpoint, 'raw', handler_success)
tchannel_success.listen()
tchannel.peers.get(tchannel_success.hostport)
response = yield tchannel.request().send(
endpoint,
"test",
"test",
headers={
're': retry.CONNECTION_ERROR_AND_TIMEOUT,
},
ttl=1,
retry_limit=2,
)
header = yield response.get_header()
body = yield response.get_body()
assert body == "success"
assert header == ""
assert hook.received_response == 1
assert hook.received_error == 2
示例5: main
def main(): # pragma: no cover
import sys
parser = argparse.ArgumentParser()
parser.add_argument(
"--port",
dest="port", default=8888, type=int,
)
parser.add_argument(
"--host",
dest="host", default="127.0.0.1"
)
args = parser.parse_args()
client = TChannel('%s:%d' % (args.host, args.port))
# TODO: service=test_as_raw
handler = RequestDispatcher()
client.host(handler)
# TODO: do we need to implement these differently?
handler.register("echo", echo)
handler.register("streaming_echo", echo)
client.listen()
print 'listening on', client.hostport
sys.stdout.flush()
tornado.ioloop.IOLoop.instance().start()
示例6: main
def main(): # pragma: no cover
args = get_args()
client = TChannel('localhost:%d' % args.port)
register_example_endpoints(client)
client.listen()
tornado.ioloop.IOLoop.instance().start()
示例7: test_request
def test_request():
channel = TChannel(name="test")
hyperbahn.advertise(channel, "foo", ["127.0.0.1:23000"])
# Just want to make sure all the plumbing fits together.
with pytest.raises(NoAvailablePeerError):
yield channel.request(service="bar").send(arg1="baz", arg2="bam", arg3="boo", headers={"as": "qux"})
示例8: test_endpoint_not_found
def test_endpoint_not_found(tchannel_server):
endpoint = b"tchanneltest"
tchannel_server.expect_call(endpoint).and_write(headers=endpoint, body="world")
tchannel = TChannel(name="test")
hostport = "localhost:%d" % (tchannel_server.port)
with pytest.raises(TChannelError):
yield tchannel.request(hostport).send(InMemStream(), InMemStream(), InMemStream())
示例9: send_stream
def send_stream(arg1, arg2, arg3, host):
tchannel = TChannel()
response = yield tchannel.request(host).send(
arg1,
arg2,
arg3)
yield print_arg(response, 0)
yield print_arg(response, 1)
yield print_arg(response, 2)
示例10: send_stream
def send_stream(arg1, arg2, arg3, host):
tchannel = TChannel(
name='stream-client',
)
yield tchannel.request(host).send(
arg1,
arg2,
arg3,
)
示例11: test_endpoint_not_found
def test_endpoint_not_found(mock_server):
endpoint = b'tchanneltest'
mock_server.expect_call(endpoint).and_write(
headers=endpoint,
body='world'
)
tchannel = TChannel(name='test')
with pytest.raises(TChannelError):
yield tchannel.request(
mock_server.hostport
).send(InMemStream(), InMemStream(), InMemStream())
示例12: main
def main(): # pragma: no cover
args = get_args()
client = TChannel(
name='tchannel_server',
hostport='%s:%d' % (args.host, args.port),
)
register_example_endpoints(client)
client.listen()
tornado.ioloop.IOLoop.instance().start()
示例13: test_request
def test_request():
channel = TChannel()
hyperbahn.advertise(channel, 'foo', ['127.0.0.1:23000'])
# Just want to make sure all the plumbing fits together.
with pytest.raises(ConnectionClosedError):
yield channel.request(service='bar').send(
arg1='baz',
arg2='bam',
arg3='boo',
headers={'as': 'qux'},
)
示例14: main
def main():
args = get_args()
tchannel = TChannel(name="raw-client")
request = tchannel.request(hostport="%s:%s" % (args.host, args.port))
response = yield request.send("hi", None, None)
body = yield response.get_body()
print body
示例15: test_endpoint_not_found
def test_endpoint_not_found(tchannel_server):
endpoint = b'tchanneltest'
tchannel_server.expect_call(endpoint).and_write(
headers=endpoint,
body='world'
)
tchannel = TChannel()
hostport = 'localhost:%d' % (tchannel_server.port)
with pytest.raises(TChannelError):
yield tchannel.request(hostport).send(InMemStream(),
InMemStream(),
InMemStream())