Skip to content

Commit

Permalink
Merge pull request #106 from tminaorg/profiling
Browse files Browse the repository at this point in the history
added cpu profiling
  • Loading branch information
k4lizen authored Oct 9, 2023
2 parents 555fe73 + bfc3bea commit dd9eb50
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ src/engines/*/site/*
logdump/*.html
log/*.log
database/
profiling/

# go generate
*_stringer.go
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ go 1.20

require (
github.com/alecthomas/kong v0.8.0
github.com/cockroachdb/pebble v0.0.0-20231004191855-d4bf20c546d3
github.com/cockroachdb/pebble v0.0.0-20231009150004-a678d0968383
github.com/fxamacker/cbor/v2 v2.5.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/graceful v0.0.0-20230904152018-e56ed94cd808
github.com/gin-contrib/pprof v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/goccy/go-json v0.10.2
github.com/gocolly/colly/v2 v2.1.0
Expand All @@ -17,6 +18,7 @@ require (
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.0.1
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/profile v1.7.0
github.com/redis/go-redis/v9 v9.2.1
github.com/robertkrimen/otto v0.2.1
github.com/rs/zerolog v1.31.0
Expand All @@ -27,7 +29,7 @@ require (
github.com/DataDog/zstd v1.5.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.9.0 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
Expand All @@ -37,6 +39,7 @@ require (
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/getsentry/sentry-go v0.25.0 // indirect
Expand All @@ -46,6 +49,7 @@ require (
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
Expand All @@ -72,7 +76,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
26 changes: 20 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=
github.com/bytedance/sonic v1.10.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
Expand All @@ -44,13 +44,16 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4=
github.com/cockroachdb/errors v1.11.1 h1:xSEW75zKaKCWzR3OfxXUxgrk/NtT4G1MiOv5lWZazG8=
github.com/cockroachdb/errors v1.11.1/go.mod h1:8MUxA3Gi6b25tYlFEBGLf+D8aISL+M4MIpiWMSNRfxw=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/pebble v0.0.0-20231004191855-d4bf20c546d3 h1:mrKmxwfU6K40vjmwBiU4UqKhN8tAZJfQOciaK+Ycbgc=
github.com/cockroachdb/pebble v0.0.0-20231004191855-d4bf20c546d3/go.mod h1:6hk1eMY/u5t+Cf18q5lFMUA1Rc+Sm5I6Ra1QuPyxXCo=
github.com/cockroachdb/pebble v0.0.0-20231009150004-a678d0968383 h1:98LmGECk4ksnbvBVr0DgssnH0Mfwkd6+L6D/JHVd144=
github.com/cockroachdb/pebble v0.0.0-20231009150004-a678d0968383/go.mod h1:sEHm5NOXxyiAoKWhoFxT8xMgd/f3RA6qUqQ1BXKrh2E=
github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30=
github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
Expand All @@ -64,6 +67,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
Expand All @@ -76,6 +81,8 @@ github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0
github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs=
github.com/gin-contrib/graceful v0.0.0-20230904152018-e56ed94cd808 h1:FWLRw/61ras/SCc7OdLlg/MyS+MDx6syQhIBfkfznUc=
github.com/gin-contrib/graceful v0.0.0-20230904152018-e56ed94cd808/go.mod h1:eRZFrgWTLneuj4V0sNAN5+eNRcnZNSTsAFC+69voi78=
github.com/gin-contrib/pprof v1.4.0 h1:XxiBSf5jWZ5i16lNOPbMTVdgHBdhfGRD5PZ1LWazzvg=
github.com/gin-contrib/pprof v1.4.0/go.mod h1:RrehPJasUVBPK6yTUwOl8/NP6i0vbUgmxtis+Z5KE90=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
Expand Down Expand Up @@ -115,7 +122,11 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 h1:pUa4ghanp6q4IJHwE9RwLgmVFfReJN+KbQ8ExNEUUoQ=
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
github.com/jawher/mow.cli v1.1.0/go.mod h1:aNaQlc7ozF3vw6IJ2dHjp2ZFiA4ozMIYY6PyuRJwlUg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down Expand Up @@ -184,6 +195,8 @@ github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA=
github.com/pkg/profile v1.7.0/go.mod h1:8Uer0jas47ZQMJ7VD+OHknK4YDY07LPUC6dEvqDjvNo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
Expand Down Expand Up @@ -250,8 +263,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down Expand Up @@ -294,6 +307,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
11 changes: 11 additions & 0 deletions src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ var (
Config string `type:"path" default:"${config_path}" env:"BRZAGUZA_CONFIG" help:"Config folder path"`
Log string `type:"path" default:"${log_path}" env:"BRZAGUZA_LOG" help:"Log file path"`
Verbosity int8 `type:"counter" default:"0" short:"v" env:"BRZAGUZA_VERBOSITY" help:"Log level verbosity"`
// profiler
CPUProfile bool `type:"bool" default:"false" env:"BRZAGUZA_CPUPROFILE" help:"Use cpu profiling"`
HeapProfile bool `type:"bool" default:"false" env:"BRZAGUZA_HEAPPROFILE" help:"Use heap profiling"`
GORProfile bool `type:"bool" default:"false" env:"BRZAGUZA_GORPROFILE" help:"Use goroutine profiling"`
ThreadProfile bool `type:"bool" default:"false" env:"BRZAGUZA_THREADPROFILE" help:"Use threadcreate profiling"`
AllocProfile bool `type:"bool" default:"false" env:"BRZAGUZA_MEMALLOCPROFILE" help:"Use alloc profiling"`
BlockProfile bool `type:"bool" default:"false" env:"BRZAGUZA_BLOCKPROFILE" help:"Use block profiling"`
MutexProfile bool `type:"bool" default:"false" env:"BRZAGUZA_MUTEXPROFILE" help:"Use mutex profiling"`
ClockProfile bool `type:"bool" default:"false" env:"BRZAGUZA_CLOCKPROFILE" help:"Use clock profiling"`
TraceProfile bool `type:"bool" default:"false" env:"BRZAGUZA_TRACEPROFILE" help:"Use trace profiling"`
ServeProfiler bool `type:"bool" default:"false" env:"BRZAGUZA_SERVEPROFILER" help:"Run the profiler and serve at /debug/pprof/ http endpoint"`
}
)

Expand Down
12 changes: 7 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ func main() {
// parse cli arguments
setupCli()

_, stopProfiler := runProfiler()
defer stopProfiler()

// signal interrupt (CTRL+C)
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

// configure logging
logger.Setup(cli.Log, cli.Verbosity)

// load config file
conf := config.New()
conf.Load(cli.Config, cli.Log)

// signal interrupt (CTRL+C)
ctx, stopCtx := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

// cache database
var db cache.DB
switch conf.Server.Cache.Type {
Expand All @@ -53,13 +56,12 @@ func main() {
if rw, err := router.New(conf, cli.Verbosity); err != nil {
log.Error().Msgf("Failed creating a router: %v", err)
} else {
rw.Start(ctx, db)
rw.Start(ctx, db, cli.ServeProfiler)
}
}

// program cleanup
db.Close()
stopCtx()

log.Debug().Msgf("Program finished in %vms", time.Since(mainTimer).Milliseconds())
}
70 changes: 70 additions & 0 deletions src/profiling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"log"

"github.com/pkg/profile"
)

type profiler struct {
enabled bool
profile func(p *profile.Profile)
}

func runProfiler() (bool, func()) {
/*
goroutine — stack traces of all current goroutines
heap — a sampling of memory allocations of live objects
allocs — a sampling of all past memory allocations
threadcreate — stack traces that led to the creation of new OS threads
block — stack traces that led to blocking on synchronization primitives
mutex — stack traces of holders of contended mutexes
*/

profilers := [...]profiler{{
enabled: cli.CPUProfile,
profile: profile.CPUProfile,
}, {
enabled: cli.HeapProfile,
profile: profile.MemProfileHeap,
}, {
enabled: cli.GORProfile,
profile: profile.GoroutineProfile,
}, {
enabled: cli.ThreadProfile,
profile: profile.ThreadcreationProfile,
}, {
enabled: cli.BlockProfile,
profile: profile.BlockProfile,
}, {
enabled: cli.AllocProfile,
profile: profile.MemProfileAllocs,
}, {
enabled: cli.MutexProfile,
profile: profile.MutexProfile,
}, {
enabled: cli.ClockProfile,
profile: profile.ClockProfile,
}, {
enabled: cli.TraceProfile,
profile: profile.TraceProfile,
}}

profilerToRun := profiler{enabled: false}
for _, p := range profilers {
if profilerToRun.enabled && p.enabled {
log.Fatal("Only one profiler can be run at a time.")
return false, func() {}
} else if p.enabled {
profilerToRun = p
}
}
if !profilerToRun.enabled {
return false, func() {}
}

p := profile.Start(profilerToRun.profile, profile.ProfilePath("./profiling/"), profile.NoShutdownHook)
return true, func() {
p.Stop()
}
}
11 changes: 7 additions & 4 deletions src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package router

import (
"context"
"fmt"
"strconv"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/graceful"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"

Expand All @@ -23,7 +24,7 @@ func New(config *config.Config, verbosity int8) (*RouterWrapper, error) {
if verbosity == 0 {
gin.SetMode(gin.ReleaseMode)
}
router, err := graceful.Default(graceful.WithAddr(fmt.Sprintf(":%v", config.Server.Port)))
router, err := graceful.Default(graceful.WithAddr(":" + strconv.Itoa(config.Server.Port)))
return &RouterWrapper{router: router, config: config}, err
}

Expand All @@ -47,7 +48,7 @@ func (rw *RouterWrapper) runWithContext(ctx context.Context) {
}
}

func (rw *RouterWrapper) Start(ctx context.Context, db cache.DB) {
func (rw *RouterWrapper) Start(ctx context.Context, db cache.DB, serveProfiler bool) {
// CORS
rw.addCors()

Expand All @@ -62,6 +63,8 @@ func (rw *RouterWrapper) Start(ctx context.Context, db cache.DB) {
Search(c, rw.config, db)
})

// startup
if serveProfiler {
pprof.Register(rw.router.Engine)
}
rw.runWithContext(ctx)
}
2 changes: 1 addition & 1 deletion src/sedefaults/sedefaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ColRequest(seName engines.Name, col *colly.Collector, ctx *context.Context,
func ColError(seName engines.Name, col *colly.Collector, retError *error) {
col.OnError(func(r *colly.Response, err error) {
log.Error().Msgf("%v: SE Collector - OnError.\nURL: %v\nError: %v", seName, r.Request.URL.String(), err)
log.Error().Msgf("%v: HTML Response written to %v%v_col.log.html", seName, config.LogDumpLocation, seName)
log.Debug().Msgf("%v: HTML Response written to %v%v_col.log.html", seName, config.LogDumpLocation, seName)
writeErr := os.WriteFile(config.LogDumpLocation+string(seName)+"_col.log.html", r.Body, 0644)
if writeErr != nil {
log.Error().Err(writeErr)
Expand Down

0 comments on commit dd9eb50

Please sign in to comment.