Add profiler

This commit is contained in:
Shtorm
2026-06-10 16:49:01 +03:00
parent 9c80cf371c
commit d174962a04
3 changed files with 32 additions and 26 deletions

View File

@@ -28,6 +28,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
env: env:
@@ -63,6 +64,7 @@ builds:
- with_openvpn - with_openvpn
- with_trusttunnel - with_trusttunnel
- with_sudoku - with_sudoku
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
targets: targets:
@@ -123,6 +125,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -155,6 +158,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -187,6 +191,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -219,6 +224,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -251,6 +257,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -299,6 +306,7 @@ builds:
- with_sudoku - with_sudoku
- with_manager - with_manager
- with_admin_panel - with_admin_panel
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
- with_naive_outbound - with_naive_outbound
@@ -353,6 +361,7 @@ builds:
- with_openvpn - with_openvpn
- with_trusttunnel - with_trusttunnel
- with_sudoku - with_sudoku
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
targets: targets:
@@ -424,6 +433,7 @@ builds:
- with_openvpn - with_openvpn
- with_trusttunnel - with_trusttunnel
- with_sudoku - with_sudoku
- with_profiler
- badlinkname - badlinkname
- tfogo_checklinkname0 - tfogo_checklinkname0
hooks: hooks:

View File

@@ -1,9 +1,5 @@
package option package option
import "github.com/sagernet/sing/common/json/badoption"
type ProfilerServiceOptions struct { type ProfilerServiceOptions struct {
Listen string `json:"listen,omitempty"` ListenOptions
ReadTimeout badoption.Duration `json:"read_timeout,omitempty"`
WriteTimeout badoption.Duration `json:"write_timeout,omitempty"`
} }

View File

@@ -6,14 +6,15 @@ import (
"net/http/pprof" "net/http/pprof"
runtimePprof "runtime/pprof" runtimePprof "runtime/pprof"
"strings" "strings"
"time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
boxService "github.com/sagernet/sing-box/adapter/service" boxService "github.com/sagernet/sing-box/adapter/service"
"github.com/sagernet/sing-box/common/listener"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
N "github.com/sagernet/sing/common/network"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@@ -24,23 +25,21 @@ func RegisterService(registry *boxService.Registry) {
type Service struct { type Service struct {
boxService.Adapter boxService.Adapter
logger log.ContextLogger logger log.ContextLogger
listen string listener *listener.Listener
readTimeout time.Duration server *http.Server
writeTimeout time.Duration
server *http.Server
} }
func NewService(ctx context.Context, logger log.ContextLogger, tag string, options option.ProfilerServiceOptions) (adapter.Service, error) { func NewService(ctx context.Context, logger log.ContextLogger, tag string, options option.ProfilerServiceOptions) (adapter.Service, error) {
if options.Listen == "" {
return nil, E.New("missing listen")
}
return &Service{ return &Service{
Adapter: boxService.NewAdapter(C.TypeProfiler, tag), Adapter: boxService.NewAdapter(C.TypeProfiler, tag),
logger: logger, logger: logger,
listen: options.Listen, listener: listener.New(listener.Options{
readTimeout: time.Duration(options.ReadTimeout), Context: ctx,
writeTimeout: time.Duration(options.WriteTimeout), Logger: logger,
Network: []string{N.NetworkTCP},
Listen: options.ListenOptions,
}),
}, nil }, nil
} }
@@ -69,14 +68,15 @@ func (s *Service) Start(stage adapter.StartStage) error {
}) })
s.server = &http.Server{ s.server = &http.Server{
Addr: s.listen, Handler: r,
Handler: r,
ReadTimeout: s.readTimeout,
WriteTimeout: s.writeTimeout,
} }
s.logger.Info("profiler listening at ", s.listen) tcpListener, err := s.listener.ListenTCP()
if err != nil {
return err
}
s.logger.Info("profiler listening at ", tcpListener.Addr())
go func() { go func() {
err := s.server.ListenAndServe() err := s.server.Serve(tcpListener)
if err != nil && !E.IsClosed(err) { if err != nil && !E.IsClosed(err) {
s.logger.Error(E.Cause(err, "serve profiler")) s.logger.Error(E.Cause(err, "serve profiler"))
} }
@@ -89,5 +89,5 @@ func (s *Service) Close() error {
_ = s.server.Close() _ = s.server.Close()
s.server = nil s.server = nil
} }
return nil return s.listener.Close()
} }