Skip to content

Commit

Permalink
feat: add pprof flag to enable profiling at runtime of the LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jan 29, 2022
1 parent af7fe71 commit e158d02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/templ/lspcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ package lspcmd
import (
"context"
"log"
"net/http"
"os"
"os/signal"

"github.com/a-h/templ/cmd/templ/lspcmd/pls"
"github.com/sourcegraph/jsonrpc2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

_ "net/http/pprof"
)

type Arguments struct {
Log string
GoplsLog string
GoplsRPCTrace bool
PPROF bool
}

func Run(args Arguments) error {
Expand All @@ -27,6 +31,11 @@ func Run(args Arguments) error {
signal.Stop(signalChan)
cancel()
}()
if args.PPROF {
go func() {
http.ListenAndServe("localhost:9999", nil)
}()
}
go func() {
select {
case <-signalChan: // First signal, cancel context.
Expand Down
2 changes: 2 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func lspCmd(args []string) {
goplsLog := cmd.String("goplsLog", "", "The file to log gopls output, or leave empty to disable logging.")
goplsRPCTrace := cmd.Bool("goplsRPCTrace", false, "Set gopls to log input and output messages.")
helpFlag := cmd.Bool("help", false, "Print help and exit.")
pprofFlag := cmd.Bool("pprof", false, "Enable pprof web server (default address is localhost:9999)")
err := cmd.Parse(args)
if err != nil || *helpFlag {
cmd.PrintDefaults()
Expand All @@ -113,6 +114,7 @@ func lspCmd(args []string) {
Log: *log,
GoplsLog: *goplsLog,
GoplsRPCTrace: *goplsRPCTrace,
PPROF: *pprofFlag,
})
if err != nil {
fmt.Println(err.Error())
Expand Down

0 comments on commit e158d02

Please sign in to comment.