Skip to content

Instantly share code, notes, and snippets.

@hedwigz
Last active September 9, 2019 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedwigz/501e49e12a83f8340145a72f65bd57f5 to your computer and use it in GitHub Desktop.
Save hedwigz/501e49e12a83f8340145a72f65bd57f5 to your computer and use it in GitHub Desktop.
try reproduce resty race
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"sync"
"sync/atomic"
"time"
resty "gopkg.in/resty.v1"
)
func serverErrorRetryCondition(r *resty.Response) (bool, error) {
retryStatuses := [...]int{
http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout}
respStatus := r.StatusCode()
for _, status := range retryStatuses {
if status == respStatus {
return true, nil
}
}
return false, nil
}
func main() {
client := resty.New().SetHostURL("http://localhost:9988")
client.SetRetryCount(3).SetRetryMaxWaitTime(time.Minute).AddRetryCondition(serverErrorRetryCondition)
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
go server()
wg := &sync.WaitGroup{}
numRoutines := 10
wg.Add(numRoutines)
for i := 0; i < numRoutines; i++ {
go executer(client, wg)
}
wg.Wait()
}
func server() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(500)
request.Write(writer)
})
var srv http.Server
srv.Addr = ":9988"
log.Fatal(srv.ListenAndServe())
}
const msg = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890v"
var count int32
func executer(client *resty.Client, wg *sync.WaitGroup) {
for i := 0; i < 100; i++ {
req := client.NewRequest().SetHeader("x-header", msg)
req.SetBody([]byte(msg))
_, err := req.Execute("POST", "/")
cnt := atomic.AddInt32(&count, 1)
fmt.Printf("Executing: %d\n", cnt)
if err != nil {
panic(err)
}
}
wg.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment