Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry Plausible stats using backoff #52

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# "jinja2",
# "requests",
# "docutils",
# "backoff",
# ]
# ///
import subprocess
Expand Down
6 changes: 4 additions & 2 deletions visitors.py
Copy link
Collaborator

@mattwang44 mattwang44 Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that backoff is not actively maintained (ref). The retry mechanism from urllib3 (which has exponential backoff too) could be a good alternative.

from requests.adapters import HTTPAdapter
from requests.sessions import Session
from urllib3.util import Retry

def make_session():
    session = Session()
    retry = Retry(
        total=5,
        backoff_factor=1,
        allowed_methods=None,  # all methods are allowed
        status_forcelist={502, 503, 504},  # add the status code of Plausible error response here
    )
    session.mount('https://', HTTPAdapter(max_retries=retry))
    return session

session = make_session()
session.get(..., timeout=40)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that BadZipFile is raised later, after successfully receiving the request. Hm, or not, actually I now realized we don't check the response status code. Let me investigate and try with urllib3.Retry 👍

Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import csv
import io
import urllib
import urllib.parse
import zipfile

import backoff
from requests import Session


@backoff.on_exception(backoff.expo, zipfile.BadZipFile, max_tries=5)
def get_number_of_visitors(language: str, requests: Session) -> int:
params = urllib.parse.urlencode(
{'filters': f'[["contains","event:page",["/{language}/"]]]', 'period': 'all'}
)
r = requests.get(
f'https://plausible.io/docs.python.org/export?{params}', timeout=20
f'https://plausible.io/docs.python.org/export?{params}', timeout=40
)
with (
zipfile.ZipFile(io.BytesIO(r.content), 'r') as z,
Expand Down
Loading