tornadoweb / tornadoweb/tornado

Access with server-sent events only logged after connection is closed

Open
#1,504 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

web
Dominant language
Python
Stars
22.2k
Forks
5.6k
Avg merge
3h 42m
Merged PRs (30d)
16

Description

I have extended tornado.web.RequestHandler to support server-sent events:

class EventSource(RequestHandler):
    """Base handler for server-sent events."""
    def initialize(self, source):
        """The ``source`` parameter is a string that is updated with
        new data. The :class:`EventSource` instance will continuously
        check if it is updated and publish to clients when it is.

        """
        assert isinstance(source, stores.DataStore)
        self.source = source
        self._last = None
        self.finished = False
        self.set_header('content-type', 'text/event-stream')
        self.set_header('cache-control', 'no-cache')

    @gen.coroutine
    def publish(self, data):
        """Pushes data to a listener."""
        try:
            self.write('data: {}\n\n'.format(data))
            yield self.flush()
        except StreamClosedError:
            self.finished = True

    @gen.coroutine
    def get(self):
        while not self.finished:
            if self.source.data != self._last:
                yield self.publish(self.source.data)
                self._last = self.source.data
            else:
                yield gen.sleep(0.01)
        self.finish()

When connecting to an implementation of EventSource, I correctly receive the streamed data. However, accessing the stream is not logged until after the connection is closed. This results in access logs that indicate very long times:

[I 150829 14:30:20 web:1908] 200 GET /stream/all (::1) 7856.83ms
[I 150829 14:30:22 web:1908] 200 GET /stream/all (::1) 25523.45ms
[I 150829 14:30:23 web:1908] 200 GET /stream/all (::1) 43150.50ms

It would be more useful for streams like this to log when the initial connection is made.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at tornado.web.RequestHandler and the access-log path, then reproduce the behavior with the server-sent EventSource example in the issue. Determine where the request is currently logged and what connection-time event is available. Done means streaming requests produce an access log when the initial connection is made rather than only after it closes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.