kopia lustrzana https://github.com/snarfed/bridgy-fed
memcache: truncate keys at 250 chars
max memcache key len: https://github.com/memcached/memcached/wiki/Commands#standard-protocol https://pymemcache.readthedocs.io/en/latest/apidoc/pymemcache.client.base.html fixes https://console.cloud.google.com/errors/detail/CNPzqYzWg4_TyQE;time=P30D?project=bridgy-federatedpull/1187/head
rodzic
e156ce9e12
commit
7b0151b0cf
|
@ -1034,7 +1034,7 @@ def inbox(protocol=None, id=None):
|
|||
logger.info(f'Discarding, {id} is on an opted out domain')
|
||||
return '', 204
|
||||
|
||||
memcache_key = f'AP-id-{id}'
|
||||
memcache_key = common.memcache_key(f'AP-id-{id}')
|
||||
if memcache.get(memcache_key):
|
||||
logger.info(f'Already seen this activity {id}')
|
||||
return '', 204
|
||||
|
|
18
common.py
18
common.py
|
@ -91,6 +91,9 @@ RUN_TASKS_INLINE = False # overridden by unit tests
|
|||
# for Protocol.REQUIRES_OLD_ACCOUNT, how old is old enough
|
||||
OLD_ACCOUNT_AGE = timedelta(days=14)
|
||||
|
||||
# https://github.com/memcached/memcached/wiki/Commands#standard-protocol
|
||||
MEMCACHE_KEY_MAX_LEN = 250
|
||||
|
||||
if appengine_info.DEBUG or appengine_info.LOCAL_SERVER:
|
||||
logger.info('Using in memory mock memcache')
|
||||
memcache = MockMemcacheClient()
|
||||
|
@ -98,7 +101,8 @@ if appengine_info.DEBUG or appengine_info.LOCAL_SERVER:
|
|||
else:
|
||||
logger.info('Using production Memorystore memcache')
|
||||
memcache = pymemcache.client.base.PooledClient(
|
||||
'10.126.144.3', timeout=10, connect_timeout=10) # seconds
|
||||
'10.126.144.3', timeout=10, connect_timeout=10, # seconds
|
||||
allow_unicode_keys=True)
|
||||
global_cache = MemcacheCache(memcache)
|
||||
|
||||
|
||||
|
@ -397,3 +401,15 @@ def global_cache_timeout_policy(key):
|
|||
return int(timedelta(hours=2).total_seconds())
|
||||
|
||||
return int(timedelta(minutes=30).total_seconds())
|
||||
|
||||
|
||||
def memcache_key(key):
|
||||
"""Preprocesses a memcache key. Right now just truncates it to 250 chars.
|
||||
|
||||
https://pymemcache.readthedocs.io/en/latest/apidoc/pymemcache.client.base.html
|
||||
https://github.com/memcached/memcached/wiki/Commands#standard-protocol
|
||||
|
||||
TODO: truncate to 250 *UTF-8* chars, to handle Unicode chars in URLs. Related:
|
||||
pymemcache Client's allow_unicode_keys constructor kwarg.
|
||||
"""
|
||||
return key[:MEMCACHE_KEY_MAX_LEN]
|
||||
|
|
|
@ -770,7 +770,7 @@ class Protocol:
|
|||
error(f'Activity {id} is blocklisted')
|
||||
|
||||
# lease this object atomically
|
||||
lease_memcache_key = f'receive-{id}'
|
||||
lease_memcache_key = common.memcache_key(f'receive-{id}')
|
||||
if not common.memcache.add(lease_memcache_key, 'leased',
|
||||
noreply=False, expire=5 * 60): # 5 min
|
||||
error('This object is already being received elsewhere', status=204)
|
||||
|
|
Ładowanie…
Reference in New Issue