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