#Chi Router Webserver concurrent programming

5 messages · Page 1 of 1 (latest)

hushed finch
#

Hi guys I am relatively new to GO and I already love it. As my first project I want to build a simple web API. And I use the chi router for it.

In the past I used Python for my webserver and I had NGINX as reverse proxy and then GUNICORN prefork workers, I think something like number of CPU cores plus one, in the RAM that had my DJANGO backend ready.

For my GO project I want to keep NGINX for SSL and caching and point from there to my GO backend API. (I plan on using DOCKER to learn it)

The question is now how are the multiple cores of my server used? Do I have to start multiple DOCKER containers of the GO backend? One for each Core? Or is the multiple core support handled within the chi router already build in? Or do I have to include my own implementation of go functions goroutines? in my single instance?

something like:


v1Router := chi.NewRouter()

    // option from a tutorial I followed:
    v1Router.Get("/healthz",handlerReadiness)
    // option with goroutine do I have to use it like this to use all cores well?
    v1Router.Get("/healtz", func(w http.ResponseWriter, r *http.Request) {
        go handlerReadiness(w, r) // handle each request in a new goroutine
    })
// the handler FYI:
package goback

import "net/http"

func handlerReadiness(w http.ResponseWriter, r *http.Request) {

    respondWithJSON(w, 200, struct{}{})

}
    

I hope my question is clear. If not I am happy to clarify what I want.
Thank you for your help.

hushed finch
#

Ok while investigating I found that http.ListenAndServe seems to already start goroutines? Is this correct? hence there is no need for simple requests to do this, only if there is a task to be done that requires concurrency itself...

feral basalt
hushed finch
#

thanks, initially I was looking for the concurrency in the chi-router code...

What is your oppinion about using NGINX before the GO server?

analog fossil
#

As a reverse proxy in a Kubernetes or Docker environment, it is totally useful for routing between multiple services. If you use the integrated template engine of Go, I think it is not necessary