mirror of
http://git.carcosa.net/jmcbray/brutaldon.git
synced 2024-11-27 09:10:08 -05:00
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:
parent
9e04cf13ff
commit
e63d6b86f5
@ -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()
|
||||||
mastodon = Mastodon(
|
if request.session['access_token'] in pool.keys():
|
||||||
client_id = client.client_id,
|
mastodon = pool[request.session['access_token']]
|
||||||
client_secret = client.client_secret,
|
print("Mastodon from pool")
|
||||||
api_base_url = client.api_base_id,
|
else:
|
||||||
access_token = request.session['access_token'],
|
mastodon = Mastodon(
|
||||||
ratelimit_method='throw')
|
client_id = client.client_id,
|
||||||
|
client_secret = client.client_secret,
|
||||||
|
api_base_url = client.api_base_id,
|
||||||
|
access_token = request.session['access_token'],
|
||||||
|
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 = Mastodon(
|
mastodon = pool[user.access_token]
|
||||||
client_id = client.client_id,
|
print("Mastodon from pool")
|
||||||
client_secret = client.client_secret,
|
else:
|
||||||
access_token = user.access_token,
|
mastodon = Mastodon(
|
||||||
api_base_url = client.api_base_id,
|
client_id = client.client_id,
|
||||||
ratelimit_method="throw")
|
client_secret = client.client_secret,
|
||||||
|
access_token = user.access_token,
|
||||||
|
api_base_url = client.api_base_id,
|
||||||
|
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):
|
||||||
|
Loading…
Reference in New Issue
Block a user