add more feture in admin
1,增加QPS的限制 2,增加任务 3,增加healthcheck
Showing
4 changed files
with
131 additions
and
3 deletions
| ... | @@ -32,6 +32,8 @@ func init() { | ... | @@ -32,6 +32,8 @@ func init() { |
| 32 | BeeAdminApp.Route("/", AdminIndex) | 32 | BeeAdminApp.Route("/", AdminIndex) |
| 33 | BeeAdminApp.Route("/qps", QpsIndex) | 33 | BeeAdminApp.Route("/qps", QpsIndex) |
| 34 | BeeAdminApp.Route("/prof", ProfIndex) | 34 | BeeAdminApp.Route("/prof", ProfIndex) |
| 35 | BeeAdminApp.Route("/healthcheck", admin.Healthcheck) | ||
| 36 | BeeAdminApp.Route("/task", admin.TaskStatus) | ||
| 35 | FilterMonitorFunc = func(string, string, time.Duration) bool { return true } | 37 | FilterMonitorFunc = func(string, string, time.Duration) bool { return true } |
| 36 | } | 38 | } |
| 37 | 39 | ||
| ... | @@ -42,6 +44,7 @@ func AdminIndex(rw http.ResponseWriter, r *http.Request) { | ... | @@ -42,6 +44,7 @@ func AdminIndex(rw http.ResponseWriter, r *http.Request) { |
| 42 | func QpsIndex(rw http.ResponseWriter, r *http.Request) { | 44 | func QpsIndex(rw http.ResponseWriter, r *http.Request) { |
| 43 | admin.StatisticsMap.GetMap(rw) | 45 | admin.StatisticsMap.GetMap(rw) |
| 44 | } | 46 | } |
| 47 | |||
| 45 | func ProfIndex(rw http.ResponseWriter, r *http.Request) { | 48 | func ProfIndex(rw http.ResponseWriter, r *http.Request) { |
| 46 | r.ParseForm() | 49 | r.ParseForm() |
| 47 | command := r.Form.Get("command") | 50 | command := r.Form.Get("command") | ... | ... |
admin/healthcheck.go
0 → 100644
| 1 | package admin | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "net/http" | ||
| 6 | ) | ||
| 7 | |||
| 8 | //type DatabaseCheck struct { | ||
| 9 | //} | ||
| 10 | |||
| 11 | //func (dc *DatabaseCheck) Check() error { | ||
| 12 | // if dc.isConnected() { | ||
| 13 | // return nil | ||
| 14 | // } else { | ||
| 15 | // return errors.New("can't connect database") | ||
| 16 | // } | ||
| 17 | //} | ||
| 18 | |||
| 19 | //AddHealthCheck("database",&DatabaseCheck{}) | ||
| 20 | |||
| 21 | var AdminCheckList map[string]HealthChecker | ||
| 22 | |||
| 23 | type HealthChecker interface { | ||
| 24 | Check() error | ||
| 25 | } | ||
| 26 | |||
| 27 | func AddHealthCheck(name string, hc HealthChecker) { | ||
| 28 | AdminCheckList[name] = hc | ||
| 29 | } | ||
| 30 | |||
| 31 | func Healthcheck(rw http.ResponseWriter, req *http.Request) { | ||
| 32 | for name, h := range AdminCheckList { | ||
| 33 | if err := h.Check(); err != nil { | ||
| 34 | fmt.Fprintf(rw, "%s : ok\n", name) | ||
| 35 | } else { | ||
| 36 | fmt.Fprintf(rw, "%s : %s\n", name, err.Error()) | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | func init() { | ||
| 42 | AdminCheckList = make(map[string]HealthChecker) | ||
| 43 | } |
| ... | @@ -17,8 +17,9 @@ type Statistics struct { | ... | @@ -17,8 +17,9 @@ type Statistics struct { |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | type UrlMap struct { | 19 | type UrlMap struct { |
| 20 | lock sync.RWMutex | 20 | lock sync.RWMutex |
| 21 | urlmap map[string]map[string]*Statistics | 21 | LengthLimit int //limit the urlmap's length if it's equal to 0 there's no limit |
| 22 | urlmap map[string]map[string]*Statistics | ||
| 22 | } | 23 | } |
| 23 | 24 | ||
| 24 | func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) { | 25 | func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) { |
| ... | @@ -47,6 +48,9 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri | ... | @@ -47,6 +48,9 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri |
| 47 | } | 48 | } |
| 48 | 49 | ||
| 49 | } else { | 50 | } else { |
| 51 | if m.LengthLimit > 0 && m.LengthLimit <= len(m.urlmap) { | ||
| 52 | return | ||
| 53 | } | ||
| 50 | methodmap := make(map[string]*Statistics) | 54 | methodmap := make(map[string]*Statistics) |
| 51 | nb := &Statistics{ | 55 | nb := &Statistics{ |
| 52 | RequestUrl: requestUrl, | 56 | RequestUrl: requestUrl, | ... | ... |
| 1 | package admin | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | package admin | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "net/http" | ||
| 6 | ) | ||
| 7 | |||
| 8 | var AdminTaskList map[string]Tasker | ||
| 9 | |||
| 10 | type Tasker interface { | ||
| 11 | GetStatus() string | ||
| 12 | Run() error | ||
| 13 | } | ||
| 14 | |||
| 15 | type Task struct { | ||
| 16 | Taskname string | ||
| 17 | Spec Schedule | ||
| 18 | Errlist []map[uint64]string //errtime:errinfo | ||
| 19 | ErrLimit int //max length for the errlist 0 stand for there' no limit | ||
| 20 | } | ||
| 21 | |||
| 22 | func (t *Task) GetStatus() string { | ||
| 23 | return "" | ||
| 24 | } | ||
| 25 | |||
| 26 | func (t *Task) Run() error { | ||
| 27 | return nil | ||
| 28 | } | ||
| 29 | |||
| 30 | func (t *Task) SetCron(spec string) { | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | type Schedule struct { | ||
| 35 | Second uint64 | ||
| 36 | Minute uint64 | ||
| 37 | Hour uint64 | ||
| 38 | DOM uint64 | ||
| 39 | Month uint64 | ||
| 40 | DOW uint64 | ||
| 41 | } | ||
| 42 | |||
| 43 | func StartTask() { | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | func StopTask() { | ||
| 48 | |||
| 49 | } | ||
| 50 | |||
| 51 | func AddTask(taskname string, t Tasker) { | ||
| 52 | AdminTaskList[taskname] = t | ||
| 53 | } | ||
| 54 | |||
| 55 | func TaskStatus(rw http.ResponseWriter, req *http.Request) { | ||
| 56 | for tname, t := range AdminTaskList { | ||
| 57 | fmt.Fprintf(rw, "%s:%s", tname, t.GetStatus()) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | //to run a Task by http from the querystring taskname | ||
| 62 | //url like /task?taskname=sendmail | ||
| 63 | func RunTask(rw http.ResponseWriter, req *http.Request) { | ||
| 64 | req.ParseForm() | ||
| 65 | taskname := req.Form.Get("taskname") | ||
| 66 | if t, ok := AdminTaskList[taskname]; ok { | ||
| 67 | err := t.Run() | ||
| 68 | if err != nil { | ||
| 69 | fmt.Fprintf(rw, "%v", err) | ||
| 70 | } | ||
| 71 | fmt.Fprintf(rw, "%s run success,Now the Status is %s", t.GetStatus()) | ||
| 72 | } else { | ||
| 73 | fmt.Fprintf(rw, "there's no task which named:%s", taskname) | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | func init() { | ||
| 78 | AdminTaskList = make(map[string]Tasker) | ||
| 79 | } | ... | ... |
-
Please register or sign in to post a comment