Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
1d200da4
Commit
1d200da4
authored
Sep 12, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
golint toolbox
parent
be7accc9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
67 deletions
+68
-67
healthcheck.go
toolbox/healthcheck.go
+3
-3
profile.go
toolbox/profile.go
+4
-4
statistics.go
toolbox/statistics.go
+19
-18
task.go
toolbox/task.go
+42
-42
No files found.
toolbox/healthcheck.go
View file @
1d200da4
...
...
@@ -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
}
...
...
toolbox/profile.go
View file @
1d200da4
...
...
@@ -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
)
...
...
toolbox/statistics.go
View file @
1d200da4
...
...
@@ -22,7 +22,7 @@ import (
// Statistics struct
type
Statistics
struct
{
RequestU
rl
string
RequestU
RL
string
RequestController
string
RequestNum
int64
MinTime
time
.
Duration
...
...
@@ -30,21 +30,21 @@ type Statistics struct {
TotalTime
time
.
Duration
}
// U
rl
Map contains several statistics struct to log different data
type
U
rl
Map
struct
{
// U
RL
Map contains several statistics struct to log different data
type
U
RL
Map
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
*
U
rlMap
)
AddStatistics
(
requestMethod
,
requestUrl
,
requestController
string
,
requesttime
time
.
Duration
)
{
func
(
m
*
U
RLMap
)
AddStatistics
(
requestMethod
,
requestURL
,
requestController
string
,
requesttime
time
.
Duration
)
{
m
.
lock
.
Lock
()
defer
m
.
lock
.
Unlock
()
if
method
,
ok
:=
m
.
urlmap
[
requestU
rl
];
ok
{
if
method
,
ok
:=
m
.
urlmap
[
requestU
RL
];
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
{
RequestU
rl
:
requestUrl
,
RequestU
RL
:
requestURL
,
RequestController
:
requestController
,
RequestNum
:
1
,
MinTime
:
requesttime
,
MaxTime
:
requesttime
,
TotalTime
:
requesttime
,
}
m
.
urlmap
[
requestU
rl
][
requestMethod
]
=
nb
m
.
urlmap
[
requestU
RL
][
requestMethod
]
=
nb
}
}
else
{
...
...
@@ -70,7 +70,7 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
}
methodmap
:=
make
(
map
[
string
]
*
Statistics
)
nb
:=
&
Statistics
{
RequestU
rl
:
requestUrl
,
RequestU
RL
:
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
[
requestU
rl
]
=
methodmap
m
.
urlmap
[
requestU
RL
]
=
methodmap
}
}
// put url statistics result in io.Writer
func
(
m
*
U
rl
Map
)
GetMap
()
map
[
string
]
interface
{}
{
//
GetMap
put url statistics result in io.Writer
func
(
m
*
U
RL
Map
)
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
*
U
rl
Map
var
StatisticsMap
*
U
RL
Map
func
init
()
{
StatisticsMap
=
&
U
rl
Map
{
StatisticsMap
=
&
U
RL
Map
{
urlmap
:
make
(
map
[
string
]
map
[
string
]
*
Statistics
),
}
}
toolbox/task.go
View file @
1d200da4
...
...
@@ -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
(
t
k
*
Task
)
GetStatus
()
string
{
//
GetStatus
get current task status
func
(
t
*
Task
)
GetStatus
()
string
{
var
str
string
for
_
,
v
:=
range
t
k
.
Errlist
{
for
_
,
v
:=
range
t
.
Errlist
{
str
+=
v
.
t
.
String
()
+
":"
+
v
.
errinfo
+
"<br>"
}
return
str
}
//
run task
func
(
t
k
*
Task
)
Run
()
error
{
err
:=
t
k
.
DoFunc
()
//
Run run all tasks
func
(
t
*
Task
)
Run
()
error
{
err
:=
t
.
DoFunc
()
if
err
!=
nil
{
if
t
k
.
ErrLimit
>
0
&&
tk
.
ErrLimit
>
len
(
tk
.
Errlist
)
{
t
k
.
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
(
t
k
*
Task
)
SetNext
(
now
time
.
Time
)
{
t
k
.
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
(
t
k
*
Task
)
GetNext
()
time
.
Time
{
return
t
k
.
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
(
t
k
*
Task
)
SetPrev
(
now
time
.
Time
)
{
t
k
.
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
(
t
k
*
Task
)
GetPrev
()
time
.
Time
{
return
t
k
.
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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment