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 ¶
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
Click to show internal directories.
Click to hide internal directories.