Skip to content

Commit

Permalink
fix: Remove ctx from grpc client struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kayano committed Dec 20, 2023
1 parent 8b315d4 commit 053417d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
11 changes: 10 additions & 1 deletion cmd/validator-exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"flag"
"fmt"
"net/http"
"time"

"github.com/caarlos0/env/v10"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -28,7 +30,14 @@ func main() {
log.Fatal(err.Error())
}

_, err := grpc.LatestBlockHeight(cfg)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(cfg.Timeout)*time.Second,
)

defer cancel()

_, err := grpc.LatestBlockHeight(ctx, cfg)
if err != nil {
log.Fatal(err.Error())

Check failure on line 42 in cmd/validator-exporter/main.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

[GolangCI Lint] cmd/validator-exporter/main.go#L42

exitAfterDefer: log.Fatal will exit, and `defer cancel()` will not run (gocritic)
Raw output
cmd/validator-exporter/main.go:42:3: exitAfterDefer: log.Fatal will exit, and `defer cancel()` will not run (gocritic)
		log.Fatal(err.Error())
		^
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/collector/collector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package collector

import (
"context"
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -36,7 +38,14 @@ func (vc ValidatorsCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (vc ValidatorsCollector) Collect(ch chan<- prometheus.Metric) {
vals, err := grpc.SigningValidators(vc.Cfg)
ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(vc.Cfg.Timeout)*time.Second,
)

defer cancel()

vals, err := grpc.SigningValidators(ctx, vc.Cfg)
if err != nil {
log.Error(fmt.Sprintf("error getting signing validators: %s", err))
} else {
Expand Down
31 changes: 9 additions & 22 deletions pkg/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package grpc
import (
"context"
"fmt"
"time"

base "cosmossdk.io/api/cosmos/base/tendermint/v1beta1"

Expand All @@ -23,8 +22,6 @@ import (
const valConsStr = "valcons"

type Client struct {
ctx context.Context
ctxCancel context.CancelFunc
cfg config.Config
conn *grpc.ClientConn
connClose func()
Expand All @@ -35,14 +32,6 @@ func NewClient(cfg config.Config) (Client, error) {
cfg: cfg,
}

ctx, cancel := context.WithTimeout(
context.Background(),
time.Duration(client.cfg.Timeout)*time.Second,
)

client.ctx = ctx
client.ctxCancel = cancel

conn, err := cfg.GRPCConn()
if err != nil {
return Client{}, err
Expand All @@ -58,15 +47,15 @@ func NewClient(cfg config.Config) (Client, error) {
return client, nil
}

func (c Client) SignigInfos() ([]slashing.ValidatorSigningInfo, error) {
func (c Client) SignigInfos(ctx context.Context) ([]slashing.ValidatorSigningInfo, error) {
infos := []slashing.ValidatorSigningInfo{}
key := []byte{}
client := slashing.NewQueryClient(c.conn)

for {
request := &slashing.QuerySigningInfosRequest{Pagination: &query.PageRequest{Key: key}}

slashRes, err := client.SigningInfos(c.ctx, request)
slashRes, err := client.SigningInfos(ctx, request)
if err != nil {
return nil, err
}
Expand All @@ -93,7 +82,7 @@ func (c Client) SignigInfos() ([]slashing.ValidatorSigningInfo, error) {
return infos, nil
}

func (c Client) Validators() ([]staking.Validator, error) {
func (c Client) Validators(ctx context.Context) ([]staking.Validator, error) {
vals := []staking.Validator{}
key := []byte{}

Expand All @@ -106,7 +95,7 @@ func (c Client) Validators() ([]staking.Validator, error) {
for {
request := &staking.QueryValidatorsRequest{Pagination: &query.PageRequest{Key: key}}

stakingRes, err := client.Validators(c.ctx, request)
stakingRes, err := client.Validators(ctx, request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +149,7 @@ func (c Client) valConsMap(vals []staking.Validator) (map[string]staking.Validat
return vMap, nil
}

func SigningValidators(cfg config.Config) ([]types.Validator, error) {
func SigningValidators(ctx context.Context, cfg config.Config) ([]types.Validator, error) {
sVals := []types.Validator{}

client, err := NewClient(cfg)
Expand All @@ -171,16 +160,15 @@ func SigningValidators(cfg config.Config) ([]types.Validator, error) {
}

defer client.connClose()
defer client.ctxCancel()

sInfos, err := client.SignigInfos()
sInfos, err := client.SignigInfos(ctx)
if err != nil {
log.Error(err.Error())

return []types.Validator{}, err
}

vals, err := client.Validators()
vals, err := client.Validators(ctx)
if err != nil {
log.Error(err.Error())

Expand Down Expand Up @@ -210,7 +198,7 @@ func SigningValidators(cfg config.Config) ([]types.Validator, error) {
return sVals, nil
}

func LatestBlockHeight(cfg config.Config) (int64, error) {
func LatestBlockHeight(ctx context.Context, cfg config.Config) (int64, error) {
client, err := NewClient(cfg)
if err != nil {
log.Error(err.Error())
Expand All @@ -219,12 +207,11 @@ func LatestBlockHeight(cfg config.Config) (int64, error) {
}

defer client.connClose()
defer client.ctxCancel()

request := &base.GetLatestBlockRequest{}
baseClient := base.NewServiceClient(client.conn)

blockResp, err := baseClient.GetLatestBlock(client.ctx, request)
blockResp, err := baseClient.GetLatestBlock(ctx, request)
if err != nil {
log.Error(err.Error())

Expand Down

0 comments on commit 053417d

Please sign in to comment.