From 1467db05fb6efd2d90b9435aadd20fa14b194e01 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Fri, 14 Apr 2023 23:17:19 +0800 Subject: [PATCH] =?UTF-8?q?runtimehelper/=E5=9F=BA=E4=BA=8Egolangci-lint?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runtimehelper/.golangci.yaml | 138 +++++++++++++++++++++++++++++++++++ runtimehelper/helper.go | 15 ++-- 2 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 runtimehelper/.golangci.yaml diff --git a/runtimehelper/.golangci.yaml b/runtimehelper/.golangci.yaml new file mode 100644 index 0000000..7883e1d --- /dev/null +++ b/runtimehelper/.golangci.yaml @@ -0,0 +1,138 @@ +## 更新到 golangci-lint@v1.52.2 +run: + timeout: 1m + build-tags: [ ] + skip-dirs: [ ] + skip-files: [ ] +linters: + disable-all: true + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - cyclop + - dupl + - durationcheck + - errname + - errorlint + - exhaustive + - exportloopref + - funlen + - gocheckcompilerdirectives + - gochecknoinits + - goconst + - gocritic + - gocyclo + - goimports + - gomnd + - goprintffuncname + - gosec + - lll + - loggercheck + - makezero + - nakedret + - nestif + - nilnil + - noctx + - nolintlint + - nosprintfhostport + - prealloc + - predeclared + - promlinter + - reassign + - revive + - stylecheck + - tenv + - testableexamples + - testpackage + - tparallel + - unconvert + - unparam + - usestdlibvars + - wastedassign + - whitespace +linters-settings: + errcheck: + check-type-assertions: true + exclude-functions: [ ] + govet: + enable-all: true + disable: [ ] + cyclop: + max-complexity: 10 + package-average: 0.0 + dupl: + threshold: 150 + exhaustive: + check: + - switch + - map + funlen: + lines: 100 + statements: 60 + gocritic: + disabled-checks: + - commentFormatting + settings: + captLocal: + paramsOnly: false + underef: + skipRecvDeref: false + gocyclo: + min-complexity: 20 + gomnd: + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets + - prometheus.ExponentialBucketsRange + - prometheus.LinearBuckets + lll: + line-length: 240 + nakedret: + max-func-lines: 10 + nestif: + min-complexity: 5 + predeclared: + ignore: "" + q: false + reassign: + patterns: + - ".*" + tenv: + all: true + usestdlibvars: + time-month: true + time-layout: true + crypto-hash: true + default-rpc-path: true + os-dev-null: true + sql-isolation-level: true + tls-signature-scheme: true + constant-kind: true + syslog-priority: true +issues: + max-same-issues: 10 + exclude-rules: + - source: "//noinspection" + linters: [ gocritic ] + - path: "_test\\.go" + linters: + - bodyclose + - dupl + - funlen + - goconst + - gosec + - noctx diff --git a/runtimehelper/helper.go b/runtimehelper/helper.go index 54d4783..ebd5a4c 100644 --- a/runtimehelper/helper.go +++ b/runtimehelper/helper.go @@ -6,6 +6,11 @@ import ( "runtime" ) +const ( + callerSkipOffset = 2 + maximumFrames = 32 +) + // Frame 调用相关信息 type Frame struct { Function string `json:"func"` @@ -38,7 +43,7 @@ func (frame Frame) SplitFunction() (string, string) { // skip=0 表示调用 Caller 处. func Caller(skip int) Frame { pc := make([]uintptr, 1) - n := runtime.Callers(skip+2, pc) + n := runtime.Callers(skip+callerSkipOffset, pc) frame, _ := runtime.CallersFrames(pc[:n]).Next() if frame.PC == 0 { return Frame{} @@ -54,7 +59,7 @@ func Caller(skip int) Frame { // skip=0 表示调用 Stack 处. func Stack(skip, maximumFrames int) []Frame { pc := make([]uintptr, maximumFrames) - n := runtime.Callers(skip+2, pc) + n := runtime.Callers(skip+callerSkipOffset, pc) stack := make([]Frame, 0, n) frames := runtime.CallersFrames(pc[:n]) for { @@ -82,7 +87,7 @@ func Stack(skip, maximumFrames int) []Frame { // 可以使用 [runtime.Frame.PC] != 0 判断 runtime.Frame 有效 func CallerFrame(skip int) runtime.Frame { pc := make([]uintptr, 1) - n := runtime.Callers(skip+2, pc) + n := runtime.Callers(skip+callerSkipOffset, pc) frame, _ := runtime.CallersFrames(pc[:n]).Next() return frame } @@ -95,14 +100,14 @@ func CallerFrame(skip int) runtime.Frame { // - 0: 调用 CallersFrames 处 func CallersFrames(skip, maximumFrames int) *runtime.Frames { pc := make([]uintptr, maximumFrames) - n := runtime.Callers(skip+2, pc) + n := runtime.Callers(skip+callerSkipOffset, pc) return runtime.CallersFrames(pc[:n]) } // PrintCallersFrames 打印函数调用栈, // 从调用 PrintCallersFrames 的地方开始打印 func PrintCallersFrames() { - frames := CallersFrames(3, 32) + frames := CallersFrames(callerSkipOffset+1, maximumFrames) for { frame, more := frames.Next() if frame.PC != 0 {