grpc-ping/ping_client/main.go

57 lines
1.1 KiB
Go
Raw Normal View History

2021-07-07 15:13:36 +00:00
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"google.golang.org/grpc"
2021-07-11 18:42:04 +00:00
pb "grpc-ping/ping"
2021-07-07 15:13:36 +00:00
)
const (
address = "localhost:50051"
)
func genPayload(s int) string {
var p string
var start int = 60
for i := start; i < (start + s); i++ {
p += string(i)
}
return p
}
func main() {
2021-07-11 18:41:25 +00:00
var i uint64
2021-07-07 15:13:36 +00:00
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
data := genPayload(64)
if len(os.Args) > 1 {
data = os.Args[1]
}
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewPingClient(conn)
for i = 1; ; i++ {
rtt := time.Now()
2021-07-11 18:42:22 +00:00
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
2021-07-07 15:13:36 +00:00
r, err := c.Ping(ctx, &pb.PingRequest{Data: data, Seq: i})
if err != nil {
log.Printf("could not connect to: %v", err)
time.Sleep(1 * time.Second)
continue
}
fmt.Printf("%d bytes from %s: seq=%d time=%s\n", len(r.Data), address, r.Seq, time.Since(rtt))
time.Sleep(1 * time.Second)
}
}