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.
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.
go get -u github.com/fox-toolkit/timeout- 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.
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)
}
}