GoLang simple REST API should use GoRoutines

Issue

My question is very simple.

Should I make use of GoRoutines for a very simple REST API?

I basically only do simple queries to a DB, verify sessions, or do logins. Is any of this operations worth setting up a GoRoutine? When are GoRoutines useful, and how should I set them up?

Solution

The net/http package already takes care of this for you. Once you call Serve (or more likely ListenAndServe) the following happens:

Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them. Handler is typically nil, in which case the DefaultServeMux is used.

See http://golang.org/pkg/net/http/ for more.

You may want another goroutine if a request triggers the need for longer processing and you don’t want to make the client wait.

Answered By – Dmitri Goldring

Answer Checked By – Pedro (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.