Skip to content

Commit 050ab5f

Browse files
update dependencies and address review comments
1 parent 453f201 commit 050ab5f

File tree

7 files changed

+77
-75
lines changed

7 files changed

+77
-75
lines changed

api/profiler.go

+53-52
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import (
99
"strconv"
1010
"time"
1111

12+
"github.com/rs/zerolog/log"
13+
1214
"github.com/onflow/flow-go/module/component"
1315
"github.com/onflow/flow-go/module/irrecoverable"
1416

1517
"github.com/rs/zerolog"
1618
)
1719

1820
type ProfileServer struct {
21+
component.Component
22+
1923
log zerolog.Logger
2024
server *http.Server
2125
endpoint string
22-
23-
startupCompleted chan struct{}
2426
}
2527

2628
var _ component.Component = (*ProfileServer)(nil)
@@ -31,70 +33,69 @@ func NewProfileServer(
3133
port int,
3234
) *ProfileServer {
3335
endpoint := net.JoinHostPort(host, strconv.Itoa(port))
34-
return &ProfileServer{
35-
log: logger,
36-
server: &http.Server{Addr: endpoint},
37-
endpoint: endpoint,
38-
startupCompleted: make(chan struct{}),
36+
37+
s := &ProfileServer{
38+
log: logger,
39+
server: &http.Server{Addr: endpoint},
40+
endpoint: endpoint,
3941
}
42+
43+
s.Component = component.NewComponentManagerBuilder().
44+
AddWorker(s.serve).
45+
AddWorker(s.shutdownOnContextDone).
46+
Build()
47+
48+
return s
4049
}
4150

42-
func (s *ProfileServer) Start(ctx irrecoverable.SignalerContext) {
43-
defer close(s.startupCompleted)
51+
func (s *ProfileServer) serve(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
52+
s.log.Info().Msg("starting profiler server on address")
53+
54+
l, err := net.Listen("tcp", s.endpoint)
55+
if err != nil {
56+
s.log.Err(err).Msg("failed to start the metrics server")
57+
ctx.Throw(err)
58+
return
59+
}
60+
61+
ready()
4462

63+
// pass the signaler context to the server so that the signaler context
64+
// can control the server's lifetime
4565
s.server.BaseContext = func(_ net.Listener) context.Context {
4666
return ctx
4767
}
4868

49-
go func() {
50-
s.log.Info().Msgf("Profiler server started: %s", s.endpoint)
51-
52-
if err := s.server.ListenAndServe(); err != nil {
53-
// http.ErrServerClosed is returned when Close or Shutdown is called
54-
// we don't consider this an error, so print this with debug level instead
55-
if errors.Is(err, http.ErrServerClosed) {
56-
s.log.Debug().Err(err).Msg("Profiler server shutdown")
57-
} else {
58-
s.log.Err(err).Msg("error running profiler server")
59-
}
69+
err = s.server.Serve(l) // blocking call
70+
if err != nil {
71+
if errors.Is(err, http.ErrServerClosed) {
72+
return
6073
}
61-
}()
62-
}
63-
64-
func (s *ProfileServer) Ready() <-chan struct{} {
65-
ready := make(chan struct{})
66-
67-
go func() {
68-
<-s.startupCompleted
69-
close(ready)
70-
}()
7174

72-
return ready
75+
log.Err(err).Msg("fatal error in the metrics server")
76+
ctx.Throw(err)
77+
}
7378
}
7479

75-
func (s *ProfileServer) Done() <-chan struct{} {
76-
done := make(chan struct{})
77-
go func() {
78-
<-s.startupCompleted
79-
defer close(done)
80+
func (s *ProfileServer) shutdownOnContextDone(ictx irrecoverable.SignalerContext, ready component.ReadyFunc) {
81+
ready()
82+
<-ictx.Done()
8083

81-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
82-
defer cancel()
84+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
85+
defer cancel()
8386

84-
err := s.server.Shutdown(ctx)
85-
if err == nil {
86-
s.log.Info().Msg("Profiler server graceful shutdown completed")
87-
}
87+
err := s.server.Shutdown(ctx)
88+
if err == nil {
89+
s.log.Info().Msg("Profiler server graceful shutdown completed")
90+
}
8891

89-
if errors.Is(err, ctx.Err()) {
90-
s.log.Warn().Msg("Profiler server graceful shutdown timed out")
91-
err := s.server.Close()
92-
if err != nil {
93-
s.log.Err(err).Msg("error closing profiler server")
94-
}
95-
} else {
96-
s.log.Err(err).Msg("error shutting down profiler server")
92+
if errors.Is(err, ctx.Err()) {
93+
s.log.Warn().Msg("Profiler server graceful shutdown timed out")
94+
err := s.server.Close()
95+
if err != nil {
96+
s.log.Err(err).Msg("error closing profiler server")
9797
}
98-
}()
99-
return done
98+
} else {
99+
s.log.Err(err).Msg("error shutting down profiler server")
100+
}
100101
}

bootstrap/bootstrap.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ func (fnb *EVMGatewayNodeBuilder) initDB() error {
148148
if err := fnb.DB.Close(); err != nil {
149149
return fmt.Errorf("error closing pebble database: %w", err)
150150
}
151+
152+
fnb.Logger.Info().Msg("database has been closed")
151153
return nil
152154
})
153155

154-
return err
156+
return nil
155157
}
156158

157159
func (fnb *EVMGatewayNodeBuilder) Component(name string, f cmd.ReadyDoneFactory[config.Config]) *EVMGatewayNodeBuilder {
@@ -173,7 +175,6 @@ func (fnb *EVMGatewayNodeBuilder) postShutdown() error {
173175
errs = multierror.Append(errs, err)
174176
}
175177
}
176-
fnb.Logger.Info().Msg("database has been closed")
177178
return errs.ErrorOrNil()
178179
}
179180

@@ -206,11 +207,11 @@ func (fnb *EVMGatewayNodeBuilder) Initialize() error {
206207
func (fnb *EVMGatewayNodeBuilder) LoadComponentsAndModules() {
207208
fnb.initPublishers()
208209

210+
fnb.Component("Profiler Server", fnb.profilerServerComponent)
211+
fnb.Component("Metrics Server", fnb.metricsServerComponent)
209212
fnb.Component("Key Store", fnb.initKeyStore)
210213
fnb.Component("API Server", fnb.apiServerComponent)
211214
fnb.Component("Event Ingestion Engine", fnb.eventIngestionEngineComponent)
212-
fnb.Component("Metrics Server", fnb.metricsServerComponent)
213-
fnb.Component("Profiler Server", fnb.profilerServerComponent)
214215
}
215216

216217
func (fnb *EVMGatewayNodeBuilder) metricsServerComponent(config config.Config) (module.ReadyDoneAware, error) {
@@ -359,30 +360,30 @@ func (fnb *EVMGatewayNodeBuilder) eventIngestionEngineComponent(cfg config.Confi
359360
l.Info().Msg("bootstrap starting event ingestion")
360361

361362
// get latest cadence block from the network and the database
362-
latestCadenceBlock, err := fnb.Client.GetLatestBlock(context.Background(), true)
363+
gatewayLatestBlock, err := fnb.Client.GetLatestBlock(context.Background(), true)
363364
if err != nil {
364365
return nil, fmt.Errorf("failed to get latest cadence block: %w", err)
365366
}
366367

367-
latestCadenceHeight, err := fnb.Storages.Blocks.LatestCadenceHeight()
368+
chainLatestHeight, err := fnb.Storages.Blocks.LatestCadenceHeight()
368369
if err != nil {
369370
return nil, err
370371
}
371372

372373
// make sure the provided block to start the indexing can be loaded
373-
_, err = fnb.Client.GetBlockHeaderByHeight(context.Background(), latestCadenceHeight)
374+
_, err = fnb.Client.GetBlockHeaderByHeight(context.Background(), chainLatestHeight)
374375
if err != nil {
375376
return nil, fmt.Errorf(
376377
"failed to get provided cadence height %d: %w",
377-
latestCadenceHeight,
378+
chainLatestHeight,
378379
err,
379380
)
380381
}
381382

382383
l.Info().
383-
Uint64("start-cadence-height", latestCadenceHeight).
384-
Uint64("latest-cadence-height", latestCadenceBlock.Height).
385-
Uint64("missed-heights", latestCadenceBlock.Height-latestCadenceHeight).
384+
Uint64("chain-cadence-height", chainLatestHeight).
385+
Uint64("gateway-cadence-height", gatewayLatestBlock.Height).
386+
Uint64("missed-heights", gatewayLatestBlock.Height-chainLatestHeight).
386387
Msg("indexing cadence height information")
387388

388389
chainID := cfg.FlowNetworkID
@@ -393,7 +394,7 @@ func (fnb *EVMGatewayNodeBuilder) eventIngestionEngineComponent(cfg config.Confi
393394
fnb.Client,
394395
chainID,
395396
fnb.Keystore,
396-
latestCadenceHeight,
397+
chainLatestHeight,
397398
)
398399

399400
callTracerCollector, err := replayer.NewCallTracerCollector(fnb.Logger)

cmd/run/cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ var Cmd = &cobra.Command{
3939
builder := bootstrap.NewEVMGatewayNodeBuilder(cfg)
4040

4141
if err := builder.Initialize(); err != nil {
42-
builder.Logger.Fatal().Err(err).Send()
42+
builder.Logger.Fatal().Err(err).Msg("failed to initialize")
4343
}
4444

4545
builder.LoadComponentsAndModules()
4646

4747
node, err := builder.Build()
4848
if err != nil {
49-
builder.Logger.Fatal().Err(err).Send()
49+
builder.Logger.Fatal().Err(err).Msg("failed to build node")
5050
}
5151
node.Run(command.Context())
5252
return nil

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/go-multierror v1.1.1
99
github.com/onflow/atree v0.8.0
1010
github.com/onflow/cadence v1.2.2
11-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49
11+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4
1212
github.com/onflow/flow-go-sdk v1.2.3
1313
github.com/onflow/go-ethereum v1.14.7
1414
github.com/prometheus/client_golang v1.18.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.1 h1:Ts5ob+CoCY2EjEd0W6vdLJ7hLL3
743743
github.com/onflow/flow-ft/lib/go/contracts v1.0.1/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
744744
github.com/onflow/flow-ft/lib/go/templates v1.0.1 h1:FDYKAiGowABtoMNusLuRCILIZDtVqJ/5tYI4VkF5zfM=
745745
github.com/onflow/flow-ft/lib/go/templates v1.0.1/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
746-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49 h1:kcAoHB/uEGP3wTJcv2aVtCHSgwY0bdn8qjw7GvvHfM8=
747-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49/go.mod h1:c4ubAQ2WIMYY/TOaBvbajROEFWv2HwhKeGOsEdLPIM0=
746+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4 h1:PSxMlqAgTzeTb/J6nknx3q74MNtUFnKWfWEneE+HLX8=
747+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4/go.mod h1:c4ubAQ2WIMYY/TOaBvbajROEFWv2HwhKeGOsEdLPIM0=
748748
github.com/onflow/flow-go-sdk v1.2.3 h1:jb+0dIXBO12Zt8x3c2xDXYPv6k3sRTUvhe59M+EcXTI=
749749
github.com/onflow/flow-go-sdk v1.2.3/go.mod h1:jMaffBTlAIdutx+pBhRIigLZFIBYSDDST0Uax1rW2qo=
750750
github.com/onflow/flow-nft/lib/go/contracts v1.2.2 h1:XFERNVUDGbZ4ViZjt7P1cGD80mO1PzUJYPfdhXFsGbQ=

tests/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ require (
66
github.com/goccy/go-json v0.10.2
77
github.com/onflow/cadence v1.2.2
88
github.com/onflow/crypto v0.25.2
9-
github.com/onflow/flow-emulator v1.1.1-0.20241216151608-ed31000dff47
9+
github.com/onflow/flow-emulator v1.1.1-0.20250103150158-e029ea3471fe
1010
github.com/onflow/flow-evm-gateway v0.0.0-20240201154855-4d4d3d3f19c7
11-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49
11+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4
1212
github.com/onflow/flow-go-sdk v1.2.3
1313
github.com/onflow/go-ethereum v1.14.7
1414
github.com/rs/zerolog v1.33.0

tests/go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -906,14 +906,14 @@ github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0 h1:R86HaOuk6vpuECZ
906906
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0/go.mod h1:9asTBnB6Tw2UlVVtQKyS/egYv3xr4zVlJnJ75z1dfac=
907907
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0 h1:u2DAG8pk0xFH7TwS70t1gSZ/FtIIZWMSNyiu4SeXBYg=
908908
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0/go.mod h1:pN768Al/wLRlf3bwugv9TyxniqJxMu4sxnX9eQJam64=
909-
github.com/onflow/flow-emulator v1.1.1-0.20241216151608-ed31000dff47 h1:OjYlGVC+rLxP7rCS74U0aqilChCjJrtvU52Bt7rZ62w=
910-
github.com/onflow/flow-emulator v1.1.1-0.20241216151608-ed31000dff47/go.mod h1:45cGyqSvI7o/oOHuYZkMc917f5JSKiCBQh30NV3w6Ks=
909+
github.com/onflow/flow-emulator v1.1.1-0.20250103150158-e029ea3471fe h1:0O/VrpF4ujhCqj5pzYmrb5qvghzf+1fxVxbp4jlneM4=
910+
github.com/onflow/flow-emulator v1.1.1-0.20250103150158-e029ea3471fe/go.mod h1:6Uj/ftISX4Su7blfRKTdENdyr29XAp364lT1ESgrh7s=
911911
github.com/onflow/flow-ft/lib/go/contracts v1.0.1 h1:Ts5ob+CoCY2EjEd0W6vdLJ7hLL3SsEftzXG2JlmSe24=
912912
github.com/onflow/flow-ft/lib/go/contracts v1.0.1/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
913913
github.com/onflow/flow-ft/lib/go/templates v1.0.1 h1:FDYKAiGowABtoMNusLuRCILIZDtVqJ/5tYI4VkF5zfM=
914914
github.com/onflow/flow-ft/lib/go/templates v1.0.1/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
915-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49 h1:kcAoHB/uEGP3wTJcv2aVtCHSgwY0bdn8qjw7GvvHfM8=
916-
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20241213150609-85fd812a7e49/go.mod h1:c4ubAQ2WIMYY/TOaBvbajROEFWv2HwhKeGOsEdLPIM0=
915+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4 h1:PSxMlqAgTzeTb/J6nknx3q74MNtUFnKWfWEneE+HLX8=
916+
github.com/onflow/flow-go v0.38.0-preview.0.4.0.20250102180624-72adf9e522c4/go.mod h1:c4ubAQ2WIMYY/TOaBvbajROEFWv2HwhKeGOsEdLPIM0=
917917
github.com/onflow/flow-go-sdk v1.2.3 h1:jb+0dIXBO12Zt8x3c2xDXYPv6k3sRTUvhe59M+EcXTI=
918918
github.com/onflow/flow-go-sdk v1.2.3/go.mod h1:jMaffBTlAIdutx+pBhRIigLZFIBYSDDST0Uax1rW2qo=
919919
github.com/onflow/flow-nft/lib/go/contracts v1.2.2 h1:XFERNVUDGbZ4ViZjt7P1cGD80mO1PzUJYPfdhXFsGbQ=

0 commit comments

Comments
 (0)