foxtimeout

package module
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 15 Imported by: 0

README

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

Foxtimeout

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

Disclaimer

Foxtimeout'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/tigerwill90/foxtimeout
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 (
	"github.com/tigerwill90/fox"
	"github.com/tigerwill90/foxtimeout"
	"log"
	"net/http"
	"time"
)

func main() {
	resolver := foxtimeout.TimeoutResolverFunc(func(c fox.Context) (dt time.Duration, ok bool) {
		for annotation := range c.Route().Annotations() {
			if annotation.Key == "timeout" {
				dt, ok = annotation.Value.(time.Duration)
				return dt, ok
			}
		}
		return 0, false
	})

	f := fox.New(
		fox.DefaultOptions(),
		fox.WithMiddlewareFor(
			fox.RouteHandler,
			foxtimeout.Middleware(
				2*time.Second,
				foxtimeout.WithTimeoutResolver(resolver),
			),
		),
	)

	f.MustHandle(http.MethodGet, "/hello/{name}", func(c fox.Context) {
		_ = c.String(http.StatusOK, "hello %s\n", c.Param("name"))
	})
	f.MustHandle(http.MethodGet, "/long_job", func(c fox.Context) {
		time.Sleep(10 * time.Second)
		c.Writer().WriteHeader(http.StatusOK)
	}, fox.WithAnnotation("timeout", 15*time.Second))

	log.Fatalln(http.ListenAndServe(":8080", f))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultTimeoutResponse

func DefaultTimeoutResponse(c fox.Context)

DefaultTimeoutResponse sends a default 503 Service Unavailable response.

func Middleware

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

Middleware returns a fox.MiddlewareFunc with a specified timeout and options. This middleware function, when used, will ensure HTTP handlers don't exceed the given timeout duration.

Types

type Filter

type Filter func(c fox.Context) (skip bool)

type Option

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

func WithFilter

func WithFilter(f ...Filter) Option

WithFilter appends the provided filters to the middleware's filter list. A filter returning true will exclude the request from using the timeout handler. If no filters are provided, all requests will be handled. Keep in mind that filters are invoked for each request, so they should be simple and efficient.

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 DefaultTimeoutResponse.

func WithTimeoutResolver added in v0.17.0

func WithTimeoutResolver(resolver Resolver) Option

WithTimeoutResolver sets a custom Resolver to determine the timeout dynamically based on fox.Context. If the resolver returns false, the default timeout is applied. Keep in mind that a resolver is invoked for each request, so they should be simple and efficient.

type Resolver added in v0.17.0

type Resolver interface {
	Resolve(c fox.Context) (dt time.Duration, ok bool)
}

Resolver defines the interface for resolving a timeout duration dynamically based on fox.Context. A time.Duration is returned if a custom timeout is applicable, along with a boolean indicating if the duration was resolved.

type Timeout

type Timeout struct {
	// contains filtered or unexported fields
}

Timeout is a middleware that ensure HTTP handlers don't exceed the configured timeout duration.

func New

func New(dt time.Duration, opts ...Option) *Timeout

New creates and initializes a new Timeout middleware with the given timeout duration and optional settings.3

func (*Timeout) Timeout

func (t *Timeout) Timeout(next fox.HandlerFunc) fox.HandlerFunc

Timeout returns a fox.HandlerFunc that runs next with the given time limit.

The new handler calls next to handle each request, but if a call runs for longer than its time limit, the handler responds with a 503 Service Unavailable error and the given message in its body (if a custom response handler is not configured). After such a timeout, writes by next to its ResponseWriter will return http.ErrHandlerTimeout.

Timeout supports the http.Pusher interface but does not support the http.Hijacker or http.Flusher interfaces.

type TimeoutResolverFunc added in v0.17.0

type TimeoutResolverFunc func(c fox.Context) (dt time.Duration, ok bool)

The TimeoutResolverFunc type is an adapter to allow the use of ordinary functions as Resolver. If f is a function with the appropriate signature, TimeoutResolverFunc(f) is a TimeoutResolverFunc that calls f.

func (TimeoutResolverFunc) Resolve added in v0.17.0

func (f TimeoutResolverFunc) Resolve(c fox.Context) (dt time.Duration, ok bool)

Resolve calls f(c).

Jump to

Keyboard shortcuts

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