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

feat: throttle download speed for archived builds #289

Merged
merged 7 commits into from
Oct 11, 2023
Merged
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 docker/dev/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ server {
limit_req zone=media burst=3 nodelay;
limit_conn addr 1;
limit_rate 3m;
internal;

alias /home/shipper/web/media/;
}
Expand Down
1 change: 1 addition & 0 deletions docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ server {
limit_req zone=media burst=3 nodelay;
limit_conn addr 1;
limit_rate 3m;
internal;

alias /home/shipper/web/media/;
}
Expand Down
10 changes: 9 additions & 1 deletion server/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@
"load problems.",
bool,
),
"SHIPPER_DOWNLOADS_ARCHIVE_THROTTLE": (
1,
"Throttles the download speed of archived builds, in MB/s.",
int,
),
"SHIPPER_UPLOAD_VARIANTS": (
'{"gapps": "GApps","vanilla": "Vanilla (no GApps)","foss": "FOSS","goapps": '
'"GoApps (Android Go Edition GApps)"}',
Expand Down Expand Up @@ -296,7 +301,10 @@
"SHIPPER_DOWNLOADS_PAGE_DONATION_URL",
"SHIPPER_DOWNLOADS_PAGE_DONATION_MESSAGE",
),
"Download": ("SHIPPER_DOWNLOADS_DISABLE_MAIN_SERVER",),
"Download": (
"SHIPPER_DOWNLOADS_DISABLE_MAIN_SERVER",
"SHIPPER_DOWNLOADS_ARCHIVE_THROTTLE",
),
"Upload": (
"SHIPPER_UPLOAD_VARIANTS",
"SHIPPER_FILE_NAME_FORMAT",
Expand Down
8 changes: 8 additions & 0 deletions server/downloads/templates/downloads_build.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ <h1>{% blocktranslate %}Downloads for {{ object }}{% endblocktranslate %}</h1>

{% include 'debug_warning.html' %}

<!-- Build archive check -->
{% if object.is_archived == True %}
<div class="alert alert-warning" role="alert">
<h4 class="alert-heading">{% translate "Warning" %}</h4>
<p class="mb-0">{% translate "This build has been archived! You may experience slower download speeds with this build." %}</p>
</div>
{% endif %}

{% include 'donation_alert.html' %}

<h2>{% translate "Build information" %}</h2>
Expand Down
4 changes: 4 additions & 0 deletions server/downloads/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DownloadsDeviceView,
DownloadsMainView,
LanguageSwitchView,
download_view,
)

urlpatterns = [
Expand All @@ -20,4 +21,7 @@
name="downloads_build",
),
path("language_switch/", LanguageSwitchView.as_view(), name="language_switch"),
path(
"media/<slug:codename>/<slug:file_name>/", download_view, name="download_view"
),
]
17 changes: 16 additions & 1 deletion server/downloads/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import DetailView, ListView, TemplateView
from django.views.generic import DetailView, TemplateView
from django.shortcuts import render
from core.models import Build, Device

from constance import config


class DownloadsMainView(TemplateView):
template_name = "downloads_main.html"
Expand Down Expand Up @@ -53,3 +56,15 @@
if not redirect_url:
redirect_url = "/"
return render(request, self.template_name, {"redirect_to": redirect_url})


def download_view(request, codename, file_name):
build = get_object_or_404(Build, file_name=file_name, device__codename=codename)
response = HttpResponse()
response["X-Accel-Redirect"] = f"/media/{codename}/{file_name}"

Check warning on line 64 in server/downloads/views.py

View check run for this annotation

Codecov / codecov/patch

server/downloads/views.py#L62-L64

Added lines #L62 - L64 were not covered by tests

if build.is_archived:
limit_speed = config.SHIPPER_DOWNLOADS_ARCHIVE_THROTTLE * 1000
response["X-Accel-Limit-Rate"] = str(limit_speed)

Check warning on line 68 in server/downloads/views.py

View check run for this annotation

Codecov / codecov/patch

server/downloads/views.py#L66-L68

Added lines #L66 - L68 were not covered by tests

return response

Check warning on line 70 in server/downloads/views.py

View check run for this annotation

Codecov / codecov/patch

server/downloads/views.py#L70

Added line #L70 was not covered by tests