mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
add http timeout example
This commit is contained in:
parent
611e981f3a
commit
d8dde0b958
|
@ -29,6 +29,7 @@
|
|||
* [Use Iris as a single http.Handler](http-server/custom-httpserver/std-way/main.go)
|
||||
* [Multi Instances](http-server/custom-httpserver/multi/main.go)
|
||||
* [HTTP/3 Quic](http-server/http3-quic)
|
||||
* [Timeout](http-server/timeout/main.go)
|
||||
* Configuration
|
||||
* [Functional](configuration/functional/main.go)
|
||||
* [Configuration Struct](configuration/from-configuration-structure/main.go)
|
||||
|
|
43
_examples/http-server/timeout/main.go
Normal file
43
_examples/http-server/timeout/main.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/test", func(ctx iris.Context) {
|
||||
w := new(worker)
|
||||
result := w.Work(ctx)
|
||||
ctx.WriteString(result)
|
||||
})
|
||||
|
||||
app.Listen(":8080", iris.WithTimeout(4*time.Second))
|
||||
}
|
||||
|
||||
type worker struct{}
|
||||
|
||||
func (w *worker) Work(ctx context.Context) string {
|
||||
t := time.Tick(time.Second)
|
||||
times := 0
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
println("context.Done: canceled")
|
||||
return "Work canceled"
|
||||
case <-t:
|
||||
times++
|
||||
println("Doing some work...")
|
||||
|
||||
if times > 5 {
|
||||
return "Work is done with success"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "nothing to do here"
|
||||
}
|
|
@ -209,10 +209,12 @@ func WithKeepAlive(keepAliveDur time.Duration) Configurator {
|
|||
}
|
||||
|
||||
// WithTimeout sets the `Configuration.Timeout` field to the given duration.
|
||||
func WithTimeout(timeoutDur time.Duration, htmlBody string) Configurator {
|
||||
func WithTimeout(timeoutDur time.Duration, htmlBody ...string) Configurator {
|
||||
return func(app *Application) {
|
||||
app.config.Timeout = timeoutDur
|
||||
app.config.TimeoutMessage = htmlBody
|
||||
if len(htmlBody) > 0 {
|
||||
app.config.TimeoutMessage = htmlBody[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
18
iris.go
18
iris.go
|
@ -643,6 +643,24 @@ func (app *Application) Build() error {
|
|||
|
||||
if app.config.Timeout > 0 {
|
||||
app.Router.SetTimeoutHandler(app.config.Timeout, app.config.TimeoutMessage)
|
||||
|
||||
app.ConfigureHost(func(su *Supervisor) {
|
||||
if su.Server.ReadHeaderTimeout == 0 {
|
||||
su.Server.ReadHeaderTimeout = app.config.Timeout + 5*time.Second
|
||||
}
|
||||
|
||||
if su.Server.ReadTimeout == 0 {
|
||||
su.Server.ReadTimeout = app.config.Timeout + 10*time.Second
|
||||
}
|
||||
|
||||
if su.Server.WriteTimeout == 0 {
|
||||
su.Server.WriteTimeout = app.config.Timeout + 15*time.Second
|
||||
}
|
||||
|
||||
if su.Server.IdleTimeout == 0 {
|
||||
su.Server.IdleTimeout = app.config.Timeout + 25*time.Second
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// re-build of the router from outside can be done with
|
||||
|
|
Loading…
Reference in New Issue
Block a user