proxy

package
v0.59.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Package proxy implements the HTTP proxy server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(config *AuthConfig) func(http.Handler) http.Handler

AuthMiddleware creates an authentication middleware. It validates the API key from the x-api-key header or Authorization header.

func GenerateCacheKey

func GenerateCacheKey(req *models.AnthropicRequest) (string, bool)

GenerateCacheKey creates a deterministic cache key from a request. Only caches requests where the response would be deterministic: - Same model, messages, system prompt, tools, and max_tokens - Excludes streaming requests (they need fresh responses) - Excludes requests with temperature > 0 (non-deterministic)

func QueueMiddleware

func QueueMiddleware(queue *RequestQueue) func(http.Handler) http.Handler

QueueMiddleware creates HTTP middleware that queues requests during outages.

func RateLimitMiddleware

func RateLimitMiddleware(limiter *RateLimiter) func(http.Handler) http.Handler

RateLimitMiddleware creates a middleware that enforces rate limiting.

Types

type AuthConfig

type AuthConfig struct {
	// Enabled indicates whether authentication is required.
	Enabled bool
	// APIKey is the required API key for authentication.
	// Clients must provide this key in the x-api-key header or Authorization header.
	APIKey string
	// AllowAnonymousHealth allows unauthenticated access to /health endpoint.
	AllowAnonymousHealth bool
	// AllowAnonymousMetrics allows unauthenticated access to /metrics endpoints.
	AllowAnonymousMetrics bool
}

AuthConfig holds authentication configuration.

type CacheEntry

type CacheEntry struct {
	Response  *models.AnthropicResponse
	CreatedAt time.Time
	Hits      int64
}

CacheEntry represents a cached response.

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker implements a simple circuit breaker pattern.

func NewCircuitBreaker

func NewCircuitBreaker(failureThreshold, successThreshold int, timeout time.Duration) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker.

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow checks if a request should be allowed.

func (*CircuitBreaker) IsOpen

func (cb *CircuitBreaker) IsOpen() bool

IsOpen returns true if the circuit is open.

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure records a failed request.

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a successful request.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() string

State returns the current state as a string.

type CostSummary

type CostSummary struct {
	TotalCostUSD      float64                    `json:"total_cost_usd"`
	InputCostUSD      float64                    `json:"input_cost_usd"`
	OutputCostUSD     float64                    `json:"output_cost_usd"`
	TotalRequests     int64                      `json:"total_requests"`
	TotalInputTokens  int64                      `json:"total_input_tokens"`
	TotalOutputTokens int64                      `json:"total_output_tokens"`
	CostPerRequest    float64                    `json:"avg_cost_per_request_usd"`
	CostPerHour       float64                    `json:"cost_per_hour_usd"`
	Uptime            string                     `json:"uptime"`
	ByProvider        map[string]ProviderSummary `json:"by_provider"`
	ByModel           map[string]ModelSummary    `json:"by_model"`
}

CostSummary represents a summary of costs.

type CostTracker

type CostTracker struct {
	// contains filtered or unexported fields
}

CostTracker tracks API costs across providers and models.

func NewCostTracker

func NewCostTracker() *CostTracker

NewCostTracker creates a new cost tracker.

func (*CostTracker) GetPricing

func (ct *CostTracker) GetPricing(model string) ModelPricing

GetPricing returns the pricing for a model.

func (*CostTracker) GetSummary

func (ct *CostTracker) GetSummary() CostSummary

GetSummary returns the current cost summary.

func (*CostTracker) GetTotalCostUSD

func (ct *CostTracker) GetTotalCostUSD() float64

GetTotalCostUSD returns the total cost in USD.

func (*CostTracker) RecordUsage

func (ct *CostTracker) RecordUsage(provider, model string, inputTokens, outputTokens int)

RecordUsage records token usage for cost tracking.

func (*CostTracker) Reset

func (ct *CostTracker) Reset()

Reset resets all cost tracking data.

func (*CostTracker) SetCustomPricing

func (ct *CostTracker) SetCustomPricing(model string, pricing ModelPricing)

SetCustomPricing sets custom pricing for a model.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles incoming Anthropic API requests.

func NewHandler

func NewHandler(cfg *config.Config) (*Handler, error)

NewHandler creates a new request handler with optimized HTTP client.

func (*Handler) GetCostTracker added in v0.33.0

func (h *Handler) GetCostTracker() *CostTracker

GetCostTracker returns the cost tracker.

func (*Handler) GetMetrics added in v0.33.0

func (h *Handler) GetMetrics() *Metrics

GetMetrics returns the current metrics.

func (*Handler) HandleCosts

func (h *Handler) HandleCosts(w http.ResponseWriter, r *http.Request)

HandleCosts handles cost tracking endpoint requests.

func (*Handler) HandleHealth

func (h *Handler) HandleHealth(w http.ResponseWriter, r *http.Request)

HandleHealth handles health check requests.

func (*Handler) HandleMessages

func (h *Handler) HandleMessages(w http.ResponseWriter, r *http.Request)

HandleMessages handles POST /v1/messages requests.

func (*Handler) HandleMetrics

func (h *Handler) HandleMetrics(w http.ResponseWriter, r *http.Request)

HandleMetrics handles metrics endpoint requests.

func (*Handler) HandleMetricsPrometheus

func (h *Handler) HandleMetricsPrometheus(w http.ResponseWriter, r *http.Request)

HandleMetricsPrometheus handles Prometheus metrics endpoint requests.

func (*Handler) HandleRoot

func (h *Handler) HandleRoot(w http.ResponseWriter, r *http.Request)

HandleRoot handles root path requests.

func (*Handler) SetCache

func (h *Handler) SetCache(cache *RequestCache)

SetCache sets the request cache.

func (*Handler) SetCircuitBreaker

func (h *Handler) SetCircuitBreaker(cb *CircuitBreaker)

SetCircuitBreaker sets the circuit breaker.

func (*Handler) SetQueue

func (h *Handler) SetQueue(queue *RequestQueue)

SetQueue sets the request queue.

func (*Handler) SetRateLimiter

func (h *Handler) SetRateLimiter(rl *RateLimiter)

SetRateLimiter sets the rate limiter for metrics reporting.

func (*Handler) SetVersion added in v0.39.4

func (h *Handler) SetVersion(version string)

SetVersion sets the handler version (used for status endpoint).

type Metrics

type Metrics struct {
	TotalRequests     int64
	SuccessRequests   int64
	ErrorRequests     int64
	StreamRequests    int64
	ToolCallRequests  int64
	TotalLatencyMs    int64
	FallbackAttempts  int64
	FallbackSuccesses int64
	StartTime         time.Time
}

Metrics tracks request statistics.

type ModelCost

type ModelCost struct {
	InputCostMicro  int64
	OutputCostMicro int64
	InputTokens     int64
	OutputTokens    int64
	Requests        int64
}

ModelCost tracks costs for a specific model.

type ModelPricing

type ModelPricing struct {
	InputPer1M  float64 // Cost per 1 million input tokens
	OutputPer1M float64 // Cost per 1 million output tokens
}

ModelPricing holds pricing information for a model (per 1M tokens).

type ModelSummary

type ModelSummary struct {
	TotalCostUSD  float64 `json:"total_cost_usd"`
	InputCostUSD  float64 `json:"input_cost_usd"`
	OutputCostUSD float64 `json:"output_cost_usd"`
	InputTokens   int64   `json:"input_tokens"`
	OutputTokens  int64   `json:"output_tokens"`
	Requests      int64   `json:"requests"`
}

ModelSummary provides cost summary for a model.

type ProviderCost

type ProviderCost struct {
	InputCostMicro  int64
	OutputCostMicro int64
	InputTokens     int64
	OutputTokens    int64
	Requests        int64
}

ProviderCost tracks costs for a specific provider.

type ProviderSummary

type ProviderSummary struct {
	TotalCostUSD  float64 `json:"total_cost_usd"`
	InputCostUSD  float64 `json:"input_cost_usd"`
	OutputCostUSD float64 `json:"output_cost_usd"`
	InputTokens   int64   `json:"input_tokens"`
	OutputTokens  int64   `json:"output_tokens"`
	Requests      int64   `json:"requests"`
}

ProviderSummary provides cost summary for a provider.

type QueueConfig

type QueueConfig struct {
	Enabled    bool
	MaxSize    int           // Maximum number of requests to queue
	MaxWait    time.Duration // Maximum time a request can wait in queue
	RetryDelay time.Duration // Delay between retry attempts
	MaxRetries int           // Maximum number of retries per request
}

QueueConfig holds configuration for the request queue.

func DefaultQueueConfig

func DefaultQueueConfig() *QueueConfig

DefaultQueueConfig returns the default queue configuration.

type QueueResult

type QueueResult struct {
	Response *http.Response
	Error    error
}

QueueResult holds the result of a queued request.

type QueueStats added in v0.50.13

type QueueStats struct {
	Queued   int64
	Dequeued int64
	Dropped  int64
	Retried  int64
	Expired  int64
	Length   int
	Paused   bool
}

QueueStats contains queue statistics.

type QueuedRequest

type QueuedRequest struct {
	Body       []byte
	CreatedAt  time.Time
	RetryCount int
	ResultCh   chan QueueResult
}

QueuedRequest represents a request waiting in the queue.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter implements a token bucket rate limiter.

func NewRateLimiter

func NewRateLimiter(requests, window, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter. requests: number of requests allowed per window window: time window in seconds burst: additional burst capacity

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow() bool

Allow checks if a request should be allowed.

func (*RateLimiter) Stats

func (rl *RateLimiter) Stats() (allowed, denied int64)

Stats returns rate limiter statistics.

func (*RateLimiter) WaitTime

func (rl *RateLimiter) WaitTime() time.Duration

WaitTime returns the duration until the next request would be allowed.

type RequestCache

type RequestCache struct {
	// contains filtered or unexported fields
}

RequestCache implements an LRU cache for API responses.

func NewRequestCache

func NewRequestCache(maxSize int, ttl time.Duration) *RequestCache

NewRequestCache creates a new request cache. maxSize: maximum number of entries (0 = unlimited, not recommended) ttl: time-to-live for entries (0 = never expire)

func (*RequestCache) Clear

func (rc *RequestCache) Clear()

Clear removes all entries from the cache.

func (*RequestCache) Get

func (rc *RequestCache) Get(key string) (*models.AnthropicResponse, bool)

Get retrieves a cached response if it exists and is not expired.

func (*RequestCache) Set

func (rc *RequestCache) Set(key string, response *models.AnthropicResponse)

Set stores a response in the cache.

func (*RequestCache) Size

func (rc *RequestCache) Size() int

Size returns the current number of entries.

func (*RequestCache) Stats

func (rc *RequestCache) Stats() (size, maxSize int, hits, misses int64, hitRate float64)

Stats returns cache statistics.

type RequestQueue

type RequestQueue struct {
	// contains filtered or unexported fields
}

RequestQueue manages request queuing during provider outages.

func NewRequestQueue

func NewRequestQueue(config *QueueConfig) *RequestQueue

NewRequestQueue creates a new request queue.

func (*RequestQueue) Close

func (q *RequestQueue) Close()

Close closes the queue and signals all waiters.

func (*RequestQueue) Dequeue

func (q *RequestQueue) Dequeue(ctx context.Context) (*QueuedRequest, error)

Dequeue removes and returns the next request from the queue. Blocks until a request is available or the queue is closed.

func (*RequestQueue) Enqueue

func (q *RequestQueue) Enqueue(body []byte) (chan QueueResult, error)

Enqueue adds a request to the queue. Returns an error if the queue is full or closed.

func (*RequestQueue) IncrementRetried

func (q *RequestQueue) IncrementRetried()

IncrementRetried increments the retry counter.

func (*RequestQueue) IsPaused

func (q *RequestQueue) IsPaused() bool

IsPaused returns whether the queue is paused.

func (*RequestQueue) Len

func (q *RequestQueue) Len() int

Len returns the current number of requests in the queue.

func (*RequestQueue) Pause

func (q *RequestQueue) Pause()

Pause temporarily pauses processing (called during outages).

func (*RequestQueue) Resume

func (q *RequestQueue) Resume()

Resume resumes processing after an outage.

func (*RequestQueue) Stats

func (q *RequestQueue) Stats() QueueStats

Stats returns queue statistics.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the CLASP proxy server.

func NewServer

func NewServer(cfg *config.Config) (*Server, error)

NewServer creates a new proxy server.

func NewServerWithVersion added in v0.33.0

func NewServerWithVersion(cfg *config.Config, version string) (*Server, error)

NewServerWithVersion creates a new proxy server with version info for status line.

func (*Server) GetHandler added in v0.33.0

func (s *Server) GetHandler() *Handler

GetHandler returns the handler for testing and metrics access.

func (*Server) GetPort added in v0.12.0

func (s *Server) GetPort() int

GetPort returns the actual port the server is running on. This is useful when auto-port selection is used.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown gracefully shuts down the server.

func (*Server) Start

func (s *Server) Start() error

Start starts the proxy server.

Jump to

Keyboard shortcuts

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