Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 27, 2023 10:05
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 arriqaaq/07773a6f1bf85e7217211f3e0d90a2f5 to your computer and use it in GitHub Desktop.
Save arriqaaq/07773a6f1bf85e7217211f3e0d90a2f5 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
type RequestHandler struct {
sharedState string
}
var handlerPool = sync.Pool{
New: func() interface{} {
return &RequestHandler{}
},
}
func handleRequest(requestID int) {
handler := handlerPool.Get().(*RequestHandler)
defer handlerPool.Put(handler)
// Use the handler to handle the request
fmt.Printf("Handling request %d with handler %p\n", requestID, handler)
}
func main() {
for i := 0; i < 10; i++ {
go handleRequest(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment