-
Notifications
You must be signed in to change notification settings - Fork 12
/
go-id-builder.go
108 lines (92 loc) · 2.26 KB
/
go-id-builder.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
99
100
101
102
103
104
105
106
107
108
package main
import (
"net/http"
"runtime"
"strconv"
"strings"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// 开始启动监听key变化监听
go watch()
bind, b := config.Get("", "bind")
if !b {
bind = ":3002"
}
http.HandleFunc("/", requestID)
http.HandleFunc("/* ", requestID)
// 开始http处理
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
// 处理用户的网络请求
func requestID(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
name := r.Form.Get("name")
if name == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("name can`t be empty"))
return
}
num := 1
if tmpNum := r.Form.Get("num"); tmpNum != "" {
tmpNum2, err := strconv.Atoi(tmpNum)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("num not a int"))
return
}
num = tmpNum2
}
if tmpMaxRequestNum, b := config.Get("", "max_request_num"); b {
if tmpMaxRequestNumInt, err := strconv.Atoi(tmpMaxRequestNum); err == nil {
if num > tmpMaxRequestNumInt {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("num more than max request id num"))
return
}
} else {
errMessage := err.Error()
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errMessage))
return
}
}
if value, exists := watchList[name]; exists {
w.WriteHeader(http.StatusOK)
if num > 1 {
arr := make([]string, 0)
for i := 0; i < num; i++ {
// 从通信队列中获取数据时添加5秒的超时时间
select {
case rs := <-value:
arr = append(arr, strconv.FormatInt(rs, 10))
case <-time.After(5 * time.Second):
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("server create id timeout"))
}
}
w.Write([]byte(strings.Join(arr, ",")))
} else {
// 从通信队列中获取数据时添加5秒的超时时间
select {
case rs := <-value:
w.Write([]byte(strconv.FormatInt(rs, 10)))
case <-time.After(5 * time.Second):
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("server create id timeout"))
}
}
return
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("name can`t found"))
}