timeout

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference tests codecov GitHub release (latest SemVer) GitHub go.mod Go version

Timeout

Timeout is a middleware for Fox which ensures that a handler does not exceed the configured timeout limit.

Getting started

Installation
go get -u github.com/fox-toolkit/timeout
Feature
  • Allows for custom timeout response to better suit specific use cases.
  • Tightly integrates with the Fox ecosystem for enhanced performance and scalability.
  • Supports per-route overrides of the handler timeout and of the connection read/write deadlines.
Usage
package main

import (
	"errors"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/fox-toolkit/fox"
	"github.com/fox-toolkit/timeout"
)

func main() {
	f := fox.MustRouter(
		fox.WithMiddleware(
			timeout.Middleware(2*time.Second),
		),
	)

	f.MustAdd(fox.MethodGet, "/hello/{name}", func(c *fox.Context) {
		_ = c.String(http.StatusOK, fmt.Sprintf("Hello %s\n", c.Param("name")))
	})
	// Disable timeout the middleware for this route
	f.MustAdd(fox.MethodGet, "/download/+{filepath}", DownloadHandler, timeout.OverrideHandler(timeout.None))
	// Use 15s timeout instead of the global 2s for this route
	f.MustAdd(fox.MethodGet, "/workflow/{id}/start", WorkflowHandler, timeout.OverrideHandler(15*time.Second))

	if err := http.ListenAndServe(":8080", f); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatalln(err)
	}
}

Documentation

Index

Examples

Constants

View Source
const None = time.Duration(0)

Variables

This section is empty.

Functions

func DefaultResponse added in v0.27.1

func DefaultResponse(c *fox.Context)

DefaultResponse sends a default 503 Service Unavailable response.

func Middleware

func Middleware(dt time.Duration, opts ...Option) fox.MiddlewareFunc

Middleware returns a fox.MiddlewareFunc that runs handlers with the given time limit.

The middleware calls the next handler to handle each request, but if a call runs for longer than its time limit, the handler responds with a 503 Service Unavailable error (see DefaultResponse). After such a timeout, writes by the handler to its ResponseWriter will return http.ErrHandlerTimeout.

The timeout middleware supports the http.Pusher interface but does not support the http.Hijacker or http.Flusher interfaces.

Individual routes can override the timeout duration using the OverrideHandler option. It's also possible to set the read and write deadline for individual route using the OverrideRead and OverrideWrite option. If dt <= 0 (or None), this is a passthrough middleware but per-route options remain effective.

func OverrideHandler

func OverrideHandler(dt time.Duration) fox.RouteOption

OverrideHandler returns a RouteOption that sets a custom timeout duration for a specific route. This allows individual routes to have different timeout values than the global timeout. Passing a value <= 0 (or None) disables the timeout for this route.

Example
f, err := fox.NewRouter(
	fox.WithMiddleware(Middleware(2 * time.Second)),
)
if err != nil {
	panic(err)
}

f.MustAdd(fox.MethodGet, "/hello/{name}", func(c *fox.Context) {
	_ = c.String(http.StatusOK, fmt.Sprintf("hello %s\n", c.Param("name")))
})

f.MustAdd(fox.MethodGet, "/long", func(c *fox.Context) {
	time.Sleep(10 * time.Second)
	c.Writer().WriteHeader(http.StatusOK)
}, OverrideHandler(12*time.Second))

f.MustAdd(fox.MethodGet, "/no-timeout", func(c *fox.Context) {
	c.Writer().WriteHeader(http.StatusOK)
}, OverrideHandler(None))

func OverrideRead

func OverrideRead(dt time.Duration) fox.RouteOption

OverrideRead returns a RouteOption that sets the read deadline for the underlying connection. This controls how long the server will wait before timing out while reading the request body. Passing a value <= 0 (or None) clears the read deadline for this route.

func OverrideWrite

func OverrideWrite(dt time.Duration) fox.RouteOption

OverrideWrite returns a RouteOption that sets the write deadline for the underlying connection. This controls how long the server will wait before timing out writes to the client. Passing a value <= 0 (or None) clears the write deadline for this route.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithResponse

func WithResponse(h fox.HandlerFunc) Option

WithResponse sets a custom response handler function for the middleware. This function will be invoked when a timeout occurs, allowing for custom responses to be sent back to the client. If not set, the middleware use DefaultResponse.

Jump to

Keyboard shortcuts

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