middleware

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package middleware provides gRPC interceptors for MongoRPC.

Package middleware provides circuit breaker for MongoRPC.

Package middleware provides health check support for MongoRPC.

Package middleware provides gRPC interceptors for MongoRPC.

Package middleware provides gRPC interceptors for MongoRPC.

Package middleware provides rate limiting for MongoRPC.

Package middleware provides request validation for MongoRPC.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthInterceptor

func AuthInterceptor(config *AuthConfig) grpc.UnaryServerInterceptor

AuthInterceptor provides API key and JWT authentication.

func CircuitBreakerInterceptor

func CircuitBreakerInterceptor(cb *CircuitBreaker) grpc.UnaryServerInterceptor

CircuitBreakerInterceptor creates a circuit breaker interceptor.

func HealthInterceptor

func HealthInterceptor(h *HealthChecker, serviceName string) grpc.UnaryServerInterceptor

HealthInterceptor creates a health check interceptor that rejects requests when unhealthy.

func LoggingInterceptor

func LoggingInterceptor() grpc.UnaryServerInterceptor

LoggingInterceptor logs all gRPC requests and responses.

func MetricsInterceptor

func MetricsInterceptor(m *Metrics) grpc.UnaryServerInterceptor

MetricsInterceptor collects request metrics.

func RateLimitInterceptor

func RateLimitInterceptor(rl *RateLimiter) grpc.UnaryServerInterceptor

RateLimitInterceptor creates a rate limiting interceptor.

func RecoveryInterceptor

func RecoveryInterceptor() grpc.UnaryServerInterceptor

RecoveryInterceptor recovers from panics and returns internal errors.

func RequiredFields

func RequiredFields(fields map[string]string) error

RequiredFields checks that required string fields are not empty.

func StreamAuthInterceptor

func StreamAuthInterceptor(config *AuthConfig) grpc.StreamServerInterceptor

StreamAuthInterceptor provides authentication for streaming RPCs.

func StreamCircuitBreakerInterceptor

func StreamCircuitBreakerInterceptor(cb *CircuitBreaker) grpc.StreamServerInterceptor

StreamCircuitBreakerInterceptor creates a streaming circuit breaker interceptor.

func StreamLoggingInterceptor

func StreamLoggingInterceptor() grpc.StreamServerInterceptor

StreamLoggingInterceptor logs streaming gRPC requests.

func StreamMetricsInterceptor

func StreamMetricsInterceptor(m *Metrics) grpc.StreamServerInterceptor

StreamMetricsInterceptor collects streaming request metrics.

func StreamRateLimitInterceptor

func StreamRateLimitInterceptor(rl *RateLimiter) grpc.StreamServerInterceptor

StreamRateLimitInterceptor creates a streaming rate limiting interceptor.

func StreamRecoveryInterceptor

func StreamRecoveryInterceptor() grpc.StreamServerInterceptor

StreamRecoveryInterceptor recovers from panics in streaming handlers.

func StreamValidationInterceptor

func StreamValidationInterceptor() grpc.StreamServerInterceptor

StreamValidationInterceptor validates streaming requests.

func ValidationInterceptor

func ValidationInterceptor() grpc.UnaryServerInterceptor

ValidationInterceptor validates requests that implement Validator.

Types

type AuthConfig

type AuthConfig struct {
	// APIKeys is a list of valid API keys
	APIKeys []string
	// JWTSecret is the secret for JWT validation
	JWTSecret string
	// SkipMethods are methods that don't require authentication
	SkipMethods []string
}

AuthConfig holds authentication configuration.

type CircuitBreaker

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

CircuitBreaker implements the circuit breaker pattern.

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker.

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow checks if the request should be allowed.

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure records a failed call.

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a successful call.

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

State returns the current circuit state.

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	FailureThreshold int           // Failures before opening
	SuccessThreshold int           // Successes in half-open to close
	Timeout          time.Duration // Time before half-open
	OnStateChange    func(from, to CircuitState)
}

CircuitBreakerConfig configures the circuit breaker.

type CircuitState

type CircuitState int

CircuitState represents the state of the circuit breaker.

const (
	CircuitClosed CircuitState = iota
	CircuitOpen
	CircuitHalfOpen
)

func (CircuitState) String

func (s CircuitState) String() string

type HealthChecker

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

HealthChecker manages service health status.

func NewHealthChecker

func NewHealthChecker() *HealthChecker

NewHealthChecker creates a new health checker.

func (*HealthChecker) Check

Check implements the gRPC health check service.

func (*HealthChecker) GetServingStatus

GetServingStatus gets the serving status for a service.

func (*HealthChecker) SetServingStatus

func (h *HealthChecker) SetServingStatus(service string, status grpc_health_v1.HealthCheckResponse_ServingStatus)

SetServingStatus sets the serving status for a service.

func (*HealthChecker) Watch

Watch implements the streaming health check.

type Metrics

type Metrics struct {
	RequestCount    map[string]int64
	RequestDuration map[string]time.Duration
	ErrorCount      map[string]int64
}

Metrics holds the metrics for the server.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new Metrics instance.

func (*Metrics) GetMetrics

func (m *Metrics) GetMetrics() map[string]interface{}

GetMetrics returns the current metrics snapshot.

type RateLimiter

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

RateLimiter provides token bucket rate limiting.

func NewRateLimiter

func NewRateLimiter(rate float64, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(key string) bool

Allow checks if a request is allowed for the given key.

type Validator

type Validator interface {
	Validate() error
}

Validator interface for request validation.

Jump to

Keyboard shortcuts

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