-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
46 lines (41 loc) · 888 Bytes
/
controller.go
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
44
45
46
package main
import (
"context"
"fmt"
"github.com/hktime/web-server-demo/framework"
"log"
"time"
)
func FooControllerHandler(c *framework.Context) error {
finish := make(chan struct{}, 1)
panicChan := make(chan interface{}, 1)
durationCtx, cancel := context.WithTimeout(c.BaseContext(), 1*time.Second)
defer cancel()
// mu := sync.Mutex{}
go func() {
defer func() {
if p := recover(); p != nil {
panicChan <- p
}
}()
// Do real action
time.Sleep(10 * time.Second)
c.SetOkStatus().Json("ok")
finish <- struct{}{}
}()
select {
case p := <-panicChan:
c.WriterMux().Lock()
defer c.WriterMux().Unlock()
log.Println(p)
c.SetStatus(500).Json("panic")
case <-finish:
fmt.Println("finish")
case <-durationCtx.Done():
c.WriterMux().Lock()
defer c.WriterMux().Unlock()
c.SetStatus(500).Json("time out")
c.SetHasTimeout()
}
return nil
}