micro--go-micro
3e885308a0
Fixes #2988. Brings 'golangci-lint run ./...' to zero issues (was ~373): - errcheck: explicitly ignore fire-and-forget calls with '_ =' (and a small errcheck.exclude-functions list for response writes — json Encoder.Encode, http ResponseWriter.Write, fmt.Fprint*); genuine cases handled. - unused: remove dead code (unexported decls and dead test helpers) and the imports they orphaned. - staticcheck: ST1005 error strings, ST1016 receiver names, S1000/S1017/S1019/ S1023 simplifications, SA4004/SA4006/SA4010 dead code, SA1021 net.IP.Equal, SA6002 (store *[]byte in sync.Pool). - govet: fix a context leak (lostcancel) in internal/util/mdns and move t.Fatal/Fatalf out of goroutines (testinggoroutine) in tests. - ineffassign, unconvert: mechanical fixes. CI: the Lint workflow now runs a blocking full-tree 'golangci-lint run' on pushes and PRs (dropped only-new-issues now that the tree is clean). Verified: go build, go vet, test compilation, and unit tests for the behaviourally-touched packages all pass. Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL Co-authored-by: Claude <noreply@anthropic.com>
99 行
1.5 KiB
Go
99 行
1.5 KiB
Go
// Package pprof provides a pprof profiler
|
|
package pprof
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
"sync"
|
|
|
|
"go-micro.dev/v6/debug/profile"
|
|
)
|
|
|
|
type profiler struct {
|
|
|
|
// where the cpu profile is written
|
|
cpuFile *os.File
|
|
// where the mem profile is written
|
|
memFile *os.File
|
|
opts profile.Options
|
|
|
|
sync.Mutex
|
|
running bool
|
|
}
|
|
|
|
func (p *profiler) Start() error {
|
|
p.Lock()
|
|
defer p.Unlock()
|
|
|
|
if p.running {
|
|
return nil
|
|
}
|
|
|
|
cpuFile := filepath.Join(os.TempDir(), "cpu.pprof")
|
|
memFile := filepath.Join(os.TempDir(), "mem.pprof")
|
|
|
|
if len(p.opts.Name) > 0 {
|
|
cpuFile = filepath.Join(os.TempDir(), p.opts.Name+".cpu.pprof")
|
|
memFile = filepath.Join(os.TempDir(), p.opts.Name+".mem.pprof")
|
|
}
|
|
|
|
f1, err := os.Create(cpuFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f2, err := os.Create(memFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// start cpu profiling
|
|
if err := pprof.StartCPUProfile(f1); err != nil {
|
|
return err
|
|
}
|
|
|
|
// set cpu file
|
|
p.cpuFile = f1
|
|
// set mem file
|
|
p.memFile = f2
|
|
|
|
p.running = true
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *profiler) Stop() error {
|
|
p.Lock()
|
|
defer p.Unlock()
|
|
|
|
if !p.running {
|
|
return nil
|
|
}
|
|
|
|
pprof.StopCPUProfile()
|
|
p.cpuFile.Close()
|
|
runtime.GC()
|
|
_ = pprof.WriteHeapProfile(p.memFile)
|
|
p.memFile.Close()
|
|
p.running = false
|
|
p.cpuFile = nil
|
|
p.memFile = nil
|
|
return nil
|
|
}
|
|
|
|
func (p *profiler) String() string {
|
|
return "pprof"
|
|
}
|
|
|
|
func NewProfile(opts ...profile.Option) profile.Profile {
|
|
var options profile.Options
|
|
for _, o := range opts {
|
|
o(&options)
|
|
}
|
|
p := new(profiler)
|
|
p.opts = options
|
|
return p
|
|
}
|