-
Notifications
You must be signed in to change notification settings - Fork 688
/
client.go
30 lines (26 loc) · 975 Bytes
/
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
package broadcast
import (
"context"
"time"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/sequencer/broadcast/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// NewClient creates a grpc client to communicates with the Broadcast server
func NewClient(ctx context.Context, serverAddress string) (pb.BroadcastServiceClient, *grpc.ClientConn, context.CancelFunc) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}
const maxWaitSeconds = 120
ctx, cancel := context.WithTimeout(ctx, maxWaitSeconds*time.Second)
log.Infof("connecting to broadcast service: %v", serverAddress)
conn, err := grpc.DialContext(ctx, serverAddress, opts...)
if err != nil {
log.Fatalf("failed to connect to broadcast service: %v", err)
}
client := pb.NewBroadcastServiceClient(conn)
log.Info("connected to broadcast service")
return client, conn, cancel
}