github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/state/runtime/executor/client.go (about)

     1  package executor
     2  
     3  import (
     4  	"context"
     5  	"os/exec"
     6  	"time"
     7  
     8  	"github.com/0xPolygon/supernets2-node/log"
     9  	"github.com/0xPolygon/supernets2-node/state/runtime/executor/pb"
    10  	"google.golang.org/grpc"
    11  	"google.golang.org/grpc/credentials/insecure"
    12  )
    13  
    14  const maxMsgSize = 100000000
    15  
    16  // NewExecutorClient is the executor client constructor.
    17  func NewExecutorClient(ctx context.Context, c Config) (pb.ExecutorServiceClient, *grpc.ClientConn, context.CancelFunc) {
    18  	opts := []grpc.DialOption{
    19  		grpc.WithTransportCredentials(insecure.NewCredentials()),
    20  		grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),
    21  		grpc.WithBlock(),
    22  	}
    23  	const maxWaitSeconds = 120
    24  	const maxRetries = 5
    25  	ctx, cancel := context.WithTimeout(ctx, maxWaitSeconds*time.Second)
    26  
    27  	connectionRetries := 0
    28  
    29  	var executorConn *grpc.ClientConn
    30  	var err error
    31  	delay := 2
    32  	for connectionRetries < maxRetries {
    33  		log.Infof("trying to connect to executor: %v", c.URI)
    34  		executorConn, err = grpc.DialContext(ctx, c.URI, opts...)
    35  		if err != nil {
    36  			log.Infof("Retrying connection to executor #%d", connectionRetries)
    37  			time.Sleep(time.Duration(delay) * time.Second)
    38  			connectionRetries = connectionRetries + 1
    39  			out, err := exec.Command("docker", []string{"logs", "supernets2-prover"}...).Output()
    40  			if err == nil {
    41  				log.Infof("Prover logs:\n%s\n", out)
    42  			}
    43  		} else {
    44  			log.Infof("connected to executor")
    45  			break
    46  		}
    47  	}
    48  
    49  	if connectionRetries == maxRetries {
    50  		log.Fatalf("fail to dial: %v", err)
    51  	}
    52  	executorClient := pb.NewExecutorServiceClient(executorConn)
    53  	return executorClient, executorConn, cancel
    54  }