Skip to content

Commit

Permalink
Initial support for gevent
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Mar 7, 2014
1 parent 00b6e2c commit 4246276
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -80,6 +80,7 @@
'django-templatetag-sugar>=0.1.0,<0.2.0',
'djangorestframework>=2.3.8,<2.4.0',
'email-reply-parser>=0.2.0,<0.3.0',
'gevent>=1.0.0,<1.1.0',
'gunicorn>=0.17.2,<0.18.0',
'httpagentparser>=1.6.0,<1.7.0',
'logan>=0.5.8.2,<0.6.0',
Expand Down
57 changes: 57 additions & 0 deletions src/sentry/utils/gevent.py
@@ -0,0 +1,57 @@
"""A wait callback to allow psycopg2 cooperation with gevent.
Use `make_psycopg_green()` to enable gevent support in Psycopg.
"""

# Copyright (C) 2010 Daniele Varrazzo <daniele.varrazzo@gmail.com>
# and licensed under the MIT license:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import absolute_import

import psycopg2
from psycopg2 import extensions

from gevent.socket import wait_read, wait_write


def make_psycopg_green():
"""Configure Psycopg to be used with gevent in non-blocking way."""
if not hasattr(extensions, 'set_wait_callback'):
raise ImportError(
"support for coroutines not available in this Psycopg version (%s)"
% psycopg2.__version__)

extensions.set_wait_callback(gevent_wait_callback)


def gevent_wait_callback(conn, timeout=None):
"""A wait callback useful to allow gevent to work with Psycopg."""
while 1:
state = conn.poll()
if state == extensions.POLL_OK:
break
elif state == extensions.POLL_READ:
wait_read(conn.fileno(), timeout=timeout)
elif state == extensions.POLL_WRITE:
wait_write(conn.fileno(), timeout=timeout)
else:
raise psycopg2.OperationalError(
"Bad result from poll: %r" % state)
25 changes: 25 additions & 0 deletions src/sentry/utils/runner.py
Expand Up @@ -6,13 +6,17 @@
:copyright: (c) 2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from logan.runner import run_app, configure_app

import base64
import os
import pkg_resources
import warnings

USE_GEVENT = os.environ.get('USE_GEVENT')

KEY_LENGTH = 40

CONFIG_TEMPLATE = """
Expand Down Expand Up @@ -222,10 +226,27 @@ def initialize_receivers():
import sentry.receivers # NOQA


def initialize_gevent():
from gevent import monkey
monkey.patch_all()

try:
import psycopg2 # NOQA
except ImportError:
pass
else:
from sentry.utils.gevent import make_psycopg_green
make_psycopg_green()


def initialize_app(config):
from django.utils import timezone
from sentry.app import env

if USE_GEVENT:
from django.db import connections
connections['default'].allow_thread_sharing = True

env.data['config'] = config.get('config_path')
env.data['start_date'] = timezone.now()

Expand Down Expand Up @@ -308,6 +329,10 @@ def configure():


def main():
if USE_GEVENT:
print "Configuring Sentry with gevent bindings"
initialize_gevent()

run_app(
project='sentry',
default_config_path='~/.sentry/sentry.conf.py',
Expand Down

0 comments on commit 4246276

Please sign in to comment.