grpc-ping/ping_client/main.go

59 lines
1.2 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "lume-grpc/ping"
)
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() {
var i uint64
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()
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
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
}
// log.Printf("%d characters roundtrip to (%s): seq=%d time=%s", len(r.Data), address, r.Seq, time.Since(rtt))
fmt.Printf("%d bytes from %s: seq=%d time=%s\n", len(r.Data), address, r.Seq, time.Since(rtt))
//log.Printf("Received: %v", r.Data)
time.Sleep(1 * time.Second)
}
}