Skip to content

Commit

Permalink
fix: setup early [debug]
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill committed Apr 25, 2024
1 parent 309c6f6 commit 2bf2b6d
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 109 deletions.
1 change: 0 additions & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (s *Service) mountTechnicalDebug() {
handler = web.ChainHandlers(auth.PermissionCheckHandler(s.auth), web.FinalHandler(handler))
}
s.router.Handle(path, handler)
s.router.Handle(rootPath+path, handler)
}
handle("/node", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.nodeGetHandler),
Expand Down
240 changes: 132 additions & 108 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,113 @@ func NewBee(
}
b.stamperStoreCloser = stamperStore

var apiService *api.Service
var debugService *api.Service

if o.DebugAPIAddr != "" {
if o.MutexProfile {
_ = runtime.SetMutexProfileFraction(1)
}

if o.BlockProfile {
runtime.SetBlockProfileRate(1)
}

debugAPIListener, err := net.Listen("tcp", o.DebugAPIAddr)
if err != nil {
return nil, fmt.Errorf("debug api listener: %w", err)
}

debugService = api.New(
*publicKey,
pssPrivateKey.PublicKey,
overlayEthAddress,
o.WhitelistedWithdrawalAddress,
logger,
transactionService,
batchStore,
beeNodeMode,
o.ChequebookEnable,
o.SwapEnable,
chainBackend,
o.CORSAllowedOrigins,
stamperStore,
)
debugService.Restricted = o.Restricted
debugService.MountTechnicalDebug()
debugService.SetProbe(probe)

debugAPIServer := &http.Server{
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
Handler: debugService,
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

go func() {
logger.Info("starting debug server", "address", debugAPIListener.Addr())

if err := debugAPIServer.Serve(debugAPIListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Debug("debug api server failed to start", "error", err)
logger.Error(nil, "debug api server failed to start")
}
}()

b.debugAPIServer = debugAPIServer
}

if o.APIAddr != "" {
if o.MutexProfile {
_ = runtime.SetMutexProfileFraction(1)
}
if o.BlockProfile {
runtime.SetBlockProfileRate(1)
}

apiServer := &http.Server{
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
Handler: apiService,
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
}

apiService = api.New(
*publicKey,
pssPrivateKey.PublicKey,
overlayEthAddress,
o.WhitelistedWithdrawalAddress,
logger,
transactionService,
batchStore,
beeNodeMode,
o.ChequebookEnable,
o.SwapEnable,
chainBackend,
o.CORSAllowedOrigins,
stamperStore,
)
apiService.Restricted = o.Restricted
apiService.MountTechnicalDebug()
apiService.SetProbe(probe)

go func() {
logger.Info("starting debug & api server", "address", apiListener.Addr())

if err := apiServer.Serve(apiListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Debug("debug & api server failed to start", "error", err)
logger.Error(nil, "debug & api server failed to start")
}
}()

b.apiServer = apiServer
b.apiCloser = apiServer
}

// Sync the with the given Ethereum backend:
isSynced, _, err := transaction.IsSynced(ctx, chainBackend, maxDelay)
if err != nil {
Expand Down Expand Up @@ -532,10 +639,9 @@ func NewBee(

var registry *promc.Registry

var apiService *api.Service

if o.APIAddr != "" {
apiService = api.New(*publicKey, pssPrivateKey.PublicKey, overlayEthAddress, o.WhitelistedWithdrawalAddress, logger, transactionService, batchStore, beeNodeMode, o.ChequebookEnable, o.SwapEnable, chainBackend, o.CORSAllowedOrigins, stamperStore)
if debugService != nil {
registry = debugService.MetricsRegistry()
} else if apiService != nil {
registry = apiService.MetricsRegistry()
}

Expand All @@ -553,6 +659,8 @@ func NewBee(
return nil, fmt.Errorf("p2p service: %w", err)
}

apiService.SetP2P(p2ps)

b.p2pService = p2ps
b.p2pHalter = p2ps

Expand Down Expand Up @@ -996,52 +1104,6 @@ func NewBee(
}

if o.APIAddr != "" {
apiService.SetProbe(probe)
apiService.SetRedistributionAgent(agent)
apiService.SetP2P(p2ps)
apiService.SetSwarmAddress(&swarmAddress)

if o.MutexProfile {
_ = runtime.SetMutexProfileFraction(1)
}

if o.BlockProfile {
runtime.SetBlockProfileRate(1)
}

apiService.Configure(signer, authenticator, tracer, api.Options{
CORSAllowedOrigins: o.CORSAllowedOrigins,
WsPingPeriod: 60 * time.Second,
Restricted: o.Restricted,
}, extraOpts, chainID, erc20Service)

apiService.MountTechnicalDebug()
apiService.MountAPI()
apiService.MountDebug()

apiServer := &http.Server{
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
Handler: apiService,
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
}

go func() {
logger.Info("starting api server", "address", apiListener.Addr())
if err := apiServer.Serve(apiListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Debug("api server failed to start", "error", err)
logger.Error(nil, "api server failed to start")
}
}()

b.apiServer = apiServer
b.apiCloser = apiService

// register metrics from components
apiService.MustRegisterMetrics(p2ps.Metrics()...)
apiService.MustRegisterMetrics(pingPong.Metrics()...)
Expand Down Expand Up @@ -1078,79 +1140,30 @@ func NewBee(
if swapBackendMetrics, ok := chainBackend.(metrics.Collector); ok {
apiService.MustRegisterMetrics(swapBackendMetrics.Metrics()...)
}
if apiService != nil {
apiService.MustRegisterMetrics(apiService.Metrics()...)
}

if l, ok := logger.(metrics.Collector); ok {
apiService.MustRegisterMetrics(l.Metrics()...)
}
apiService.MustRegisterMetrics(pseudosettleService.Metrics()...)
if swapService != nil {
apiService.MustRegisterMetrics(swapService.Metrics()...)
}
}

if o.DebugAPIAddr != "" {
debugService := api.New(
*publicKey,
pssPrivateKey.PublicKey,
overlayEthAddress,
o.WhitelistedWithdrawalAddress,
logger,
transactionService,
batchStore,
beeNodeMode,
o.ChequebookEnable,
o.SwapEnable,
chainBackend,
o.CORSAllowedOrigins,
stamperStore,
)

debugService.SetProbe(probe)
debugService.SetP2P(p2ps)
debugService.SetSwarmAddress(&swarmAddress)
debugService.SetRedistributionAgent(agent)

if o.MutexProfile {
_ = runtime.SetMutexProfileFraction(1)
}

if o.BlockProfile {
runtime.SetBlockProfileRate(1)
}

debugService.Configure(signer, authenticator, tracer, api.Options{
apiService.Configure(signer, authenticator, tracer, api.Options{
CORSAllowedOrigins: o.CORSAllowedOrigins,
WsPingPeriod: 60 * time.Second,
Restricted: o.Restricted,
}, extraOpts, chainID, erc20Service)
debugService.MountTechnicalDebug()
debugService.MountDebug()

debugAPIServer := &http.Server{
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
Handler: debugService,
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

debugAPIListener, err := net.Listen("tcp", o.DebugAPIAddr)
if err != nil {
return nil, fmt.Errorf("debug api listener: %w", err)
}

go func() {
logger.Info("starting debug server", "address", debugAPIListener.Addr())

if err := debugAPIServer.Serve(debugAPIListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Debug("debug api server failed to start", "error", err)
logger.Error(nil, "debug api server failed to start")
}
}()
apiService.MountAPI()
apiService.MountDebug()

b.debugAPIServer = debugAPIServer
debugService.SetP2P(p2ps)
debugService.SetSwarmAddress(&swarmAddress)
debugService.SetRedistributionAgent(agent)
}

if o.DebugAPIAddr != "" {
// register metrics from components
debugService.MustRegisterMetrics(p2ps.Metrics()...)
debugService.MustRegisterMetrics(pingPong.Metrics()...)
Expand Down Expand Up @@ -1197,6 +1210,17 @@ func NewBee(
if swapService != nil {
debugService.MustRegisterMetrics(swapService.Metrics()...)
}

debugService.Configure(signer, authenticator, tracer, api.Options{
CORSAllowedOrigins: o.CORSAllowedOrigins,
WsPingPeriod: 60 * time.Second,
Restricted: o.Restricted,
}, extraOpts, chainID, erc20Service)

debugService.SetP2P(p2ps)
debugService.SetSwarmAddress(&swarmAddress)
debugService.MountDebug()
debugService.SetRedistributionAgent(agent)
}

if err := kad.Start(ctx); err != nil {
Expand Down

0 comments on commit 2bf2b6d

Please sign in to comment.