Skip to content

Commit

Permalink
cli: enable http pprof in debug mode (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
beihai0xff authored Jul 5, 2024
1 parent 0bab959 commit 6e74711
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
14 changes: 11 additions & 3 deletions app/turl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"golang.org/x/time/rate"

Expand All @@ -20,6 +21,13 @@ const HealthCheckPath = "/healthcheck"
// NewServer creates a new HTTP server.
func NewServer(h *Handler, c *configs.ServerConfig) (*http.Server, error) {
router := gin.New()
router.UseH2C = true

if c.Debug {
// register pprof router
pprof.Register(router, "api/debug/pprof")
}

router.Use(middleware.Logger(), middleware.HealthCheck(HealthCheckPath))

router.Use(gin.Recovery()) // recover from any panics, should be the last middleware
Expand All @@ -36,8 +44,8 @@ func NewServer(h *Handler, c *configs.ServerConfig) (*http.Server, error) {
return &http.Server{
Addr: fmt.Sprintf("%s:%d", c.Listen, c.Port),
Handler: http.TimeoutHandler(router.Handler(), c.RequestTimeout, "request timeout"),
ReadHeaderTimeout: time.Second,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
ReadHeaderTimeout: c.RequestTimeout,
ReadTimeout: c.RequestTimeout,
WriteTimeout: c.RequestTimeout,
}, nil
}
17 changes: 14 additions & 3 deletions cli/server_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@ var (
configPathFlag = &cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "turl server config file path",
Usage: "TURL Server Config File Path",
Value: "./config.yaml",
EnvVars: []string{"TURL_CONFIG_FILE", "TURL_FILE"},
}
readonlyFlag = &cli.BoolFlag{
Name: "readonly",
Aliases: []string{"ro"},
Usage: "start server in read-only mode",
Usage: "Start Server IN Read-Only Mode",
Value: false,
EnvVars: []string{"TURL_READONLY", "TURL_RO"},
}
debugFlag = &cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "Enable Debug Mode",
Value: false,
EnvVars: []string{"TURL_DEBUG"},
}
)

type serverCLI struct{}

func (c *serverCLI) getServerStartFlags() []cli.Flag {
return []cli.Flag{configPathFlag, readonlyFlag}
return []cli.Flag{configPathFlag, readonlyFlag, debugFlag}
}

func (c *serverCLI) serverHealth(ctx *cli.Context) error {
Expand Down Expand Up @@ -112,6 +119,10 @@ func (c *serverCLI) parseServerStartConfig(ctx *cli.Context) (*configs.ServerCon
mp[readonlyFlag.Name] = true
}

if ctx.Bool(debugFlag.Name) {
mp[debugFlag.Name] = true
}

conf, err := configs.ReadFile(filePath, mp)
if err != nil {
slog.Error("read server config file failed", slog.Any("error", err), slog.Any("file", filePath))
Expand Down
2 changes: 2 additions & 0 deletions cmd/turl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log/slog"
"os"

_ "go.uber.org/automaxprocs"

"github.com/beihai0xff/turl/cli"
)

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22

require (
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-contrib/pprof v1.5.0
github.com/gin-gonic/gin v1.10.0
github.com/go-playground/validator/v10 v10.22.0
github.com/google/uuid v1.6.0
Expand All @@ -15,6 +16,7 @@ require (
github.com/redis/go-redis/v9 v9.5.3
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.2
go.uber.org/automaxprocs v1.5.3
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/time v0.5.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gin-contrib/pprof v1.5.0 h1:E/Oy7g+kNw94KfdCy3bZxQFtyDnAX2V7axRS7sNYVrU=
github.com/gin-contrib/pprof v1.5.0/go.mod h1:GqFL6LerKoCQ/RSWnkYczkTJ+tOAUVN/8sbnEtaqOKs=
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.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
Expand Down Expand Up @@ -117,6 +119,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
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/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
Expand Down Expand Up @@ -144,6 +148,8 @@ github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
Expand Down

0 comments on commit 6e74711

Please sign in to comment.