I need to write a simple web server in Go. It accepts requests, maps the request to avro object and sends it to Kafka. The requirement is that it answers immediately to keep the latency low for the users. Mapping to avro object and sending to Kafka can happen asynchronously. I came up with the following design, but I wonder if it is using Go structures in the intended way or if it can be optimized using channels for example. I'm omitting private methods and initializing structures. The problem is that the server can handle up to 10500 requests a second and then the response time goes up dramatically. So I was wondering if there is a way to optimize it.
func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) // not needed in Go 1.5.0
server := &Server{
Producer: newProducer(brokerList),
}
defer func() {
if err := server.Close(); err != nil {
Error.Println("Failed to close server", err)
}
}()
Error.Fatal(server.Run(*addr))
}
func (s *Server) Run(addr string) error {
httpServer := &http.Server{
Addr: addr,
Handler: s.Handler(),
}
return httpServer.ListenAndServe()
}
func (s *Server) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
req, err := ParseRequest(r.Body)
if err != nil {
Warning.Println("Failed to parse request", err.Error())
} else {
go handleRequest(s, req)
}
w.WriteHeader(204) // respond with 'no bid'
)
}
func handleRequest(s *Server, req *openrtb.BidRequest) {
req.Validate()
var avroObject, err = createAvro(req)
if err != nil {
Warning.Printf(err.Error())
}
if avroObject != nil {
sendToKafka(avroObject, s)
}
}
}