Loading documentation...
Loading documentation...
Loading documentation...
Adds a timeout to request processing. Cancels the request context if the timeout is exceeded.
// 30 second timeout
s.Use(middleware.Timeout(30 * time.Second))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
// Custom timeout response
helix.ServiceUnavailable(w, "Request timeout")
},
SkipFunc: func(r *http.Request) bool {
// Don't timeout long-running operations
return r.URL.Path == "/long-task"
},
}))The middleware:
s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
helix.WriteProblem(w, helix.ErrGatewayTimeout.WithDetail(
"Request processing exceeded timeout",
))
},
}))Skip timeout for long-running operations:
s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
SkipFunc: func(r *http.Request) bool {
// Don't timeout file uploads or long tasks
return r.URL.Path == "/upload" || r.URL.Path == "/long-task"
},
}))Handlers should respect context cancellation:
s.GET("/process", helix.HandleCtx(func(c *helix.Ctx) error {
ctx := c.Context()
// Check if context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Do work that respects context
result, err := longRunningTask(ctx)
if err != nil {
return err
}
return c.OK(result)
}))context.Context for cancellationSkipFunc for endpoints that legitimately take longers := helix.New()
// Global timeout: 30 seconds
s.Use(middleware.Timeout(30 * time.Second))
// Shorter timeout for API routes
api := s.Group("/api", middleware.Timeout(10 * time.Second))
// No timeout for file uploads
s.POST("/upload", uploadHandler) // Not in timeout groupAdds a timeout to request processing. Cancels the request context if the timeout is exceeded.
// 30 second timeout
s.Use(middleware.Timeout(30 * time.Second))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
// Custom timeout response
helix.ServiceUnavailable(w, "Request timeout")
},
SkipFunc: func(r *http.Request) bool {
// Don't timeout long-running operations
return r.URL.Path == "/long-task"
},
}))The middleware:
s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
helix.WriteProblem(w, helix.ErrGatewayTimeout.WithDetail(
"Request processing exceeded timeout",
))
},
}))Skip timeout for long-running operations:
s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
SkipFunc: func(r *http.Request) bool {
// Don't timeout file uploads or long tasks
return r.URL.Path == "/upload" || r.URL.Path == "/long-task"
},
}))Handlers should respect context cancellation:
s.GET("/process", helix.HandleCtx(func(c *helix.Ctx) error {
ctx := c.Context()
// Check if context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Do work that respects context
result, err := longRunningTask(ctx)
if err != nil {
return err
}
return c.OK(result)
}))context.Context for cancellationSkipFunc for endpoints that legitimately take longers := helix.New()
// Global timeout: 30 seconds
s.Use(middleware.Timeout(30 * time.Second))
// Shorter timeout for API routes
api := s.Group("/api", middleware.Timeout(10 * time.Second))
// No timeout for file uploads
s.POST("/upload", uploadHandler) // Not in timeout groups.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
// Custom timeout response
helix.ServiceUnavailable(w, "Request timeout")
},
SkipFunc: func(r *http.Request) bool {
// Don't timeout long-running operations
return r.URL.Path == "/long-task"
},
}))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
helix.WriteProblem(w, helix.ErrGatewayTimeout.WithDetail(
"Request processing exceeded timeout",
))
},
}))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
SkipFunc: func(r *http.Request) bool {
// Don't timeout file uploads or long tasks
return r.URL.Path == "/upload" || r.URL.Path == "/long-task"
},
}))s.GET("/process", helix.HandleCtx(func(c *helix.Ctx) error {
ctx := c.Context()
// Check if context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Do work that respects context
result, err := longRunningTask(ctx)
if err != nil {
return err
}
return c.OK(result)
}))s := helix.New()
// Global timeout: 30 seconds
s.Use(middleware.Timeout(30 * time.Second))
// Shorter timeout for API routes
api := s.Group("/api", middleware.Timeout(10 * time.Second))
// No timeout for file uploads
s.POST("/upload", uploadHandler) // Not in timeout groups.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
// Custom timeout response
helix.ServiceUnavailable(w, "Request timeout")
},
SkipFunc: func(r *http.Request) bool {
// Don't timeout long-running operations
return r.URL.Path == "/long-task"
},
}))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
Handler: func(w http.ResponseWriter, r *http.Request) {
helix.WriteProblem(w, helix.ErrGatewayTimeout.WithDetail(
"Request processing exceeded timeout",
))
},
}))s.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Timeout: 30 * time.Second,
SkipFunc: func(r *http.Request) bool {
// Don't timeout file uploads or long tasks
return r.URL.Path == "/upload" || r.URL.Path == "/long-task"
},
}))s.GET("/process", helix.HandleCtx(func(c *helix.Ctx) error {
ctx := c.Context()
// Check if context is cancelled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Do work that respects context
result, err := longRunningTask(ctx)
if err != nil {
return err
}
return c.OK(result)
}))s := helix.New()
// Global timeout: 30 seconds
s.Use(middleware.Timeout(30 * time.Second))
// Shorter timeout for API routes
api := s.Group("/api", middleware.Timeout(10 * time.Second))
// No timeout for file uploads
s.POST("/upload", uploadHandler) // Not in timeout group