Timeout

February 1, 2026 ยท View on GitHub

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

Timeout

Note

This repository has been transferred from github.com/tigerwill90/foxtimeout to github.com/fox-toolkit/timeout. Existing users should update their imports and go.mod accordingly.

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

Disclaimer

Timeout's API is closely tied to the Fox router, and it will only reach v1 when the router is stabilized. During the pre-v1 phase, breaking changes may occur and will be documented in the release notes.

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 dynamic timeout configuration on a per-route & per-request basis using custom Resolver.

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.DefaultOptions(),
		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.NoTimeout))
	// 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)
	}
}