Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoLang simple REST API should use GoRoutines

Tags:

rest

go

goroutine

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?

like image 837
doart3 Avatar asked Mar 07 '14 10:03

doart3


1 Answers

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.

like image 186
Dmitri Goldring Avatar answered Nov 02 '22 18:11

Dmitri Goldring