middleware

package
v0.0.0-...-e285621 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestId = "X-Request-Id"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

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

func NewMiddleware

func NewMiddleware(utility utility) *Resource

NewMiddleware creates a new middleware

func (*Resource) Timeout

func (res *Resource) Timeout(timeout time.Duration) func(next http.Handler) http.Handler

Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client.

It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.

i.e. a route/handler may look like:

r.Get("/long", func(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	processTime := time.Duration(rand.Intn(4)+1) * time.Second

	select {
	case <-ctx.Done():
		return

	case <-time.After(processTime):
		// The above channel simulates some hard work.
	}

	w.Write([]byte("done"))
})

FYI: it adopted: https://github.com/go-chi/chi/blob/master/middleware/timeout.go

Jump to

Keyboard shortcuts

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