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 ¶
- func AuthInterceptor(config *AuthConfig) grpc.UnaryServerInterceptor
- func CircuitBreakerInterceptor(cb *CircuitBreaker) grpc.UnaryServerInterceptor
- func HealthInterceptor(h *HealthChecker, serviceName string) grpc.UnaryServerInterceptor
- func LoggingInterceptor() grpc.UnaryServerInterceptor
- func MetricsInterceptor(m *Metrics) grpc.UnaryServerInterceptor
- func RateLimitInterceptor(rl *RateLimiter) grpc.UnaryServerInterceptor
- func RecoveryInterceptor() grpc.UnaryServerInterceptor
- func RequiredFields(fields map[string]string) error
- func StreamAuthInterceptor(config *AuthConfig) grpc.StreamServerInterceptor
- func StreamCircuitBreakerInterceptor(cb *CircuitBreaker) grpc.StreamServerInterceptor
- func StreamLoggingInterceptor() grpc.StreamServerInterceptor
- func StreamMetricsInterceptor(m *Metrics) grpc.StreamServerInterceptor
- func StreamRateLimitInterceptor(rl *RateLimiter) grpc.StreamServerInterceptor
- func StreamRecoveryInterceptor() grpc.StreamServerInterceptor
- func StreamValidationInterceptor() grpc.StreamServerInterceptor
- func ValidationInterceptor() grpc.UnaryServerInterceptor
- type AuthConfig
- type CircuitBreaker
- type CircuitBreakerConfig
- type CircuitState
- type HealthChecker
- func (h *HealthChecker) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error)
- func (h *HealthChecker) GetServingStatus(service string) grpc_health_v1.HealthCheckResponse_ServingStatus
- func (h *HealthChecker) SetServingStatus(service string, status grpc_health_v1.HealthCheckResponse_ServingStatus)
- func (h *HealthChecker) Watch(req *grpc_health_v1.HealthCheckRequest, ...) error
- type Metrics
- type RateLimiter
- type Validator
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 ¶
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 ¶
func (h *HealthChecker) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error)
Check implements the gRPC health check service.
func (*HealthChecker) GetServingStatus ¶
func (h *HealthChecker) GetServingStatus(service string) grpc_health_v1.HealthCheckResponse_ServingStatus
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 ¶
func (h *HealthChecker) Watch(req *grpc_health_v1.HealthCheckRequest, stream grpc_health_v1.Health_WatchServer) error
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 (*Metrics) GetMetrics ¶
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.