client exponential backoff

This commit is contained in:
Stephen McQuay 2018-01-26 18:05:04 -08:00
parent 5997dc44d8
commit 82946a1480
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5

View File

@ -3,9 +3,11 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/twitchtv/twirp" "github.com/twitchtv/twirp"
@ -36,12 +38,18 @@ func main() {
for i := 0; ; i++ { for i := 0; ; i++ {
s <- true s <- true
go func(j int) { go func(j int) {
sleep := time.Duration(250+rand.Intn(500)) * time.Millisecond
for {
resp, err := c.Hello(ctx, &pb.HelloReq{Subject: strings.Join(os.Args[2:], " ")}) resp, err := c.Hello(ctx, &pb.HelloReq{Subject: strings.Join(os.Args[2:], " ")})
if err != nil { if err == nil {
fmt.Fprintf(os.Stderr, "hello: %#v\n", err)
os.Exit(1)
}
fmt.Printf("0x%08x: %#v\n", j, resp) 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
}
<-s <-s
}(i) }(i)
} }