Skip to content

Commit

Permalink
Merge branch 'qt5' into py3
Browse files Browse the repository at this point in the history
Conflicts:
	splash/resources.py
  • Loading branch information
sunu committed Aug 15, 2015
2 parents c21e5cc + 3afb20f commit 216198d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions splash/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ def render_GET(self, request):

# check arguments before starting the render
render_options.get_filters(self.pool)

timeout = render_options.get_timeout()
wait_time = render_options.get_wait()

pool_d = self._getRender(request, render_options)
timer = reactor.callLater(timeout+wait_time, pool_d.cancel)
pool_d.addCallback(self._cancelTimer, timer)
pool_d.addCallback(self._writeOutput, request)
pool_d.addCallback(self._writeOutput, request, options=render_options.data)
pool_d.addErrback(self._timeoutError, request)
pool_d.addErrback(self._renderError, request)
pool_d.addErrback(self._badRequest, request)
Expand Down Expand Up @@ -99,38 +100,38 @@ def _cancelTimer(self, _, timer):
timer.cancel()
return _

def _writeOutput(self, data, request, content_type=None):
def _writeOutput(self, data, request, content_type=None, options=None):
# log.msg("_writeOutput: %s" % id(request))

if content_type is None:
content_type = self.content_type

if isinstance(data, (dict, list)):
data = json.dumps(data, cls=SplashJSONEncoder)
return self._writeOutput(data, request, "application/json")
return self._writeOutput(data, request, "application/json", options)

if isinstance(data, tuple) and len(data) == 3:
data, content_type, headers = data

for name, value in headers:
request.setHeader(name, value)
return self._writeOutput(data, request, content_type)
return self._writeOutput(data, request, content_type, options)

if data is None or isinstance(data, (bool, six.integer_types, float)):
return self._writeOutput(str(data), request, content_type)
return self._writeOutput(str(data), request, content_type, options)

if isinstance(data, BinaryCapsule):
return self._writeOutput(data.data, request, content_type)
return self._writeOutput(data.data, request, content_type, options)

request.setHeader(b"content-type", content_type)

self._logStats(request)
self._logStats(request, options)
if not isinstance(data, bytes):
# Twisted expects bytes as response
data = data.encode('utf-8')
request.write(data)

def _logStats(self, request):
def _logStats(self, request, options):

def args_to_unicode(args):
unicode_args = {}
Expand All @@ -143,20 +144,24 @@ def args_to_unicode(args):
unicode_args[key] = val
return unicode_args

stats = {
msg = {
# Anything we retrieve from Twisted request object contains bytes.
# We have to convert it to unicode first for json.dump to succeed.
"path": request.path.decode('utf-8'),
"args": args_to_unicode(request.args),
"rendertime": time.time() - request.starttime,
"maxrss": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
"load": os.getloadavg(),
"fds": get_num_fds(),
"active": len(self.pool.active),
"qsize": len(self.pool.queue.pending),
"_id": id(request),
"method": request.method.decode('ascii'),
"timestamp": int(time.time()),
"user-agent": (request.getHeader(b"user-agent").decode('utf-8')
if request.getHeader(b"user-agent") else None),
"args": options
}
log.msg(json.dumps(stats), system="stats")
log.msg(json.dumps(msg), system="events")

def _timeoutError(self, failure, request):
failure.trap(defer.CancelledError)
Expand Down

0 comments on commit 216198d

Please sign in to comment.