Commit 82f96927 authored by Ben Wilson's avatar Ben Wilson

Added context to GetTotalElements

parent a756c1f3
...@@ -12,12 +12,11 @@ import ( ...@@ -12,12 +12,11 @@ import (
// CTC interacts with the OVM CTC contract // CTC interacts with the OVM CTC contract
type CTC struct { type CTC struct {
Ctx context.Context
Address common.Address Address common.Address
Client *ethclient.Client Client *ethclient.Client
} }
func (ctc *CTC) GetTotalElements() (*big.Int, error) { func (ctc *CTC) GetTotalElements(ctx context.Context) (*big.Int, error) {
contract, err := bindings.NewCanonicalTransactionChainCaller(ctc.Address, ctc.Client) contract, err := bindings.NewCanonicalTransactionChainCaller(ctc.Address, ctc.Client)
if err != nil { if err != nil {
...@@ -25,7 +24,7 @@ func (ctc *CTC) GetTotalElements() (*big.Int, error) { ...@@ -25,7 +24,7 @@ func (ctc *CTC) GetTotalElements() (*big.Int, error) {
} }
totalElements, err := contract.GetTotalElements(&bind.CallOpts{ totalElements, err := contract.GetTotalElements(&bind.CallOpts{
Context: ctc.Ctx, Context: ctx,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
......
package main package main
import ( import (
"context"
"math/big" "math/big"
"net/http" "net/http"
"os" "os"
...@@ -13,6 +14,10 @@ import ( ...@@ -13,6 +14,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
var (
l1TimeoutSeconds = 5
)
func main() { func main() {
listenAddress := os.Getenv("LISTEN_ADDRESS") listenAddress := os.Getenv("LISTEN_ADDRESS")
if listenAddress == "" { if listenAddress == "" {
...@@ -64,17 +69,20 @@ func getCTCTotalElements(address string, client *ethclient.Client) { ...@@ -64,17 +69,20 @@ func getCTCTotalElements(address string, client *ethclient.Client) {
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop() defer ticker.Stop()
for { for {
totalElements, err := ctc.GetTotalElements() ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(l1TimeoutSeconds))
totalElements, err := ctc.GetTotalElements(ctx)
if err != nil { if err != nil {
ctcTotalElementsCallSuccess.Set(0) ctcTotalElementsCallSuccess.Set(0)
log.Error("Error calling GetTotalElements", "error", err) log.Error("Error calling GetTotalElements", "error", err)
cancel()
continue continue
} }
ctcTotalElementsCallSuccess.Set(1) ctcTotalElementsCallSuccess.Set(1)
totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64() totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64()
ctcTotalElements.WithLabelValues( ctcTotalElements.WithLabelValues(
"latest").Set(totalElementsFloat) "latest").Set(totalElementsFloat)
log.Info("ctc updated", "ctcTotalElements", totalElementsFloat)
ctx.Done()
<-ticker.C <-ticker.C
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment