hwt/cmd/hwtc/main.go

57 lines
1.1 KiB
Go
Raw Normal View History

2018-01-16 20:57:04 -08:00
package main
import (
"context"
"fmt"
2018-01-26 18:05:04 -08:00
"math/rand"
2018-01-16 20:57:04 -08:00
"net/http"
"os"
"strings"
2018-01-26 18:05:04 -08:00
"time"
2018-01-16 20:57:04 -08:00
2018-01-21 20:18:28 -08:00
"github.com/twitchtv/twirp"
"mcquay.me/hwt"
pb "mcquay.me/hwt/rpc/hwt"
2018-01-16 20:57:04 -08:00
)
2018-01-20 08:49:02 -08:00
const usage = "hwtc <server address> [subject]"
2018-01-16 20:57:04 -08:00
func main() {
2018-01-20 08:49:02 -08:00
if len(os.Args) < 3 {
2018-01-16 20:57:04 -08:00
fmt.Fprintf(os.Stderr, "%v\n", usage)
os.Exit(1)
}
2018-01-20 08:49:02 -08:00
c := pb.NewHelloWorldProtobufClient(fmt.Sprintf("http://%s", os.Args[1]), &http.Client{})
2018-01-16 20:57:04 -08:00
2018-01-21 20:18:28 -08:00
h := http.Header{}
h.Set("sm-auth", hwt.PSK)
ctx := context.Background()
ctx, err := twirp.WithHTTPRequestHeaders(ctx, h)
if err != nil {
fmt.Fprintf(os.Stderr, "setting twirp headers: %v\n", err)
os.Exit(1)
}
2018-01-26 17:15:13 -08:00
s := make(chan bool, 10)
2018-01-21 20:12:05 -08:00
for i := 0; ; i++ {
2018-01-26 17:15:13 -08:00
s <- true
go func(j int) {
2018-01-26 18:05:04 -08:00
sleep := time.Duration(250+rand.Intn(500)) * time.Millisecond
for {
resp, err := c.Hello(ctx, &pb.HelloReq{Subject: strings.Join(os.Args[2:], " ")})
if err == nil {
fmt.Printf("0x%08x: %#v\n", j, resp)
break
}
fmt.Fprintf(os.Stderr, "sleeping %v because: %v\n", sleep, err)
time.Sleep(sleep)
sleep *= 2
continue
2018-01-26 17:15:13 -08:00
}
<-s
}(i)
2018-01-16 20:57:04 -08:00
}
}