Skip to content

Instantly share code, notes, and snippets.

@kostya-sh
Created June 4, 2016 09:42
Show Gist options
  • Select an option

  • Save kostya-sh/392dc8a7ccfcce0961087e1e7cc81b07 to your computer and use it in GitHub Desktop.

Select an option

Save kostya-sh/392dc8a7ccfcce0961087e1e7cc81b07 to your computer and use it in GitHub Desktop.
package main
import (
"sync"
"github.com/valyala/fasthttp"
)
func main() {
mutex := &sync.Mutex{}
m := make(map[int][]byte)
cnt := 0
max := 250000
pool := sync.Pool{
New: func() interface{} {
return make([]byte, 1024)
},
}
handler := func(ctx *fasthttp.RequestCtx) {
newBuffer := pool.Get().([]byte)
for i := range newBuffer {
newBuffer[i] = byte(i)
}
mutex.Lock()
m[cnt] = newBuffer
if cnt >= max {
buf := m[cnt-max]
delete(m, cnt-max)
pool.Put(buf)
}
cnt++
mutex.Unlock()
ctx.WriteString("OK")
}
fasthttp.ListenAndServe(":8080", handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment