Created
January 27, 2023 10:05
-
-
Save arriqaaq/07773a6f1bf85e7217211f3e0d90a2f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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