Skip to content
Open
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
176 changes: 176 additions & 0 deletions api/ee/src/core/entitlements/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Tracker(str, Enum):
FLAGS = "flags"
COUNTERS = "counters"
GAUGES = "gauges"
THROTTLES = "throttles"


class Flag(str, Enum):
Expand Down Expand Up @@ -47,6 +48,73 @@ class Probe(BaseModel):
delta: Optional[bool] = False


class Bucket(BaseModel):
capacity: Optional[int] = None # max tokens in the bucket
rate: Optional[int] = None # tokens added per minute
algorithm: Optional[str] = None


class Category(str, Enum):
STANDARD = "standard"
CORE_FAST = "core_fast"
CORE_SLOW = "core_slow"
TRACING_FAST = "tracing_fast"
TRACING_SLOW = "tracing_slow"
SERVICES_FAST = "services_fast"
SERVICES_SLOW = "services_slow"


class Method(str, Enum):
POST = "post"
GET = "get"
PUT = "put"
PATCH = "patch"
DELETE = "delete"
QUERY = "query"
MUTATION = "mutation"
ANY = "any"


class Mode(str, Enum):
INCLUDE = "include"
EXCLUDE = "exclude"


class Throttle(BaseModel):
bucket: Bucket
mode: Mode
categories: list[Category] | None = None
endpoints: list[tuple[Method, str]] | None = None


ENDPOINTS = {
Category.CORE_FAST: [
(Method.POST, "*/retrieve"),
],
Category.CORE_SLOW: [
# None defined yet
],
Category.TRACING_FAST: [
(Method.POST, "/otlp/v1/traces"),
],
Category.TRACING_SLOW: [
(Method.POST, "/tracing/*/query"),
#
(Method.POST, "/tracing/spans/analytics"), # LEGACY
],
Category.SERVICES_FAST: [
(Method.ANY, "/permissions/verify"),
],
Category.SERVICES_SLOW: [
# None defined yet
],
Category.STANDARD: [
# None defined yet
# CATCH ALL
],
}


CATALOG = [
{
"title": "Hobby",
Expand Down Expand Up @@ -216,6 +284,42 @@ class Probe(BaseModel):
Gauge.USERS: Quota(limit=2, strict=True, free=2),
Gauge.APPLICATIONS: Quota(strict=True),
},
Tracker.THROTTLES: [
Throttle(
categories=[
Category.STANDARD,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=120,
rate=120,
),
),
Throttle(
categories=[
Category.CORE_FAST,
Category.TRACING_FAST,
Category.SERVICES_FAST,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=1200,
rate=1200,
),
),
Throttle(
categories=[
Category.CORE_SLOW,
Category.TRACING_SLOW,
Category.SERVICES_SLOW,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=120,
rate=1,
),
),
],
},
Plan.CLOUD_V0_PRO: {
Tracker.FLAGS: {
Expand All @@ -231,6 +335,42 @@ class Probe(BaseModel):
Gauge.USERS: Quota(limit=10, strict=True, free=3),
Gauge.APPLICATIONS: Quota(strict=True),
},
Tracker.THROTTLES: [
Throttle(
categories=[
Category.STANDARD,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=360,
rate=360,
),
),
Throttle(
categories=[
Category.CORE_FAST,
Category.TRACING_FAST,
Category.SERVICES_FAST,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=3600,
rate=3600,
),
),
Throttle(
categories=[
Category.CORE_SLOW,
Category.TRACING_SLOW,
Category.SERVICES_SLOW,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=180,
rate=1,
),
),
],
},
Plan.CLOUD_V0_BUSINESS: {
Tracker.FLAGS: {
Expand All @@ -246,6 +386,42 @@ class Probe(BaseModel):
Gauge.USERS: Quota(strict=True),
Gauge.APPLICATIONS: Quota(strict=True),
},
Tracker.THROTTLES: [
Throttle(
categories=[
Category.STANDARD,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=3600,
rate=3600,
),
),
Throttle(
categories=[
Category.CORE_FAST,
Category.TRACING_FAST,
Category.SERVICES_FAST,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=36000,
rate=36000,
),
),
Throttle(
categories=[
Category.CORE_SLOW,
Category.TRACING_SLOW,
Category.SERVICES_SLOW,
],
mode=Mode.INCLUDE,
bucket=Bucket(
capacity=1800,
rate=1,
),
),
],
},
Plan.CLOUD_V0_HUMANITY_LABS: {
Tracker.FLAGS: {
Expand Down
9 changes: 8 additions & 1 deletion api/ee/src/core/subscriptions/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from ee.src.core.subscriptions.interfaces import SubscriptionsDAOInterface
from ee.src.core.entitlements.service import EntitlementsService
from oss.src.utils.caching import invalidate_cache
from ee.src.core.meters.service import MetersService

log = get_module_logger(__name__)
Expand Down Expand Up @@ -71,7 +72,13 @@ async def update(
*,
subscription: SubscriptionDTO,
) -> Optional[SubscriptionDTO]:
return await self.subscriptions_dao.update(subscription=subscription)
updated = await self.subscriptions_dao.update(subscription=subscription)
if updated:
await invalidate_cache(
namespace="entitlements:subscription",
key={"organization_id": str(updated.organization_id)},
)
return updated

async def start_reverse_trial(
self,
Expand Down
Loading