Skip to content

Commit

Permalink
extract helper initializeDeciderClient()
Browse files Browse the repository at this point in the history
addresses review comment ava-labs/awm-relayer#344 (comment)
  • Loading branch information
feuGeneA committed Jul 19, 2024
1 parent c4133c3 commit fefcf44
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,33 +222,20 @@ func main() {
log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", cfg.APIPort), nil))
}()

errGroup, ctx := errgroup.WithContext(context.Background())

if cfg.DeciderPort != nil {
port := strconv.FormatUint(uint64(*cfg.DeciderPort), 10)

host := cfg.DeciderHost
if len(host) == 0 {
host = "localhost"
}

grpcClient, err = grpc.NewClient(
strings.Join([]string{host, port}, ":"),
grpc.WithTransportCredentials(
insecure.NewCredentials(),
),
grpcClient, err = initializeDeciderClient(
cfg.DeciderHost,
cfg.DeciderPort,
)
if err != nil {
logger.Fatal(
"Failed to instantiate decider client",
zap.Error(err),
)
if err != nil {
logger.Fatal(
"Failed to instantiate decider client",
zap.Error(err),
)
panic(err)
}
runtime.SetFinalizer(grpcClient, func(c *grpc.ClientConn) { c.Close() })
panic(err)
}

// Create listeners for each of the subnets configured as a source
errGroup, ctx := errgroup.WithContext(context.Background())
for _, s := range cfg.SourceBlockchains {
sourceBlockchain := s

Expand Down Expand Up @@ -485,6 +472,39 @@ func startMetricsServer(logger logging.Logger, gatherer prometheus.Gatherer, por
}()
}

func initializeDeciderClient(host string, port *uint16) (*grpc.ClientConn, error) {
if port == nil {
return nil, nil
}

if len(host) == 0 {
host = "localhost"
}

client, err := grpc.NewClient(
strings.Join(
[]string{host, strconv.FormatUint(uint64(*port), 10)},
":",
),
grpc.WithTransportCredentials(
insecure.NewCredentials(),
),
)
if err != nil {
return nil, fmt.Errorf(
"Failed to instantiate grpc client: %w",
err,
)
}

runtime.SetFinalizer(
client,
func(c *grpc.ClientConn) { c.Close() },
)

return client, nil
}

func initializeMetrics() (prometheus.Gatherer, prometheus.Registerer, error) {
gatherer := metrics.NewMultiGatherer()
registry := prometheus.NewRegistry()
Expand Down

0 comments on commit fefcf44

Please sign in to comment.