Last active
August 29, 2015 14:06
-
-
Save hagsteel/6113b0e0e9cd4b4c3585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
from swampdragon_auth.socketconnection import HttpDataConnection | |
from tornado import ioloop | |
class MysqlHeartbeatConnection(HttpDataConnection): | |
def _close_db_connection(self): | |
connection.close() | |
def on_open(self, request): | |
super(MysqlHeartbeatConnection, self).on_open(request) | |
# Change the callback time to be closer to DB timeout | |
iol = ioloop.IOLoop.current() | |
self.db_heartbeat = ioloop.PeriodicCallback(self._close_db_connection, callback_time=3000, io_loop=iol) | |
self.db_heartbeat.start() | |
def on_close(self): | |
super(MysqlHeartbeatConnection, self).on_close() | |
self._close_db_connection() | |
self.db_heartbeat.stop() | |
def on_message(self, data): | |
self.db_heartbeat.stop() | |
self.db_heartbeat.start() | |
super(MysqlHeartbeatConnection, self).on_message(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment