...
1 package main
2
3 import (
4 "context"
5 "fmt"
6 pb "golang.conradwood.net/apis/getestservice"
7 ge "golang.conradwood.net/apis/goeasyops"
8 "golang.conradwood.net/go-easyops/auth"
9 "golang.conradwood.net/go-easyops/authremote"
10 "golang.conradwood.net/go-easyops/utils"
11 "time"
12 )
13
14 func do_client() error {
15 c := pb.GetEchoClient()
16
17
23
24 seq := uint32(0)
25 my_tags := parse_tags()
26 for {
27 now := time.Now()
28 ctx := clientContext()
29 u := auth.GetUser(ctx)
30 seq++
31 fmt.Printf(" %s pinging, SEQ=%d, as %s...", printTags(), seq, auth.Description(u))
32 resp, err := c.Ping(ctx, &pb.PingRequest{SequenceNumber: seq, TTL: uint32(*ttl)})
33 if err != nil {
34 fmt.Printf("Error :%s\n", utils.ErrorString(err))
35 }
36 if !tagsEqual(my_tags, resp.ServerTags) {
37 fmt.Printf("TAG mismatch (local:%s vs server:%s)\n", printMap(my_tags), printMap(resp.ServerTags))
38 return fmt.Errorf("tag mismatch")
39 }
40 dur := time.Since(now).Milliseconds()
41 fmt.Printf("%d milliseconds\n", dur)
42 if !*ping {
43 return nil
44 }
45 time.Sleep(time.Duration(300) * time.Millisecond)
46 }
47 }
48
49 func clientContext() context.Context {
50 if *tag == "" {
51 return authremote.Context()
52 }
53 rt := &ge.CTXRoutingTags{
54 Tags: parse_tags(),
55 FallbackToPlain: *fallback,
56 Propagate: false,
57 }
58 ctx := authremote.ContextWithTimeoutAndTags(time.Duration(2)*time.Second, rt)
59 return ctx
60 }
61 func printTags() string {
62 return fmt.Sprintf("\"%s\"", *tag)
63 }
64 func printMap(m map[string]string) string {
65 deli := ""
66 s := ""
67 for k, v := range m {
68 s = s + deli + k + "=" + v
69 deli = ", "
70 }
71 return "\"" + s + "\""
72 }
73
74 func tagsEqual(cl, srv map[string]string) bool {
75 if *fallback {
76 if len(srv) == 0 {
77 return true
78 }
79 }
80 if len(cl) != len(srv) {
81 return false
82 }
83 for k, v := range cl {
84 if srv[k] != v {
85 return false
86 }
87 }
88 return true
89 }
90
View as plain text