fix golint issues in core/prof (#503)

master
Kevin Wan 4 years ago committed by GitHub
parent d84e3d4b53
commit a58fac9000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,34 +13,34 @@ import (
) )
type ( type (
Slot struct { profileSlot struct {
lifecount int64 lifecount int64
lastcount int64 lastcount int64
lifecycle int64 lifecycle int64
lastcycle int64 lastcycle int64
} }
ProfileCenter struct { profileCenter struct {
lock sync.RWMutex lock sync.RWMutex
slots map[string]*Slot slots map[string]*profileSlot
} }
) )
const flushInterval = 5 * time.Minute const flushInterval = 5 * time.Minute
var ( var (
profileCenter = &ProfileCenter{ pc = &profileCenter{
slots: make(map[string]*Slot), slots: make(map[string]*profileSlot),
} }
once sync.Once once sync.Once
) )
func report(name string, duration time.Duration) { func report(name string, duration time.Duration) {
updated := func() bool { updated := func() bool {
profileCenter.lock.RLock() pc.lock.RLock()
defer profileCenter.lock.RUnlock() defer pc.lock.RUnlock()
slot, ok := profileCenter.slots[name] slot, ok := pc.slots[name]
if ok { if ok {
atomic.AddInt64(&slot.lifecount, 1) atomic.AddInt64(&slot.lifecount, 1)
atomic.AddInt64(&slot.lastcount, 1) atomic.AddInt64(&slot.lastcount, 1)
@ -52,10 +52,10 @@ func report(name string, duration time.Duration) {
if !updated { if !updated {
func() { func() {
profileCenter.lock.Lock() pc.lock.Lock()
defer profileCenter.lock.Unlock() defer pc.lock.Unlock()
profileCenter.slots[name] = &Slot{ pc.slots[name] = &profileSlot{
lifecount: 1, lifecount: 1,
lastcount: 1, lastcount: 1,
lifecycle: int64(duration), lifecycle: int64(duration),
@ -89,10 +89,10 @@ func generateReport() string {
} }
func() { func() {
profileCenter.lock.Lock() pc.lock.Lock()
defer profileCenter.lock.Unlock() defer pc.lock.Unlock()
for key, slot := range profileCenter.slots { for key, slot := range pc.slots {
data = append(data, []string{ data = append(data, []string{
key, key,
strconv.FormatInt(slot.lifecount, 10), strconv.FormatInt(slot.lifecount, 10),

@ -3,56 +3,61 @@ package prof
import "github.com/tal-tech/go-zero/core/utils" import "github.com/tal-tech/go-zero/core/utils"
type ( type (
// A ProfilePoint is a profile time point.
ProfilePoint struct { ProfilePoint struct {
*utils.ElapsedTimer *utils.ElapsedTimer
} }
// A Profiler interface represents a profiler that used to report profile points.
Profiler interface { Profiler interface {
Start() ProfilePoint Start() ProfilePoint
Report(name string, point ProfilePoint) Report(name string, point ProfilePoint)
} }
RealProfiler struct{} realProfiler struct{}
NullProfiler struct{} nullProfiler struct{}
) )
var profiler = newNullProfiler() var profiler = newNullProfiler()
// EnableProfiling enables profiling.
func EnableProfiling() { func EnableProfiling() {
profiler = newRealProfiler() profiler = newRealProfiler()
} }
// Start starts a Profiler, and returns a start profiling point.
func Start() ProfilePoint { func Start() ProfilePoint {
return profiler.Start() return profiler.Start()
} }
// Report reports a ProfilePoint with given name.
func Report(name string, point ProfilePoint) { func Report(name string, point ProfilePoint) {
profiler.Report(name, point) profiler.Report(name, point)
} }
func newRealProfiler() Profiler { func newRealProfiler() Profiler {
return &RealProfiler{} return &realProfiler{}
} }
func (rp *RealProfiler) Start() ProfilePoint { func (rp *realProfiler) Start() ProfilePoint {
return ProfilePoint{ return ProfilePoint{
ElapsedTimer: utils.NewElapsedTimer(), ElapsedTimer: utils.NewElapsedTimer(),
} }
} }
func (rp *RealProfiler) Report(name string, point ProfilePoint) { func (rp *realProfiler) Report(name string, point ProfilePoint) {
duration := point.Duration() duration := point.Duration()
report(name, duration) report(name, duration)
} }
func newNullProfiler() Profiler { func newNullProfiler() Profiler {
return &NullProfiler{} return &nullProfiler{}
} }
func (np *NullProfiler) Start() ProfilePoint { func (np *nullProfiler) Start() ProfilePoint {
return ProfilePoint{} return ProfilePoint{}
} }
func (np *NullProfiler) Report(string, ProfilePoint) { func (np *nullProfiler) Report(string, ProfilePoint) {
} }

Loading…
Cancel
Save