Add speed profiles to limit the bandwidth of each peer

This commit is contained in:
Eduardo Silva
2026-11-24 17:05:13 -03:00
parent 026c82445b
commit 3f2da7e545
35 changed files with 1538 additions and 25 deletions
+2
View File
@@ -125,6 +125,8 @@ def _clean_section_report(sections):
'hash': str(report.get('hash', ''))[:64],
'status': status,
'message': str(report.get('message', ''))[:1000],
# Problems that did not stop the section, such as speed limits that could not be applied
'warning': str(report.get('warning', ''))[:1000] if status == 'ok' else '',
'updated': timezone.now().isoformat(),
}
return cleaned_sections
+17 -5
View File
@@ -107,6 +107,7 @@ def _mirror_endpoint_peers(content, content_sha256, filename, worker):
suspended=False, disabled_by_schedule=False,
)
.exclude(endpoint='')
.select_related('speed_profile')
.prefetch_related(Prefetch('peerallowedip_set', queryset=PeerAllowedIP.objects.filter(config_file='server')))
)
worker_lines = [line for peer in endpoint_peers for line in peer_config_lines(peer)]
@@ -203,12 +204,15 @@ def build_manifest(worker):
CERTIFICATE_EXPIRATION_WARNING_DAYS = 14
def get_worker_warnings(worker):
"""Stopped services and certificates that are not trusted or expire soon, as reported by the worker."""
def get_worker_warnings(worker, sync_state=None):
"""
Stopped services, certificates that are not trusted or expire soon and sections applied with warnings, as
reported by the worker. sync_state avoids building the manifest again when it is already known.
"""
try:
worker_status = worker.workerstatus
except WorkerStatus.DoesNotExist:
return {'stopped_services': [], 'certificate_issues': []}
return {'stopped_services': [], 'certificate_issues': [], 'section_warnings': []}
stopped_services = sorted(
service for service, state in (worker_status.services_status or {}).items() if state != 'running'
@@ -221,7 +225,15 @@ def get_worker_warnings(worker):
certificate_issues.append(hostname)
elif certificate.get('status') == 'trusted' and not_after and not_after < expiration_limit:
certificate_issues.append(hostname)
return {'stopped_services': stopped_services, 'certificate_issues': certificate_issues}
# Only warnings of the content currently applied: a newer version replaces them once it is reported
sync_state = sync_state or get_worker_sync_state(worker)
section_warnings = sorted(
section_name for section_name, section in sync_state['sections'].items() if section.get('warning')
)
return {
'stopped_services': stopped_services, 'certificate_issues': certificate_issues,
'section_warnings': section_warnings,
}
def get_worker_sync_state(worker, manifest=None):
@@ -244,7 +256,7 @@ def get_worker_sync_state(worker, manifest=None):
elif reported.get('status') == 'error':
sections[section_name] = {'state': 'error', 'message': reported.get('message', '')}
else:
sections[section_name] = {'state': 'synced', 'message': ''}
sections[section_name] = {'state': 'synced', 'message': '', 'warning': reported.get('warning', '')}
states = {section['state'] for section in sections.values()}
if 'error' in states:
+3 -2
View File
@@ -35,7 +35,7 @@ def describe_worker(worker):
f"{section_name}: {section['message']}" for section_name, section in worker.sync_state['sections'].items()
if section['state'] == 'error' and section['message']
]
worker.warnings = get_worker_warnings(worker)
worker.warnings = get_worker_warnings(worker, worker.sync_state)
# Reported data of disabled, refused or offline workers is only the last known state
worker.operational = worker.enabled and not worker.error_status and worker.is_online
worker_status = get_worker_status(worker)
@@ -84,7 +84,7 @@ def cluster_main(request):
'attention': sum(
1 for worker in enabled_workers
if worker.sync_state['state'] == 'error' or worker.error_status or worker.warnings['stopped_services']
or worker.warnings['certificate_issues'] or not worker.is_online
or worker.warnings['certificate_issues'] or worker.warnings['section_warnings'] or not worker.is_online
),
},
'cache_refresh_interval': settings.WIREGUARD_STATUS_CACHE_REFRESH_INTERVAL,
@@ -112,6 +112,7 @@ def worker_details(request):
'name': section_name,
'state': section_state.get('state', 'pending'),
'message': section_state.get('message', ''),
'warning': section_state.get('warning', ''),
'updated': parse_datetime(reported.get('updated', '')) if reported.get('updated') else None,
})