Skip to content

Commit

Permalink
fix webmention endpoint discovery cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 11, 2023
1 parent 76e26a7 commit 9cc8451
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
26 changes: 25 additions & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,32 @@ def redirect_unwrap(val):
return val


def webmention_endpoint_cache_key(url):
"""Returns cache key for a cached webmention endpoint for a given URL.
Just the domain by default. If the URL is the home page, ie path is / , the
key includes a / at the end, so that we cache webmention endpoints for home
pages separate from other pages. https://github.com/snarfed/bridgy/issues/701
Example: 'snarfed.org /'
https://github.com/snarfed/bridgy-fed/issues/423
Adapted from bridgy/util.py.
"""
parsed = urllib.parse.urlparse(url)
key = parsed.netloc
if parsed.path in ('', '/'):
key += ' /'

# logger.debug(f'wm cache key {key}')
return key


@cachetools.cached(cachetools.TTLCache(50000, 60 * 60 * 2), # 2h expiration
lock=threading.Lock())
key=webmention_endpoint_cache_key,
lock=threading.Lock(),
info=True)
def webmention_discover(url, **kwargs):
"""Thin caching wrapper around :func:`webmention.discover`."""
return webmention.discover(url, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def send_webmentions(obj, proxy=None):

try:
endpoint = common.webmention_discover(target.uri).endpoint
# logger.debug(f'wm cache info {common.webmention_discover.cache_info()}')
if endpoint:
webmention.send(endpoint, wm_source, target.uri)
logger.info('Success!')
Expand Down

0 comments on commit 9cc8451

Please sign in to comment.