1d08a54f by 傅小黑

add comments for toolbox packages

1 parent 68254416
...@@ -29,16 +29,13 @@ type pointerInfo struct { ...@@ -29,16 +29,13 @@ type pointerInfo struct {
29 used []int 29 used []int
30 } 30 }
31 31
32 //
33 // print the data in console 32 // print the data in console
34 //
35 func Display(data ...interface{}) { 33 func Display(data ...interface{}) {
36 display(true, data...) 34 display(true, data...)
37 } 35 }
38 36
39 // 37
40 // return string 38 // return data print string
41 //
42 func GetDisplayString(data ...interface{}) string { 39 func GetDisplayString(data ...interface{}) string {
43 return display(false, data...) 40 return display(false, data...)
44 } 41 }
...@@ -67,9 +64,7 @@ func display(displayed bool, data ...interface{}) string { ...@@ -67,9 +64,7 @@ func display(displayed bool, data ...interface{}) string {
67 return buf.String() 64 return buf.String()
68 } 65 }
69 66
70 // 67 // return data dump and format bytes
71 // return fomateinfo
72 //
73 func fomateinfo(headlen int, data ...interface{}) []byte { 68 func fomateinfo(headlen int, data ...interface{}) []byte {
74 var buf = new(bytes.Buffer) 69 var buf = new(bytes.Buffer)
75 70
...@@ -108,6 +103,7 @@ func fomateinfo(headlen int, data ...interface{}) []byte { ...@@ -108,6 +103,7 @@ func fomateinfo(headlen int, data ...interface{}) []byte {
108 return buf.Bytes() 103 return buf.Bytes()
109 } 104 }
110 105
106 // check data is golang basic type
111 func isSimpleType(val reflect.Value, kind reflect.Kind, pointers **pointerInfo, interfaces *[]reflect.Value) bool { 107 func isSimpleType(val reflect.Value, kind reflect.Kind, pointers **pointerInfo, interfaces *[]reflect.Value) bool {
112 switch kind { 108 switch kind {
113 case reflect.Bool: 109 case reflect.Bool:
...@@ -158,6 +154,7 @@ func isSimpleType(val reflect.Value, kind reflect.Kind, pointers **pointerInfo, ...@@ -158,6 +154,7 @@ func isSimpleType(val reflect.Value, kind reflect.Kind, pointers **pointerInfo,
158 return false 154 return false
159 } 155 }
160 156
157 // dump value
161 func printKeyValue(buf *bytes.Buffer, val reflect.Value, pointers **pointerInfo, interfaces *[]reflect.Value, structFilter func(string, string) bool, formatOutput bool, indent string, level int) { 158 func printKeyValue(buf *bytes.Buffer, val reflect.Value, pointers **pointerInfo, interfaces *[]reflect.Value, structFilter func(string, string) bool, formatOutput bool, indent string, level int) {
162 var t = val.Kind() 159 var t = val.Kind()
163 160
...@@ -367,6 +364,7 @@ func printKeyValue(buf *bytes.Buffer, val reflect.Value, pointers **pointerInfo, ...@@ -367,6 +364,7 @@ func printKeyValue(buf *bytes.Buffer, val reflect.Value, pointers **pointerInfo,
367 } 364 }
368 } 365 }
369 366
367 // dump pointer value
370 func printPointerInfo(buf *bytes.Buffer, headlen int, pointers *pointerInfo) { 368 func printPointerInfo(buf *bytes.Buffer, headlen int, pointers *pointerInfo) {
371 var anyused = false 369 var anyused = false
372 var pointerNum = 0 370 var pointerNum = 0
...@@ -434,9 +432,7 @@ func printPointerInfo(buf *bytes.Buffer, headlen int, pointers *pointerInfo) { ...@@ -434,9 +432,7 @@ func printPointerInfo(buf *bytes.Buffer, headlen int, pointers *pointerInfo) {
434 } 432 }
435 } 433 }
436 434
437 // 435 // get stack bytes
438 // get stack info
439 //
440 func stack(skip int, indent string) []byte { 436 func stack(skip int, indent string) []byte {
441 var buf = new(bytes.Buffer) 437 var buf = new(bytes.Buffer)
442 438
...@@ -455,7 +451,7 @@ func stack(skip int, indent string) []byte { ...@@ -455,7 +451,7 @@ func stack(skip int, indent string) []byte {
455 return buf.Bytes() 451 return buf.Bytes()
456 } 452 }
457 453
458 // function returns, if possible, the name of the function containing the PC. 454 // return the name of the function containing the PC if possible,
459 func function(pc uintptr) []byte { 455 func function(pc uintptr) []byte {
460 fn := runtime.FuncForPC(pc) 456 fn := runtime.FuncForPC(pc)
461 if fn == nil { 457 if fn == nil {
......
...@@ -13,12 +13,15 @@ package toolbox ...@@ -13,12 +13,15 @@ package toolbox
13 13
14 //AddHealthCheck("database",&DatabaseCheck{}) 14 //AddHealthCheck("database",&DatabaseCheck{})
15 15
16 // health checker map
16 var AdminCheckList map[string]HealthChecker 17 var AdminCheckList map[string]HealthChecker
17 18
19 // health checker interface
18 type HealthChecker interface { 20 type HealthChecker interface {
19 Check() error 21 Check() error
20 } 22 }
21 23
24 // add health checker with name string
22 func AddHealthCheck(name string, hc HealthChecker) { 25 func AddHealthCheck(name string, hc HealthChecker) {
23 AdminCheckList[name] = hc 26 AdminCheckList[name] = hc
24 } 27 }
......
...@@ -19,6 +19,7 @@ func init() { ...@@ -19,6 +19,7 @@ func init() {
19 pid = os.Getpid() 19 pid = os.Getpid()
20 } 20 }
21 21
22 // parse input command string
22 func ProcessInput(input string, w io.Writer) { 23 func ProcessInput(input string, w io.Writer) {
23 switch input { 24 switch input {
24 case "lookup goroutine": 25 case "lookup goroutine":
...@@ -44,6 +45,7 @@ func ProcessInput(input string, w io.Writer) { ...@@ -44,6 +45,7 @@ func ProcessInput(input string, w io.Writer) {
44 } 45 }
45 } 46 }
46 47
48 // record memory profile in pprof
47 func MemProf() { 49 func MemProf() {
48 if f, err := os.Create("mem-" + strconv.Itoa(pid) + ".memprof"); err != nil { 50 if f, err := os.Create("mem-" + strconv.Itoa(pid) + ".memprof"); err != nil {
49 log.Fatal("record memory profile failed: %v", err) 51 log.Fatal("record memory profile failed: %v", err)
...@@ -54,6 +56,7 @@ func MemProf() { ...@@ -54,6 +56,7 @@ func MemProf() {
54 } 56 }
55 } 57 }
56 58
59 // start cpu profile monitor
57 func StartCPUProfile() { 60 func StartCPUProfile() {
58 f, err := os.Create("cpu-" + strconv.Itoa(pid) + ".pprof") 61 f, err := os.Create("cpu-" + strconv.Itoa(pid) + ".pprof")
59 if err != nil { 62 if err != nil {
...@@ -62,10 +65,12 @@ func StartCPUProfile() { ...@@ -62,10 +65,12 @@ func StartCPUProfile() {
62 pprof.StartCPUProfile(f) 65 pprof.StartCPUProfile(f)
63 } 66 }
64 67
68 // stop cpu profile monitor
65 func StopCPUProfile() { 69 func StopCPUProfile() {
66 pprof.StopCPUProfile() 70 pprof.StopCPUProfile()
67 } 71 }
68 72
73 // print gc information to io.Writer
69 func PrintGCSummary(w io.Writer) { 74 func PrintGCSummary(w io.Writer) {
70 memStats := &runtime.MemStats{} 75 memStats := &runtime.MemStats{}
71 runtime.ReadMemStats(memStats) 76 runtime.ReadMemStats(memStats)
...@@ -114,7 +119,7 @@ func avg(items []time.Duration) time.Duration { ...@@ -114,7 +119,7 @@ func avg(items []time.Duration) time.Duration {
114 return time.Duration(int64(sum) / int64(len(items))) 119 return time.Duration(int64(sum) / int64(len(items)))
115 } 120 }
116 121
117 // human readable format 122 // format bytes number friendly
118 func toH(bytes uint64) string { 123 func toH(bytes uint64) string {
119 switch { 124 switch {
120 case bytes < 1024: 125 case bytes < 1024:
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
7 "time" 7 "time"
8 ) 8 )
9 9
10 // Statistics struct
10 type Statistics struct { 11 type Statistics struct {
11 RequestUrl string 12 RequestUrl string
12 RequestController string 13 RequestController string
...@@ -16,12 +17,15 @@ type Statistics struct { ...@@ -16,12 +17,15 @@ type Statistics struct {
16 TotalTime time.Duration 17 TotalTime time.Duration
17 } 18 }
18 19
20 // UrlMap contains several statistics struct to log different data
19 type UrlMap struct { 21 type UrlMap struct {
20 lock sync.RWMutex 22 lock sync.RWMutex
21 LengthLimit int //limit the urlmap's length if it's equal to 0 there's no limit 23 LengthLimit int //limit the urlmap's length if it's equal to 0 there's no limit
22 urlmap map[string]map[string]*Statistics 24 urlmap map[string]map[string]*Statistics
23 } 25 }
24 26
27 // add statistics task.
28 // it needs request method, request url, request controller and statistics time duration
25 func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) { 29 func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) {
26 m.lock.Lock() 30 m.lock.Lock()
27 defer m.lock.Unlock() 31 defer m.lock.Unlock()
...@@ -65,6 +69,7 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri ...@@ -65,6 +69,7 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
65 } 69 }
66 } 70 }
67 71
72 // put url statistics result in io.Writer
68 func (m *UrlMap) GetMap(rw io.Writer) { 73 func (m *UrlMap) GetMap(rw io.Writer) {
69 m.lock.RLock() 74 m.lock.RLock()
70 defer m.lock.RUnlock() 75 defer m.lock.RUnlock()
...@@ -78,6 +83,7 @@ func (m *UrlMap) GetMap(rw io.Writer) { ...@@ -78,6 +83,7 @@ func (m *UrlMap) GetMap(rw io.Writer) {
78 } 83 }
79 } 84 }
80 85
86 // global statistics data map
81 var StatisticsMap *UrlMap 87 var StatisticsMap *UrlMap
82 88
83 func init() { 89 func init() {
......
...@@ -53,6 +53,7 @@ const ( ...@@ -53,6 +53,7 @@ const (
53 starBit = 1 << 63 53 starBit = 1 << 63
54 ) 54 )
55 55
56 // time taks schedule
56 type Schedule struct { 57 type Schedule struct {
57 Second uint64 58 Second uint64
58 Minute uint64 59 Minute uint64
...@@ -62,8 +63,10 @@ type Schedule struct { ...@@ -62,8 +63,10 @@ type Schedule struct {
62 Week uint64 63 Week uint64
63 } 64 }
64 65
66 // task func type
65 type TaskFunc func() error 67 type TaskFunc func() error
66 68
69 // task interface
67 type Tasker interface { 70 type Tasker interface {
68 GetStatus() string 71 GetStatus() string
69 Run() error 72 Run() error
...@@ -73,21 +76,24 @@ type Tasker interface { ...@@ -73,21 +76,24 @@ type Tasker interface {
73 GetPrev() time.Time 76 GetPrev() time.Time
74 } 77 }
75 78
79 // task error
76 type taskerr struct { 80 type taskerr struct {
77 t time.Time 81 t time.Time
78 errinfo string 82 errinfo string
79 } 83 }
80 84
85 // task struct
81 type Task struct { 86 type Task struct {
82 Taskname string 87 Taskname string
83 Spec *Schedule 88 Spec *Schedule
84 DoFunc TaskFunc 89 DoFunc TaskFunc
85 Prev time.Time 90 Prev time.Time
86 Next time.Time 91 Next time.Time
87 Errlist []*taskerr //errtime:errinfo 92 Errlist []*taskerr // like errtime:errinfo
88 ErrLimit int //max length for the errlist 0 stand for there' no limit 93 ErrLimit int // max length for the errlist, 0 stand for no limit
89 } 94 }
90 95
96 // add new task with name, time and func
91 func NewTask(tname string, spec string, f TaskFunc) *Task { 97 func NewTask(tname string, spec string, f TaskFunc) *Task {
92 98
93 task := &Task{ 99 task := &Task{
...@@ -99,6 +105,7 @@ func NewTask(tname string, spec string, f TaskFunc) *Task { ...@@ -99,6 +105,7 @@ func NewTask(tname string, spec string, f TaskFunc) *Task {
99 return task 105 return task
100 } 106 }
101 107
108 // get current task status
102 func (tk *Task) GetStatus() string { 109 func (tk *Task) GetStatus() string {
103 var str string 110 var str string
104 for _, v := range tk.Errlist { 111 for _, v := range tk.Errlist {
...@@ -107,6 +114,7 @@ func (tk *Task) GetStatus() string { ...@@ -107,6 +114,7 @@ func (tk *Task) GetStatus() string {
107 return str 114 return str
108 } 115 }
109 116
117 // run task
110 func (tk *Task) Run() error { 118 func (tk *Task) Run() error {
111 err := tk.DoFunc() 119 err := tk.DoFunc()
112 if err != nil { 120 if err != nil {
...@@ -117,53 +125,58 @@ func (tk *Task) Run() error { ...@@ -117,53 +125,58 @@ func (tk *Task) Run() error {
117 return err 125 return err
118 } 126 }
119 127
128 // set next time for this task
120 func (tk *Task) SetNext(now time.Time) { 129 func (tk *Task) SetNext(now time.Time) {
121 tk.Next = tk.Spec.Next(now) 130 tk.Next = tk.Spec.Next(now)
122 } 131 }
123 132
133 // get the next call time of this task
124 func (tk *Task) GetNext() time.Time { 134 func (tk *Task) GetNext() time.Time {
125 return tk.Next 135 return tk.Next
126 } 136 }
137
138 // set prev time of this task
127 func (tk *Task) SetPrev(now time.Time) { 139 func (tk *Task) SetPrev(now time.Time) {
128 tk.Prev = now 140 tk.Prev = now
129 } 141 }
130 142
143 // get prev time of this task
131 func (tk *Task) GetPrev() time.Time { 144 func (tk *Task) GetPrev() time.Time {
132 return tk.Prev 145 return tk.Prev
133 } 146 }
134 147
135 //前6个字段分别表示 148 // six columns mean
136 // 秒钟:0-59 149 // second:0-59
137 // 分钟:0-59 150 // minute:0-59
138 // 小时:1-23 151 // hour:1-23
139 // 日期:1-31 152 // day:1-31
140 // 月份:1-12 153 // month:1-12
141 // 星期:0-6(0表示周日 154 // week:0-6(0 means Sunday
142 155
143 //还可以用一些特殊符号 156 // some signals
144 // *: 表示任何时刻 157 // *: any time
145 // ,: 表示分割,如第三段里:2,4,表示2点和4点执行 158 // ,:  separate signal
146 //   -:表示一个段,如第三端里: 1-5,就表示1到5点 159 //   -:duration
147 // /n : 表示每个n的单位执行一次,如第三段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1. 160 // /n : do as n times of time duration
148 ///////////////////////////////////////////////////////// 161 /////////////////////////////////////////////////////////
149 // 0/30 * * * * * 每30秒 执行 162 // 0/30 * * * * * every 30s
150 // 0 43 21 * * * 21:43 执行 163 // 0 43 21 * * * 21:43
151 // 0 15 05 * * *    05:15 执行 164 // 0 15 05 * * *    05:15
152 // 0 0 17 * * * 17:00 执行 165 // 0 0 17 * * * 17:00
153 // 0 0 17 * * 1 每周一的 17:00 执行 166 // 0 0 17 * * 1 17:00 in every Monday
154 // 0 0,10 17 * * 0,2,3 每周日,周二,周三的 17:00和 17:10 执行 167 // 0 0,10 17 * * 0,2,3 17:00 and 17:10 in every Sunday, Tuesday and Wednesday
155 // 0 0-10 17 1 * * 毎月1日从 17:00到7:10 毎隔1分钟 执行 168 // 0 0-10 17 1 * * 17:00 to 17:10 in 1 min duration each time on the first day of month
156 // 0 0 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行 169 // 0 0 0 1,15 * 1 0:00 on the 1st day and 15th day of month
157 // 0 42 4 1 * *     毎月1日的 4:42分 执行 170 // 0 42 4 1 * *     4:42 on the 1st day of month
158 // 0 0 21 * * 1-6   周一到周六 21:00 执行 171 // 0 0 21 * * 1-6   21:00 from Monday to Saturday
159 // 0 0,10,20,30,40,50 * * * *  每隔10分 执行 172 // 0 0,10,20,30,40,50 * * * *  every 10 min duration
160 // 0 */10 * * * *        每隔10分 执行 173 // 0 */10 * * * *        every 10 min duration
161 // 0 * 1 * * *         从1:0到1:59 每隔1分钟 执行 174 // 0 * 1 * * *         1:00 to 1:59 in 1 min duration each time
162 // 0 0 1 * * *         1:00 执行 175 // 0 0 1 * * *         1:00
163 // 0 0 */1 * * *        毎时0分 每隔1小时 执行 176 // 0 0 */1 * * *        0 min of hour in 1 hour duration
164 // 0 0 * * * *         毎时0分 每隔1小时 执行 177 // 0 0 * * * *         0 min of hour in 1 hour duration
165 // 0 2 8-20/3 * * *       8:02,11:02,14:02,17:02,20:02 执行 178 // 0 2 8-20/3 * * *       8:02, 11:02, 14:02, 17:02, 20:02
166 // 0 30 5 1,15 * *       1日 和 15日的 5:30 执行 179 // 0 30 5 1,15 * *       5:30 on the 1st day and 15th day of month
167 func (t *Task) SetCron(spec string) { 180 func (t *Task) SetCron(spec string) {
168 t.Spec = t.parse(spec) 181 t.Spec = t.parse(spec)
169 } 182 }
...@@ -252,6 +265,7 @@ func (t *Task) parseSpec(spec string) *Schedule { ...@@ -252,6 +265,7 @@ func (t *Task) parseSpec(spec string) *Schedule {
252 return nil 265 return nil
253 } 266 }
254 267
268 // set schedule to next time
255 func (s *Schedule) Next(t time.Time) time.Time { 269 func (s *Schedule) Next(t time.Time) time.Time {
256 270
257 // Start at the earliest possible time (the upcoming second). 271 // Start at the earliest possible time (the upcoming second).
...@@ -349,6 +363,7 @@ func dayMatches(s *Schedule, t time.Time) bool { ...@@ -349,6 +363,7 @@ func dayMatches(s *Schedule, t time.Time) bool {
349 return domMatch || dowMatch 363 return domMatch || dowMatch
350 } 364 }
351 365
366 // start all tasks
352 func StartTask() { 367 func StartTask() {
353 go run() 368 go run()
354 } 369 }
...@@ -388,20 +403,23 @@ func run() { ...@@ -388,20 +403,23 @@ func run() {
388 } 403 }
389 } 404 }
390 405
406 // start all tasks
391 func StopTask() { 407 func StopTask() {
392 stop <- true 408 stop <- true
393 } 409 }
394 410
411 // add task with name
395 func AddTask(taskname string, t Tasker) { 412 func AddTask(taskname string, t Tasker) {
396 AdminTaskList[taskname] = t 413 AdminTaskList[taskname] = t
397 } 414 }
398 415
399 //sort map for tasker 416 // sort map for tasker
400 type MapSorter struct { 417 type MapSorter struct {
401 Keys []string 418 Keys []string
402 Vals []Tasker 419 Vals []Tasker
403 } 420 }
404 421
422 // create new tasker map
405 func NewMapSorter(m map[string]Tasker) *MapSorter { 423 func NewMapSorter(m map[string]Tasker) *MapSorter {
406 ms := &MapSorter{ 424 ms := &MapSorter{
407 Keys: make([]string, 0, len(m)), 425 Keys: make([]string, 0, len(m)),
...@@ -414,6 +432,7 @@ func NewMapSorter(m map[string]Tasker) *MapSorter { ...@@ -414,6 +432,7 @@ func NewMapSorter(m map[string]Tasker) *MapSorter {
414 return ms 432 return ms
415 } 433 }
416 434
435 // sort tasker map
417 func (ms *MapSorter) Sort() { 436 func (ms *MapSorter) Sort() {
418 sort.Sort(ms) 437 sort.Sort(ms)
419 } 438 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!