Add independent MIT account-rate500 policy source supplement

This commit is contained in:
2026-11-06 16:16:55 +00:00
parent ac40dd3a6c
commit c4f43cca5c
11 changed files with 950 additions and 0 deletions
@@ -0,0 +1,149 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from control.models import Node, Subscription
from .policy import DEFAULT_KIB_PER_SECOND, MAX_KIB_PER_SECOND, MAX_BYTES_PER_SECOND, MAX_BUCKET_MICROBYTES
ACCOUNT_BANDWIDTH_EXCEPTION_KEY = "owner-approved-20261022"
APPROVED_EXEMPT_USER_ID = 5
class AccountBandwidthPolicy(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
primary_key=True, related_name="shared_bandwidth_policy")
upload_limit_kib_per_second = models.PositiveSmallIntegerField(
"账号上传上限(KiB/s)", default=DEFAULT_KIB_PER_SECOND, db_default=DEFAULT_KIB_PER_SECOND,
validators=[MinValueValidator(1), MaxValueValidator(MAX_KIB_PER_SECOND)])
download_limit_kib_per_second = models.PositiveSmallIntegerField(
"账号下载上限(KiB/s)", default=DEFAULT_KIB_PER_SECOND, db_default=DEFAULT_KIB_PER_SECOND,
validators=[MinValueValidator(1), MaxValueValidator(MAX_KIB_PER_SECOND)])
class Meta:
verbose_name = "账户带宽策略"
verbose_name_plural = "账户带宽策略"
constraints = [
models.CheckConstraint(condition=models.Q(upload_limit_kib_per_second__gte=1,
upload_limit_kib_per_second__lte=MAX_KIB_PER_SECOND), name="sb_policy_upload_bounded"),
models.CheckConstraint(condition=models.Q(download_limit_kib_per_second__gte=1,
download_limit_kib_per_second__lte=MAX_KIB_PER_SECOND), name="sb_policy_download_bounded"),
]
def __str__(self):
return str(self.user)
class ProtectedExemptionQuerySet(models.QuerySet):
def update(self, **kwargs):
raise ValidationError("受控豁免不能通过普通查询修改。")
def delete(self):
raise ValidationError("受控豁免不能通过普通查询删除。")
def bulk_create(self, *args, **kwargs):
raise ValidationError("受控豁免仅可通过审核后的数据迁移建立。")
def bulk_update(self, *args, **kwargs):
raise ValidationError("受控豁免不能通过普通查询修改。")
class AccountBandwidthExemption(models.Model):
policy_key = models.CharField(primary_key=True, max_length=64,
default=ACCOUNT_BANDWIDTH_EXCEPTION_KEY, editable=False)
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
related_name="shared_bandwidth_exemption", editable=False)
created_at = models.DateTimeField(auto_now_add=True)
objects = ProtectedExemptionQuerySet.as_manager()
class Meta:
verbose_name = "受控账号免限速绑定"
verbose_name_plural = "受控账号免限速绑定"
default_permissions = ("view",)
constraints = [models.CheckConstraint(
condition=models.Q(policy_key=ACCOUNT_BANDWIDTH_EXCEPTION_KEY, user_id=APPROVED_EXEMPT_USER_ID),
name="sb_single_approved_exemption")]
def save(self, *args, **kwargs):
raise ValidationError("受控豁免仅可通过审核后的数据迁移建立。")
def delete(self, *args, **kwargs):
raise ValidationError("受控豁免不能由普通后台操作删除。")
class AccountBandwidthBucket(models.Model):
"""Primary-DB account/direction credit, shared by every subscription/node.
Microbytes preserve sub-byte refill fractions without floating point. These
rows and the grant ledger are authorizations, not traffic measurements.
"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
related_name="bandwidth_budget_buckets")
direction = models.CharField(max_length=8, choices=[("upload", "upload"), ("download", "download")])
available_microbytes = models.PositiveBigIntegerField(default=0)
rate_bytes_per_second = models.PositiveIntegerField()
exempt = models.BooleanField(default=False)
refilled_at = models.DateTimeField()
class Meta:
default_permissions = ()
constraints = [
models.UniqueConstraint(fields=["user", "direction"], name="unique_account_bandwidth_bucket"),
models.CheckConstraint(condition=models.Q(direction__in=["upload", "download"]),
name="bandwidth_bucket_direction"),
models.CheckConstraint(condition=models.Q(rate_bytes_per_second__gte=1,
rate_bytes_per_second__lte=MAX_BYTES_PER_SECOND),
name="bandwidth_bucket_rate_bounded"),
models.CheckConstraint(condition=models.Q(available_microbytes__lte=MAX_BUCKET_MICROBYTES),
name="bandwidth_bucket_credit_bounded"),
]
class AccountBandwidthExecutor(models.Model):
"""One durable fencing generation per node; never reset/delete on restart."""
node = models.OneToOneField(Node, on_delete=models.PROTECT, primary_key=True,
related_name="bandwidth_budget_executor")
epoch = models.PositiveBigIntegerField()
boot_id = models.UUIDField()
registration_id = models.UUIDField()
opened_at = models.DateTimeField()
clock_floor = models.DateTimeField()
class Meta:
default_permissions = ()
constraints = [models.CheckConstraint(condition=models.Q(epoch__gte=1, epoch__lte=9223372036854775807),
name="bandwidth_executor_epoch_bounded")]
class AccountBandwidthLane(models.Model):
"""Fixed one-response replay slot, not a per-RPC ledger.
Same-epoch sequence/session state must never be reset. Older-epoch rows may
be reused only while holding the durable node executor fence.
"""
node = models.ForeignKey(Node, on_delete=models.PROTECT, related_name="bandwidth_budget_lanes")
subscription = models.ForeignKey(Subscription, on_delete=models.PROTECT, related_name="bandwidth_budget_lanes")
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="bandwidth_budget_lanes")
direction = models.CharField(max_length=8, choices=[("upload", "upload"), ("download", "download")])
executor_epoch = models.PositiveBigIntegerField()
credential_uuid = models.UUIDField()
session_id = models.UUIDField()
sequence = models.PositiveBigIntegerField()
fingerprint = models.CharField(max_length=64)
response = models.CharField(max_length=2048)
issued_at = models.DateTimeField()
class Meta:
default_permissions = ()
constraints = [
models.UniqueConstraint(fields=["node", "subscription", "direction"], name="unique_account_bandwidth_lane"),
models.CheckConstraint(condition=models.Q(direction__in=["upload", "download"]), name="bandwidth_lane_direction"),
models.CheckConstraint(condition=models.Q(executor_epoch__gte=1, executor_epoch__lte=9223372036854775807,
sequence__gte=1, sequence__lte=9223372036854775807),
name="bandwidth_lane_identity_bounded"),
]