Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@tornado.gen.coroutine
def make_websocket_connection(host, port):
global conn_try_count
url = "ws://%s:%d/ws" % (host, port)
conn_try_count += 1
ws = yield tornado.websocket.websocket_connect(url)
conn_try_count -= 1
ws.stream.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
connections.add(ws)
loop_websocket(ws)
@tornado.gen.coroutine
def post(self):
span = self._get_span()
if span:
try:
with span_in_stack_context(span):
res = self.client_channel.json(
service='handler2',
hostport=self.request.body,
endpoint="endpoint2",
)
res = yield res
body = res.body
except Exception as e:
traceback.print_exc()
self.write('ERROR: %s' % e)
self.set_status(200)
@tornado.gen.coroutine
def _catch_errors(future, verbose, exit=sys.exit):
try:
result = yield future
except Exception, e:
if verbose:
traceback.print_exc(file=sys.stderr)
else:
print >> sys.stderr, str(e)
exit(1)
raise tornado.gen.Return(result)
@gen.coroutine
def get(self):
"""
Return length of all active queues
**Example request**:
.. sourcecode:: http
GET /api/queues/length
Host: localhost:5555
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
@gen.coroutine
def _oauth_get_user_future(self, access_token, callback):
user = yield self.friendfeed_request(
"/feedinfo/" + access_token["username"],
include="id,name,description", access_token=access_token)
if user:
user["username"] = user["id"]
callback(user)
@gen.coroutine
def pre_get(self):
# authenticate first
super(ZMQChannelHandler, self).pre_get()
# then request kernel info, waiting up to a certain time before giving up.
# We don't want to wait forever, because browsers don't take it well when
# servers never respond to websocket connection requests.
future = self.request_kernel_info()
def give_up():
"""Don't wait forever for the kernel to reply"""
if future.done():
return
self.log.warn(
"Timeout waiting for kernel_info reply from %s", self.kernel_id)
future.set_result({})
loop = IOLoop.current()
@gen.coroutine
def _notify_end_sync():
if self._websock is None:
raise TypeError('The websock is not initialized')
self._websock.write_message(json.dumps(end_msg))
def wrapper(self, *args, **kwargs):
self._auto_finish = False
result = method(self, *args, **kwargs)
if result is not None:
result = gen.convert_yielded(result)
# If @asynchronous is used with @gen.coroutine, (but
# not @gen.engine), we can automatically finish the
# request when the future resolves. Additionally,
# the Future will swallow any exceptions so we need
# to throw them back out to the stack context to finish
# the request.
def future_complete(f):
f.result()
if not self._finished:
self.finish()
IOLoop.current().add_future(result, future_complete)
# Once we have done this, hide the Future from our
# caller (i.e. RequestHandler._when_complete), which
# would otherwise set up its own callback and
# exception handler (resulting in exceptions being
@gen.coroutine
def _save_report_files(
self, report_id, files, progress_coroutine=None, manifest_save_order=None
):
"""Concrete implemetation of saving filesself.
Subclasses of AbstractStorage must implement this method. See
`AbstractStorage.csave_report_files` (with no leading underscore) for a
description of the arguments to this function.
The difference between save_report_files (no underscore) and
_save_report_files (leading underscore) is that the former calls the
latter and handles coroutine locking to ensure that subsequent calls to
save_report_files happen in order.
"""
raise NotImplementedError()
)
request = httpclient.HTTPRequest(
url=url, method='GET', request_timeout=self.REQUEST_TIMEOUT
)
async_client = httpclient.AsyncHTTPClient()
try:
# Send Future object to coroutine and suspend till result is ready
response = yield async_client.fetch(request)
except (socket.error, httpclient.HTTPError) as err:
msg = u"Failed to get stats from {url} ({err})".format(url=url, err=err)
if hasattr(err, 'response') and err.response and err.response.body:
msg += u"\nBODY: {body}".format(body=err.response.body)
logger.error(msg)
failure = FailureSnapshot(ip_port=ip_port, error=unicode(err))
raise gen.Return(failure)
try:
stats_body = json.loads(response.body)
cumulative_dict = stats_body["cumulative_counters"]
recent_dict = stats_body["recent_stats"]
cumulative = CumulativeStatsSnapshot(
total=cumulative_dict["all"],
failed=cumulative_dict["failed"],
pb_reqs=cumulative_dict["pb_reqs"],
rest_reqs=cumulative_dict["rest_reqs"]
)
recent = RecentStatsSnapshot(
total=recent_dict["all"],
failed=recent_dict["failed"],
avg_latency=recent_dict["avg_latency"],
pb_reqs=recent_dict["pb_reqs"],