-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
98 lines (87 loc) · 2.06 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"ProjectName/framework"
"context"
"fmt"
"log"
"time"
)
func FooControllerHandler(c *framework.Context) error {
// 这个 channal 负责通知结束
finish := make(chan struct{}, 1)
// 这个 channel 负责通知 panic 异常
panicChan := make(chan interface{}, 1)
durationCtx, cancel := context.WithTimeout(c.BaseContext(), time.Duration(1*time.Second))
defer cancel()
go func() {
// 这里增加异常处理
defer func() {
if p := recover(); p != nil {
panicChan <- p
}
}()
// 这里做具体的业务
time.Sleep(10 * time.Second)
c.Json(200, "ok")
finish <- struct{}{}
}()
select {
case p := <-panicChan:
c.WriterMux().Lock()
defer c.WriterMux().Unlock()
log.Println(p)
c.Json(500, "panic")
case <-finish:
fmt.Println("finish")
case <-durationCtx.Done():
c.WriterMux().Lock()
defer c.WriterMux().Unlock()
c.Json(500, "time out")
c.SetHasTimeout()
}
return nil
}
// func Foo(request *http.Request, response http.ResponseWriter) {
// obj := map[string]interface{}{
// "errno": 50001,
// "errmsg": "inner error",
// "data": nil,
// }
// response.Header().Set("Content-Type", "application/json")
// foo := request.PostFormValue("foo")
// if foo == "" {
// foo = "10"
// }
// fooInt, err := strconv.Atoi(foo)
// if err != nil {
// response.WriteHeader(500)
// return
// }
// obj["data"] = fooInt
// byt, err := json.Marshal(obj)
// if err != nil {
// response.WriteHeader(500)
// return
// }
// response.WriteHeader(200)
// response.Write(byt)
// return
// }
// func Foo2(ctx *framework.Context) error {
// obj := map[string]interface{}{
// "errno": 50001,
// "errmsg": "inner error",
// "data": nil,
// }
// fooInt := ctx.FormInt("foo", 10)
// obj["data"] = fooInt
// return ctx.Json(http.StatusOK, obj)
// }
// func Foo3(ctx *framework.Context) error {
// rdb := redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Password: "", // no password set
// DB: 0, // use default DB
// })
// return rdb.Set(ctx, "key", "value", 0).Err()
// }