fetch_ml/internal/api/helpers/db_helpers.go
Jeremie Fraeys b05470b30a
refactor: improve API structure and WebSocket protocol
- Extract WebSocket protocol handling to dedicated module
- Add helper functions for DB operations, validation, and responses
- Improve WebSocket frame handling and opcodes
- Refactor dataset, job, and Jupyter handlers
- Add duplicate detection processing
2026-02-16 20:38:12 -05:00

49 lines
1.5 KiB
Go

// Package helpers provides shared utilities for WebSocket handlers.
package helpers
import (
"context"
"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 {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
// 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
}