mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 17:40:48 +00:00
golint toolbox
This commit is contained in:
parent
be7accc94c
commit
1d200da472
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// toolbox healthcheck
|
||||
// Package toolbox healthcheck
|
||||
//
|
||||
// type DatabaseCheck struct {
|
||||
// }
|
||||
@ -33,12 +33,12 @@ package toolbox
|
||||
// health checker map
|
||||
var AdminCheckList map[string]HealthChecker
|
||||
|
||||
// health checker interface
|
||||
// HealthChecker health checker interface
|
||||
type HealthChecker interface {
|
||||
Check() error
|
||||
}
|
||||
|
||||
// add health checker with name string
|
||||
// AddHealthCheck add health checker with name string
|
||||
func AddHealthCheck(name string, hc HealthChecker) {
|
||||
AdminCheckList[name] = hc
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func init() {
|
||||
pid = os.Getpid()
|
||||
}
|
||||
|
||||
// parse input command string
|
||||
// ProcessInput parse input command string
|
||||
func ProcessInput(input string, w io.Writer) {
|
||||
switch input {
|
||||
case "lookup goroutine":
|
||||
@ -58,7 +58,7 @@ func ProcessInput(input string, w io.Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
// record memory profile in pprof
|
||||
// MemProf record memory profile in pprof
|
||||
func MemProf(w io.Writer) {
|
||||
filename := "mem-" + strconv.Itoa(pid) + ".memprof"
|
||||
if f, err := os.Create(filename); err != nil {
|
||||
@ -74,7 +74,7 @@ func MemProf(w io.Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
// start cpu profile monitor
|
||||
// GetCPUProfile start cpu profile monitor
|
||||
func GetCPUProfile(w io.Writer) {
|
||||
sec := 30
|
||||
filename := "cpu-" + strconv.Itoa(pid) + ".pprof"
|
||||
@ -92,7 +92,7 @@ func GetCPUProfile(w io.Writer) {
|
||||
fmt.Fprintf(w, "Now you can use this to check it: go tool pprof %s %s\n", fl, filename)
|
||||
}
|
||||
|
||||
// print gc information to io.Writer
|
||||
// PrintGCSummary print gc information to io.Writer
|
||||
func PrintGCSummary(w io.Writer) {
|
||||
memStats := &runtime.MemStats{}
|
||||
runtime.ReadMemStats(memStats)
|
||||
|
@ -22,7 +22,7 @@ import (
|
||||
|
||||
// Statistics struct
|
||||
type Statistics struct {
|
||||
RequestUrl string
|
||||
RequestURL string
|
||||
RequestController string
|
||||
RequestNum int64
|
||||
MinTime time.Duration
|
||||
@ -30,21 +30,21 @@ type Statistics struct {
|
||||
TotalTime time.Duration
|
||||
}
|
||||
|
||||
// UrlMap contains several statistics struct to log different data
|
||||
type UrlMap struct {
|
||||
// URLMap contains several statistics struct to log different data
|
||||
type URLMap struct {
|
||||
lock sync.RWMutex
|
||||
LengthLimit int //limit the urlmap's length if it's equal to 0 there's no limit
|
||||
urlmap map[string]map[string]*Statistics
|
||||
}
|
||||
|
||||
// add statistics task.
|
||||
// AddStatistics add statistics task.
|
||||
// it needs request method, request url, request controller and statistics time duration
|
||||
func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) {
|
||||
func (m *URLMap) AddStatistics(requestMethod, requestURL, requestController string, requesttime time.Duration) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
if method, ok := m.urlmap[requestUrl]; ok {
|
||||
if method, ok := m.urlmap[requestURL]; ok {
|
||||
if s, ok := method[requestMethod]; ok {
|
||||
s.RequestNum += 1
|
||||
s.RequestNum++
|
||||
if s.MaxTime < requesttime {
|
||||
s.MaxTime = requesttime
|
||||
}
|
||||
@ -54,14 +54,14 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
|
||||
s.TotalTime += requesttime
|
||||
} else {
|
||||
nb := &Statistics{
|
||||
RequestUrl: requestUrl,
|
||||
RequestURL: requestURL,
|
||||
RequestController: requestController,
|
||||
RequestNum: 1,
|
||||
MinTime: requesttime,
|
||||
MaxTime: requesttime,
|
||||
TotalTime: requesttime,
|
||||
}
|
||||
m.urlmap[requestUrl][requestMethod] = nb
|
||||
m.urlmap[requestURL][requestMethod] = nb
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -70,7 +70,7 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
|
||||
}
|
||||
methodmap := make(map[string]*Statistics)
|
||||
nb := &Statistics{
|
||||
RequestUrl: requestUrl,
|
||||
RequestURL: requestURL,
|
||||
RequestController: requestController,
|
||||
RequestNum: 1,
|
||||
MinTime: requesttime,
|
||||
@ -78,18 +78,18 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
|
||||
TotalTime: requesttime,
|
||||
}
|
||||
methodmap[requestMethod] = nb
|
||||
m.urlmap[requestUrl] = methodmap
|
||||
m.urlmap[requestURL] = methodmap
|
||||
}
|
||||
}
|
||||
|
||||
// put url statistics result in io.Writer
|
||||
func (m *UrlMap) GetMap() map[string]interface{} {
|
||||
// GetMap put url statistics result in io.Writer
|
||||
func (m *URLMap) GetMap() map[string]interface{} {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
var fields = []string{"requestUrl", "method", "times", "used", "max used", "min used", "avg used"}
|
||||
|
||||
resultLists := make([][]string, 0)
|
||||
var resultLists [][]string
|
||||
content := make(map[string]interface{})
|
||||
content["Fields"] = fields
|
||||
|
||||
@ -111,9 +111,10 @@ func (m *UrlMap) GetMap() map[string]interface{} {
|
||||
return content
|
||||
}
|
||||
|
||||
func (m *UrlMap) GetMapData() []map[string]interface{} {
|
||||
// GetMapData return all mapdata
|
||||
func (m *URLMap) GetMapData() []map[string]interface{} {
|
||||
|
||||
resultLists := make([]map[string]interface{}, 0)
|
||||
var resultLists []map[string]interface{}
|
||||
|
||||
for k, v := range m.urlmap {
|
||||
for kk, vv := range v {
|
||||
@ -133,10 +134,10 @@ func (m *UrlMap) GetMapData() []map[string]interface{} {
|
||||
}
|
||||
|
||||
// global statistics data map
|
||||
var StatisticsMap *UrlMap
|
||||
var StatisticsMap *URLMap
|
||||
|
||||
func init() {
|
||||
StatisticsMap = &UrlMap{
|
||||
StatisticsMap = &URLMap{
|
||||
urlmap: make(map[string]map[string]*Statistics),
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ const (
|
||||
starBit = 1 << 63
|
||||
)
|
||||
|
||||
// time taks schedule
|
||||
// Schedule time taks schedule
|
||||
type Schedule struct {
|
||||
Second uint64
|
||||
Minute uint64
|
||||
@ -79,10 +79,10 @@ type Schedule struct {
|
||||
Week uint64
|
||||
}
|
||||
|
||||
// task func type
|
||||
// TaskFunc task func type
|
||||
type TaskFunc func() error
|
||||
|
||||
// task interface
|
||||
// Tasker task interface
|
||||
type Tasker interface {
|
||||
GetSpec() string
|
||||
GetStatus() string
|
||||
@ -99,7 +99,7 @@ type taskerr struct {
|
||||
errinfo string
|
||||
}
|
||||
|
||||
// task struct
|
||||
// Task task struct
|
||||
type Task struct {
|
||||
Taskname string
|
||||
Spec *Schedule
|
||||
@ -111,7 +111,7 @@ type Task struct {
|
||||
ErrLimit int // max length for the errlist, 0 stand for no limit
|
||||
}
|
||||
|
||||
// add new task with name, time and func
|
||||
// NewTask add new task with name, time and func
|
||||
func NewTask(tname string, spec string, f TaskFunc) *Task {
|
||||
|
||||
task := &Task{
|
||||
@ -124,49 +124,49 @@ func NewTask(tname string, spec string, f TaskFunc) *Task {
|
||||
return task
|
||||
}
|
||||
|
||||
//get spec string
|
||||
func (s *Task) GetSpec() string {
|
||||
return s.SpecStr
|
||||
// GetSpec get spec string
|
||||
func (t *Task) GetSpec() string {
|
||||
return t.SpecStr
|
||||
}
|
||||
|
||||
// get current task status
|
||||
func (tk *Task) GetStatus() string {
|
||||
// GetStatus get current task status
|
||||
func (t *Task) GetStatus() string {
|
||||
var str string
|
||||
for _, v := range tk.Errlist {
|
||||
for _, v := range t.Errlist {
|
||||
str += v.t.String() + ":" + v.errinfo + "<br>"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// run task
|
||||
func (tk *Task) Run() error {
|
||||
err := tk.DoFunc()
|
||||
// Run run all tasks
|
||||
func (t *Task) Run() error {
|
||||
err := t.DoFunc()
|
||||
if err != nil {
|
||||
if tk.ErrLimit > 0 && tk.ErrLimit > len(tk.Errlist) {
|
||||
tk.Errlist = append(tk.Errlist, &taskerr{t: tk.Next, errinfo: err.Error()})
|
||||
if t.ErrLimit > 0 && t.ErrLimit > len(t.Errlist) {
|
||||
t.Errlist = append(t.Errlist, &taskerr{t: t.Next, errinfo: err.Error()})
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// set next time for this task
|
||||
func (tk *Task) SetNext(now time.Time) {
|
||||
tk.Next = tk.Spec.Next(now)
|
||||
// SetNext set next time for this task
|
||||
func (t *Task) SetNext(now time.Time) {
|
||||
t.Next = t.Spec.Next(now)
|
||||
}
|
||||
|
||||
// get the next call time of this task
|
||||
func (tk *Task) GetNext() time.Time {
|
||||
return tk.Next
|
||||
// GetNext get the next call time of this task
|
||||
func (t *Task) GetNext() time.Time {
|
||||
return t.Next
|
||||
}
|
||||
|
||||
// set prev time of this task
|
||||
func (tk *Task) SetPrev(now time.Time) {
|
||||
tk.Prev = now
|
||||
// SetPrev set prev time of this task
|
||||
func (t *Task) SetPrev(now time.Time) {
|
||||
t.Prev = now
|
||||
}
|
||||
|
||||
// get prev time of this task
|
||||
func (tk *Task) GetPrev() time.Time {
|
||||
return tk.Prev
|
||||
// GetPrev get prev time of this task
|
||||
func (t *Task) GetPrev() time.Time {
|
||||
return t.Prev
|
||||
}
|
||||
|
||||
// six columns mean:
|
||||
@ -177,7 +177,7 @@ func (tk *Task) GetPrev() time.Time {
|
||||
// month:1-12
|
||||
// week:0-6(0 means Sunday)
|
||||
|
||||
// some signals:
|
||||
// SetCron some signals:
|
||||
// *: any time
|
||||
// ,: separate signal
|
||||
// -:duration
|
||||
@ -289,7 +289,7 @@ func (t *Task) parseSpec(spec string) *Schedule {
|
||||
return nil
|
||||
}
|
||||
|
||||
// set schedule to next time
|
||||
// Next set schedule to next time
|
||||
func (s *Schedule) Next(t time.Time) time.Time {
|
||||
|
||||
// Start at the earliest possible time (the upcoming second).
|
||||
@ -377,8 +377,8 @@ WRAP:
|
||||
|
||||
func dayMatches(s *Schedule, t time.Time) bool {
|
||||
var (
|
||||
domMatch bool = 1<<uint(t.Day())&s.Day > 0
|
||||
dowMatch bool = 1<<uint(t.Weekday())&s.Week > 0
|
||||
domMatch = 1<<uint(t.Day())&s.Day > 0
|
||||
dowMatch = 1<<uint(t.Weekday())&s.Week > 0
|
||||
)
|
||||
|
||||
if s.Day&starBit > 0 || s.Week&starBit > 0 {
|
||||
@ -387,7 +387,7 @@ func dayMatches(s *Schedule, t time.Time) bool {
|
||||
return domMatch || dowMatch
|
||||
}
|
||||
|
||||
// start all tasks
|
||||
// StartTask start all tasks
|
||||
func StartTask() {
|
||||
isstart = true
|
||||
go run()
|
||||
@ -430,13 +430,13 @@ func run() {
|
||||
}
|
||||
}
|
||||
|
||||
// start all tasks
|
||||
// StopTask stop all tasks
|
||||
func StopTask() {
|
||||
isstart = false
|
||||
stop <- true
|
||||
}
|
||||
|
||||
// add task with name
|
||||
// AddTask add task with name
|
||||
func AddTask(taskname string, t Tasker) {
|
||||
AdminTaskList[taskname] = t
|
||||
if isstart {
|
||||
@ -444,7 +444,7 @@ func AddTask(taskname string, t Tasker) {
|
||||
}
|
||||
}
|
||||
|
||||
// add task with name
|
||||
// DeleteTask delete task with name
|
||||
func DeleteTask(taskname string) {
|
||||
delete(AdminTaskList, taskname)
|
||||
if isstart {
|
||||
@ -452,13 +452,13 @@ func DeleteTask(taskname string) {
|
||||
}
|
||||
}
|
||||
|
||||
// sort map for tasker
|
||||
// MapSorter sort map for tasker
|
||||
type MapSorter struct {
|
||||
Keys []string
|
||||
Vals []Tasker
|
||||
}
|
||||
|
||||
// create new tasker map
|
||||
// NewMapSorter create new tasker map
|
||||
func NewMapSorter(m map[string]Tasker) *MapSorter {
|
||||
ms := &MapSorter{
|
||||
Keys: make([]string, 0, len(m)),
|
||||
@ -471,7 +471,7 @@ func NewMapSorter(m map[string]Tasker) *MapSorter {
|
||||
return ms
|
||||
}
|
||||
|
||||
// sort tasker map
|
||||
// Sort sort tasker map
|
||||
func (ms *MapSorter) Sort() {
|
||||
sort.Sort(ms)
|
||||
}
|
||||
@ -512,11 +512,11 @@ func getRange(expr string, r bounds) uint64 {
|
||||
singleDigit = len(lowAndHigh) == 1
|
||||
)
|
||||
|
||||
var extra_star uint64
|
||||
var extrastar uint64
|
||||
if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
|
||||
start = r.min
|
||||
end = r.max
|
||||
extra_star = starBit
|
||||
extrastar = starBit
|
||||
} else {
|
||||
start = parseIntOrName(lowAndHigh[0], r.names)
|
||||
switch len(lowAndHigh) {
|
||||
@ -553,7 +553,7 @@ func getRange(expr string, r bounds) uint64 {
|
||||
log.Panicf("Beginning of range (%d) beyond end of range (%d): %s", start, end, expr)
|
||||
}
|
||||
|
||||
return getBits(start, end, step) | extra_star
|
||||
return getBits(start, end, step) | extrastar
|
||||
}
|
||||
|
||||
// parseIntOrName returns the (possibly-named) integer contained in expr.
|
||||
|
Loading…
Reference in New Issue
Block a user