1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-01 09:17:02 +00:00

add http timeout example

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-01-04 23:06:39 +02:00
parent 611e981f3a
commit d8dde0b958
4 changed files with 66 additions and 2 deletions

View File

@@ -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)

View 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"
}