github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/consensus/bor/heimdallgrpc/client.go (about)

     1  package heimdallgrpc
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ethereum/go-ethereum/log"
     7  
     8  	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
     9  	proto "github.com/maticnetwork/polyproto/heimdall"
    10  
    11  	"google.golang.org/grpc"
    12  	"google.golang.org/grpc/codes"
    13  	"google.golang.org/grpc/credentials/insecure"
    14  )
    15  
    16  const (
    17  	stateFetchLimit = 50
    18  )
    19  
    20  type HeimdallGRPCClient struct {
    21  	conn   *grpc.ClientConn
    22  	client proto.HeimdallClient
    23  }
    24  
    25  func NewHeimdallGRPCClient(address string) *HeimdallGRPCClient {
    26  	opts := []grpc_retry.CallOption{
    27  		grpc_retry.WithMax(10000),
    28  		grpc_retry.WithBackoff(grpc_retry.BackoffLinear(5 * time.Second)),
    29  		grpc_retry.WithCodes(codes.Internal, codes.Unavailable, codes.Aborted, codes.NotFound),
    30  	}
    31  
    32  	conn, err := grpc.Dial(address,
    33  		grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)),
    34  		grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)),
    35  		grpc.WithTransportCredentials(insecure.NewCredentials()),
    36  	)
    37  	if err != nil {
    38  		log.Crit("Failed to connect to Heimdall gRPC", "error", err)
    39  	}
    40  
    41  	log.Info("Connected to Heimdall gRPC server", "address", address)
    42  
    43  	return &HeimdallGRPCClient{
    44  		conn:   conn,
    45  		client: proto.NewHeimdallClient(conn),
    46  	}
    47  }
    48  
    49  func (h *HeimdallGRPCClient) Close() {
    50  	log.Debug("Shutdown detected, Closing Heimdall gRPC client")
    51  	h.conn.Close()
    52  }