Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Created June 13, 2019 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikdubbelboer/6ca1686a8ffc00fb757b0cc46efc7ac6 to your computer and use it in GitHub Desktop.
Save erikdubbelboer/6ca1686a8ffc00fb757b0cc46efc7ac6 to your computer and use it in GitHub Desktop.
package main
import (
"math/rand"
"net"
//"runtime"
"sync/atomic"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
var (
ln *fasthttputil.InmemoryListener
c *fasthttp.Client
t int64
)
func doit() {
request := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(request)
response := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(response)
request.SetRequestURI("http://localhost:9090")
// Only 8128 gorountines allowed when using the race detector.
// Some of the DoTimeout calls haven't started their goroutine yet
// so use a lower limit here.
//if runtime.NumGoroutine() > 8000 {
// return
//}
n := rand.Intn(10)
for i := 0; i < n; i++ {
if err := c.DoTimeout(request, response, time.Second); err != nil {
if err == fasthttp.ErrTimeout {
atomic.AddInt64(&t, 1)
} else if err != nil {
panic(err)
}
}
}
}
func main() {
ln = fasthttputil.NewInmemoryListener()
c = &fasthttp.Client{
Dial: func(addr string) (net.Conn, error) {
return ln.Dial()
},
MaxConnsPerHost: 10000,
}
go func() {
r := int64(0)
go func() {
for {
time.Sleep(time.Second)
rr := atomic.SwapInt64(&r, 0)
tt := atomic.SwapInt64(&t, 0)
println(rr, tt)
}
}()
if err := fasthttp.Serve(ln, func(ctx *fasthttp.RequestCtx) {
atomic.AddInt64(&r, 1)
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
}); err != nil {
panic(err)
}
}()
time.Sleep(time.Millisecond * 10)
n := int64(10000)
for {
time.Sleep(time.Millisecond)
// Only 8128 gorountines allowed when using the race detector.
// Some of the DoTimeout calls haven't started their goroutine yet
// so use a lower limit here.
//if runtime.NumGoroutine() > 7000 {
// time.Sleep(time.Millisecond * 10)
// continue
//}
if atomic.LoadInt64(&n) > 0 {
atomic.AddInt64(&n, -1)
go func() {
doit()
atomic.AddInt64(&n, 1)
}()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment