foxtimeout

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 14 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.
Usage
package main

import (
	"github.com/tigerwill90/fox"
	"github.com/tigerwill90/foxtimeout"
	"log"
	"net/http"
	"time"
)

func main() {

	f := fox.New(
		fox.DefaultOptions(),
		fox.WithMiddlewareFor(fox.RouteHandlers, foxtimeout.Middleware(50*time.Microsecond)),
	)
	f.MustHandle(http.MethodGet, "/hello/{name}", func(c fox.Context) {
		time.Sleep(10 * time.Millisecond)
		_ = c.String(http.StatusOK, "hello %s\n", c.Param("name"))
	})

	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(r *http.Request) 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 false 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.

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.

Jump to

Keyboard shortcuts

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