A quota management plugin for the portal framework that provides flexible quota enforcement with multiple policies, usage tracking, and comprehensive API extensions for managing storage, upload, and download quotas.
-
Multiple Enforcement Policies
- HARD_LIMITS: Strict limits blocking operations when exceeded
- ALLOWANCE: Pre-paid allowance consumption with grant tracking
- THRESHOLD: Soft limits with warnings, allows exceeding threshold
- UNLIMITED: No limits, only records usage
-
Usage Tracking
- Records upload/download/storage operations
- Tracks daily and detailed usage
- Provides usage history queries
-
Plan Management
- Reusable quota templates with limits and thresholds
- Per-user configuration with plan assignment
- Custom limits override support
-
API Extensions
- Admin API for plan and allowance management
- User-facing API for usage queries
- Swagger/OpenAPI documentation
-
Observability
- Prometheus metrics for policy checks and operations
- OpenTelemetry tracing throughout
- Comprehensive logging
- Go 1.26.0 or later
- Portal framework
# Build the plugin
go build -o quota.so
# Build with build metadata
go build -ldflags "-X main.Version=1.0.0 -X main.GitCommit=$(git rev-parse HEAD)"Copy the built plugin to your portal plugins directory:
cp quota.so /path/to/portal/plugins/The plugin is configured through the portal configuration system with the following options:
| Option | Type | Default | Description |
|---|---|---|---|
enabled |
bool | true |
Controls whether the quota service is active |
default_enforcement_policy |
string | HARD_LIMITS |
Default policy for new users (HARD_LIMITS, UNLIMITED, ALLOWANCE, THRESHOLD) |
reconciliation_hour |
int | 2 |
Hour (0-23) when daily quota reconciliation runs |
history_retention_days |
int | 90 |
How long to retain quota history records |
detailed_retention_days |
int | 30 |
How long to retain detailed usage records |
enable_shared_usage |
bool | true |
Enables shared usage tracking |
shared_usage_precision |
int | 2 |
Decimal precision for shared usage (0-10) |
default_quota_plan_name |
string | default |
Quota plan assigned to new users |
quota:
enabled: true
default_enforcement_policy: "HARD_LIMITS"
reconciliation_hour: 2
history_retention_days: 90
detailed_retention_days: 30
enable_shared_usage: true
shared_usage_precision: 2
default_quota_plan_name: "default"The plugin provides an admin API extension at /api/quota/ for managing plans and allowances:
-
Plan Management
GET /api/quota/plans- List all quota plansPOST /api/quota/plans- Create a new quota planGET /api/quota/plans/:id- Get a specific planPUT /api/quota/plans/:id- Update a planDELETE /api/quota/plans/:id- Delete a planPOST /api/quota/plans/:id/default- Set as default plan
-
Allowance Management
GET /api/quota/allowances- List allowancesPOST /api/quota/allowances- Create an allowance grantPUT /api/quota/allowances/:id- Update an allowanceDELETE /api/quota/allowances/:id- Deactivate an allowance
-
System Management
GET /api/quota/system/stats- Get system statisticsPOST /api/quota/system/reconcile- Trigger quota reconciliationPOST /api/quota/system/cleanup- Clean up old usage recordsGET /api/quota/system/config- Get current configuration
Authenticated users can query their quota status and usage history:
GET /api/account/quota- Get current quota statusGET /api/account/quota/history- Get quota usage history for charting and analytics
portal-plugin-quota/
├── quota.go # Plugin registration
├── core/ # Public interfaces and types
│ ├── quota_service.go
│ ├── usage_manager.go
│ ├── grant_manager.go
│ └── ...
├── internal/
│ ├── service/
│ │ ├── quota/ # Main quota service
│ │ ├── managers/ # Specialized managers
│ │ └── policies/ # Policy enforcement implementations
│ ├── api/ # HTTP endpoints
│ ├── db/
│ │ ├── models/ # Database models
│ │ └── migrations/ # Database migrations
│ └── config/ # Configuration schema
└── build/ # Build metadata
# Run all tests
go test ./...
# Run tests for a specific package
go test ./internal/service/quota
# Run tests with verbose output
go test -v ./internal/service/policies
# Run a specific test
go test -run TestHardLimitsPolicyEnforcer_CheckUploadQuota_Success ./internal/service/policies
# Run tests with coverage
go test ./... -cover
# Run integration tests (require database)
go test ./internal/service/policies/hard_limits_integration_test.go -v# Install mockery for mock generation
go install github.com/vektra/mockery/v3@v3.7.0
# Generate mocks
$HOME/go/bin/mockery# Run go vet
go vet ./...
# Run golangci-lint
golangci-lint runThe plugin follows a layered architecture:
- Core Layer (
core/): Defines public interfaces and types - Service Layer (
internal/service/): Implements business logicquota/: Main quota service implementationmanagers/: Specialized managers (usage, grants, config)policies/: Policy enforcement strategies
- API Layer (
internal/api/): HTTP handlers and extensions - Database Layer (
internal/db/): GORM models and migrations
- Quota check requested via
QuotaService.CheckXQuota() - Get user config via
ConfigManager.GetUserQuotaConfig() - Get appropriate policy enforcer via
ConfigManager.GetPolicyEnforcer() - Enforcer checks limits using
LimitResolver.ResolvedEffectiveLimits() - Result includes allowed/disallowed status, reason, and details
Key models include:
QuotaPlan: Reusable quota templatesUserQuotaConfig: Per-user configurationAllowanceGrant: Pre-paid allowancesAllowanceConsumption: Consumption trackingUserUsageDetail: Usage records by typeQuotaCheckReason: Check result enumerationEnforcementPolicy: Policy enumerationUsageType: Usage type enumerationGrantType: Grant type enumeration
MIT License - see LICENSE file for details.
For issues and questions, please refer to the portal framework documentation.