models

package
v1.0.48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IdempotencyStatusPending    = "PENDING"
	IdempotencyStatusProcessing = "PROCESSING"
	IdempotencyStatusCompleted  = "COMPLETED"
	IdempotencyStatusFailed     = "FAILED"
)

Status constants

View Source
const (
	RateLimitByIP     = "ip"
	RateLimitByUser   = "user"
	RateLimitByTenant = "tenant"
	RateLimitByCustom = "custom"
)

RateLimitType constants for different rate limiting strategies

Variables

This section is empty.

Functions

func HashRequest

func HashRequest(request interface{}) (string, error)

HashRequest creates a SHA256 hash of the request for comparison

Types

type IdempotencyRecord

type IdempotencyRecord struct {
	// Primary key: idempotency key (from header or request)
	IdempotencyKey string `dynamorm:"pk" json:"idempotency_key"`

	// Sort key: constant value for single item per key
	SK string `dynamorm:"sk" json:"sk" default:"IDEMPOTENCY"`

	// GSIs for querying
	FunctionName string    `dynamorm:"index:gsi-function,pk" json:"function_name"`
	TenantID     string    `dynamorm:"index:gsi-tenant,pk" json:"tenant_id,omitempty"`
	Status       string    `dynamorm:"index:gsi-status,pk" json:"status"`
	Timestamp    time.Time `dynamorm:"index:gsi-timestamp,pk" json:"timestamp"`

	// Request/Response data
	RequestHash string `json:"request_hash"`
	RequestBody string `dynamorm:"json" json:"request_body"` // Stored as JSON
	Response    string `dynamorm:"json" json:"response"`     // Can be up to 400KB
	StatusCode  int    `json:"status_code"`

	// State management
	LockToken   string    `json:"lock_token,omitempty"`
	LockedUntil time.Time `json:"locked_until,omitempty"`

	// TTL for automatic cleanup
	ExpiresAt time.Time `dynamorm:"ttl" json:"expires_at"`

	// Metadata
	CreatedAt   time.Time `dynamorm:"created_at" json:"created_at"`
	UpdatedAt   time.Time `dynamorm:"updated_at" json:"updated_at"`
	CompletedAt time.Time `json:"completed_at,omitempty"`

	// Error tracking
	ErrorMessage string `json:"error_message,omitempty"`
	RetryCount   int    `json:"retry_count,omitempty"`
}

IdempotencyRecord stores idempotent request data

func NewIdempotencyRecord

func NewIdempotencyRecord(key string, functionName string) *IdempotencyRecord

NewIdempotencyRecord creates a new idempotency record

func (*IdempotencyRecord) CanRetry

func (i *IdempotencyRecord) CanRetry() bool

CanRetry checks if the record can be retried

func (*IdempotencyRecord) GetRequest

func (i *IdempotencyRecord) GetRequest(target interface{}) error

GetRequest unmarshals the stored request

func (*IdempotencyRecord) GetResponse

func (i *IdempotencyRecord) GetResponse(target interface{}) error

GetResponse unmarshals the stored response

func (*IdempotencyRecord) IsLocked

func (i *IdempotencyRecord) IsLocked() bool

IsLocked checks if the record is currently locked for processing

func (*IdempotencyRecord) SetError

func (i *IdempotencyRecord) SetError(err error)

SetError marks the record as failed with an error message

func (*IdempotencyRecord) SetRequest

func (i *IdempotencyRecord) SetRequest(request interface{}) error

SetRequest stores the request body and hash

func (*IdempotencyRecord) SetResponse

func (i *IdempotencyRecord) SetResponse(response interface{}, statusCode int) error

SetResponse stores the response and marks as completed

func (*IdempotencyRecord) TableName

func (i *IdempotencyRecord) TableName() string

TableName returns the DynamoDB table name from environment

type RateLimitRecord

type RateLimitRecord struct {
	// Primary key: identifier (could be IP, UserID, TenantID+UserID, etc.)
	Identifier string `dynamorm:"pk" json:"identifier"`

	// Sort key: window timestamp (for sliding window rate limiting)
	WindowTime string `dynamorm:"sk" json:"window_time"`

	// GSI for querying by different dimensions
	IPAddress string `dynamorm:"index:gsi-ip,pk" json:"ip_address,omitempty"`
	UserID    string `dynamorm:"index:gsi-user,pk" json:"user_id,omitempty"`
	TenantID  string `dynamorm:"index:gsi-tenant,pk" json:"tenant_id,omitempty"`

	// Rate limit data
	Count     int    `json:"count"`
	BucketKey string `dynamorm:"index:gsi-bucket,pk" json:"bucket_key"`

	// TTL for automatic cleanup (set to window end + buffer)
	ExpiresAt time.Time `dynamorm:"ttl" json:"expires_at"`

	// Metadata
	CreatedAt time.Time `dynamorm:"created_at" json:"created_at"`
	UpdatedAt time.Time `dynamorm:"updated_at" json:"updated_at"`
}

RateLimitRecord is compatible with both DynamORM and the Limited library

func NewRateLimitRecord

func NewRateLimitRecord(identifier string, window time.Time) *RateLimitRecord

NewRateLimitRecord creates a new rate limit record with defaults

func (*RateLimitRecord) IncrementCount

func (r *RateLimitRecord) IncrementCount()

IncrementCount atomically increments the count

func (*RateLimitRecord) IsExpired

func (r *RateLimitRecord) IsExpired() bool

IsExpired checks if the record has expired

func (*RateLimitRecord) SetIdentifierMetadata

func (r *RateLimitRecord) SetIdentifierMetadata(identifierType string, value string)

SetIdentifierMetadata sets IP, UserID, and TenantID based on the identifier type

func (*RateLimitRecord) TableName

func (r *RateLimitRecord) TableName() string

TableName returns the DynamoDB table name from environment

type SlidingWindowEntry

type SlidingWindowEntry struct {
	// Composite key: RateLimitKey#Timestamp
	PK string `dynamorm:"pk" json:"-"`
	SK string `dynamorm:"sk" json:"-"`

	// Attributes
	RateLimitKey string    `json:"rate_limit_key"`
	Timestamp    time.Time `json:"timestamp"`
	Weight       int       `json:"weight,omitempty"` // For weighted rate limiting
	RequestID    string    `json:"request_id"`

	// TTL for automatic cleanup (set to window duration + buffer)
	ExpiresAt int64 `dynamorm:"ttl" json:"-"`
}

SlidingWindowEntry represents a single request in the sliding window

func (*SlidingWindowEntry) GetTableName

func (s *SlidingWindowEntry) GetTableName() string

GetTableName returns the DynamoDB table name for this model

func (*SlidingWindowEntry) Key

func (s *SlidingWindowEntry) Key(rateLimitKey string, timestamp time.Time)

Key structure for efficient queries

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL