~ubuntuone-pqm-team/python-timeline-django/trunk

« back to all changes in this revision

Viewing changes to timeline_django/hooks.py

  • Committer: James Westby
  • Date: 2014-09-01 14:09:06 UTC
  • mfrom: (22.1.2 clean-env)
  • Revision ID: james.westby@canonical.com-20140901140906-jxhwp4pqvnvu96a7
Be more careful about leaking objects.

1. Register signals with a dispatch_uid so that if register is called
   twice there aren't double receivers. There is a very slim chance that
   someone could two want two sets of receivers with timeline_factories
   that get different timelines. In that case this should be rewritten
   to not use dispatch_uid and offer a disconnect method that can be
   used in tests etc.

2. Re-set the environ stored in the thread local after the wsgi response
   is delivered. In a case where some requests go through wsgi and some
   don't (e.g. tests) the environ would leak in to those that don't use
   wsgi. This could lead to an ever-growing timeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# GNU Lesser General Public License version 3 (see the file LICENSE).
15
15
 
16
16
from datetime import timedelta
17
 
import threading
18
17
 
19
18
import django.core.signals
20
19
import django.db.backends.signals
87
86
 
88
87
    def connect_to_signals(self):
89
88
        """Connect the callbacks to their corresponding signals."""
90
 
        django.core.signals.request_started.connect(self.request_started, weak=False)
91
 
        django.core.signals.request_finished.connect(self.request_finished, weak=False)
92
 
        django.core.signals.got_request_exception.connect(self.got_request_exception, weak=False)
93
 
        django.db.backends.signals.connection_created.connect(self.connection_created, weak=False)
94
 
        signals.wsgi_request_started.connect(self.wsgi_request_started, weak=False)
95
 
        signals.wsgi_response_started.connect(self.wsgi_response_started, weak=False)
 
89
        django.core.signals.request_started.connect(
 
90
            self.request_started, weak=False, dispatch_uid="timeline_django")
 
91
        django.core.signals.request_finished.connect(
 
92
            self.request_finished, weak=False, dispatch_uid="timeline_django")
 
93
        django.core.signals.got_request_exception.connect(
 
94
            self.got_request_exception, weak=False, dispatch_uid="timeline_django")
 
95
        django.db.backends.signals.connection_created.connect(
 
96
            self.connection_created, weak=False, dispatch_uid="timeline_django")
 
97
        signals.wsgi_request_started.connect(
 
98
            self.wsgi_request_started, weak=False, dispatch_uid="timeline_django")
 
99
        signals.wsgi_response_started.connect(
 
100
            self.wsgi_response_started, weak=False, dispatch_uid="timeline_django")