]> jfr.im git - erebus.git/commitdiff
have multiple DB connections for use in different threads
authorzonidjan <redacted>
Mon, 16 Sep 2019 02:21:41 +0000 (21:21 -0500)
committerzonidjan <redacted>
Mon, 16 Sep 2019 02:21:41 +0000 (21:21 -0500)
erebus.py

index 8a48473cad82dcac9eefc9d45db28f73352b9162..dc26fef1bd7d4dd8bc52911aeaf6b18ae6dc847c 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -237,6 +237,13 @@ class Erebus(object): #singleton to pass around
        def getuserbyauth(self, auth):
                return [u for u in self.users.values() if u.auth == auth.lower()]
 
+       def getdb(self):
+               """Get a DB object. The object must be returned to the pool after us, using returndb()."""
+               return self.dbs.pop()
+
+       def returndb(self, db):
+               self.dbs.append(db)
+
        #bind functions
        def hook(self, word, handler):
                try:
@@ -280,6 +287,9 @@ class Erebus(object): #singleton to pass around
 
 def dbsetup():
        main.db = None
+       main.dbs = []
+       for i in range(cfg.get('erebus', 'num_db_connections', 2)-1):
+               main.dbs.append(MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor))
        main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
 
 def setup():
@@ -295,12 +305,12 @@ def setup():
        pidfile.close()
 
        main = Erebus(cfg)
+       dbsetup()
 
        autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
        for mod in autoloads:
                ctlmod.load(main, mod)
 
-       dbsetup()
        c = main.query("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1")
        if c:
                rows = c.fetchall()