-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (49 loc) · 1.08 KB
/
main.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
53
54
55
56
57
package main
import (
"context"
"fmt"
"log"
"os"
"time"
pb "github.com/philipsahli/gontador/service"
"google.golang.org/grpc"
)
var c pb.CounterClient
var ctx context.Context
var cancel context.CancelFunc
func inc() {
r, err := c.Count(ctx, &pb.Empty{})
if err != nil {
log.Fatalf("could not increment counter: %v", err)
}
log.Printf("Counter: %s", fmt.Sprint(r.Counter))
}
func get() {
r, err := c.GetCounter(ctx, &pb.Empty{})
if err != nil {
log.Fatalf("could not get counter: %v", err)
}
log.Printf("Counter: %s", fmt.Sprint(r.Counter))
}
func main() {
// Set up a connection to the server.
address := os.Getenv("SERVER_ADDRESS")
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c = pb.NewCounterClient(conn)
// Contact the server and print out its response.
// name := defaultName
// if len(os.Args) > 1 {
// name = os.Args[1]
// }
for {
ctx, cancel = context.WithTimeout(context.Background(), 4*time.Second)
inc()
time.Sleep(2 * time.Second)
get()
defer cancel()
}
}