fetch_ml/internal/api/helpers/db_helpers.go
Jeremie Fraeys 420de879ff
feat(api): integrate scheduler protocol and WebSocket enhancements
Update API layer for scheduler integration:
- WebSocket handlers with scheduler protocol support
- Jobs WebSocket endpoint with priority queue integration
- Validation middleware for scheduler messages
- Server configuration with security hardening
- Protocol definitions for worker-scheduler communication
- Dataset handlers with tenant isolation checks
- Response helpers with audit context
- OpenAPI spec updates for new endpoints
2026-02-26 12:05:57 -05:00

45 lines
1.5 KiB
Go

// Package helpers provides shared utilities for WebSocket handlers.
package helpers
import (
"context"
"slices"
"time"
)
// DBContext provides a standard database operation context.
// It creates a context with the specified timeout and returns the context and cancel function.
func DBContext(timeout time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), timeout)
}
// DBContextShort returns a short-lived context for quick DB operations (3 seconds).
func DBContextShort() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 3*time.Second)
}
// DBContextMedium returns a medium-lived context for standard DB operations (5 seconds).
func DBContextMedium() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 5*time.Second)
}
// DBContextLong returns a long-lived context for complex DB operations (10 seconds).
func DBContextLong() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 10*time.Second)
}
// StringSliceContains checks if a string slice contains a specific string.
func StringSliceContains(slice []string, item string) bool {
return slices.Contains(slice, item)
}
// StringSliceFilter filters a string slice based on a predicate.
func StringSliceFilter(slice []string, predicate func(string) bool) []string {
result := make([]string, 0)
for _, s := range slice {
if predicate(s) {
result = append(result, s)
}
}
return result
}