Add connection pooling for Mastodon objects

brutaldon has really bad latency. Some part of it is reconnecting to the
mastodon server every request, even with a cached auth token. This commit
adds a singleton object that caches mastodon.Mastodon objects.
This commit is contained in:
Jason McBrayer 2018-06-05 08:08:12 -04:00
parent 9e04cf13ff
commit e63d6b86f5
1 changed files with 35 additions and 13 deletions

View File

@ -11,23 +11,40 @@ from urllib import parse
class NotLoggedInException(Exception): class NotLoggedInException(Exception):
pass pass
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class MastodonPool(dict, metaclass=Singleton):
pass
def get_mastodon(request): def get_mastodon(request):
if not (request.session.has_key('instance') and if not (request.session.has_key('instance') and
(request.session.has_key('username') or (request.session.has_key('username') or
request.session.has_key('access_token'))): request.session.has_key('access_token'))):
raise NotLoggedInException() raise NotLoggedInException()
pool = MastodonPool()
if request.session.has_key('access_token'): if request.session.has_key('access_token'):
try: try:
client = Client.objects.get(api_base_id=request.session['instance']) client = Client.objects.get(api_base_id=request.session['instance'])
except (Client.DoesNotExist, Client.MultipleObjectsReturned): except (Client.DoesNotExist, Client.MultipleObjectsReturned):
raise NotLoggedInException() raise NotLoggedInException()
if request.session['access_token'] in pool.keys():
mastodon = pool[request.session['access_token']]
print("Mastodon from pool")
else:
mastodon = Mastodon( mastodon = Mastodon(
client_id = client.client_id, client_id = client.client_id,
client_secret = client.client_secret, client_secret = client.client_secret,
api_base_url = client.api_base_id, api_base_url = client.api_base_id,
access_token = request.session['access_token'], access_token = request.session['access_token'],
ratelimit_method='throw') ratelimit_method='throw')
print("New Mastodon added to pool")
pool[request.session['access_token']] = mastodon
else: else:
try: try:
client = Client.objects.get(api_base_id=request.session['instance']) client = Client.objects.get(api_base_id=request.session['instance'])
@ -35,13 +52,18 @@ def get_mastodon(request):
except (Client.DoesNotExist, Client.MultipleObjectsReturned, except (Client.DoesNotExist, Client.MultipleObjectsReturned,
Account.DoesNotExist, Account.MultipleObjectsReturned): Account.DoesNotExist, Account.MultipleObjectsReturned):
raise NotLoggedInException() raise NotLoggedInException()
if user.access_token in pool.keys():
mastodon = pool[user.access_token]
print("Mastodon from pool")
else:
mastodon = Mastodon( mastodon = Mastodon(
client_id = client.client_id, client_id = client.client_id,
client_secret = client.client_secret, client_secret = client.client_secret,
access_token = user.access_token, access_token = user.access_token,
api_base_url = client.api_base_id, api_base_url = client.api_base_id,
ratelimit_method="throw") ratelimit_method="throw")
print("New Mastodon added to pool")
pool[user.access_token] = mastodon
return mastodon return mastodon
def fullbrutalism_p(request): def fullbrutalism_p(request):