-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathmain.go
More file actions
43 lines (34 loc) · 660 Bytes
/
main.go
File metadata and controls
43 lines (34 loc) · 660 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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"
}