#net/http server

19 messages · Page 1 of 1 (latest)

errant gazelle
#

does the net/http handler require me to run handlers in goroutine? nope right, bcs net/http already runs the func (e *Engine) handler() in goroutines right?

flat hamlet
#

you must NOT run it in goroutines.
net/http will perform some cleanup when you return from the Handle function

errant gazelle
#

but; http.ListenAndServe() runs the handler in goroutines right

#

?

flat hamlet
#

so you would have a data race between net/http and your goroutine
*more precisely you can use goroutines but if you do you need to use sync.WaitGroup or some other synchronisation mechanism to return from Handle only after you are finished

#

yes net/http spawn one goroutine per invocation already

errant gazelle
#

ye then i dont require to add goroutines anymore

#

only for in the body of the handle function but it will sync

flat hamlet
#

?

errant gazelle
#

when user a enters /hello and user b enters /hello at the same time, net/http pushes 2 seperate goroutines for the handler, in the handler like for example to do some database lookup i can sync goroutines it doesnt block any other client

#

did i understand it correct?

errant gazelle
#

oke thnx

flat hamlet
#

your database internally maintain locks, but theses are ideally around very small sections of code

#

so the blocking is really small

#

usally writing is worst than reading (as-in, it blocks more).

errant gazelle
#

go has implemented net/http Server concept pretty well its very easy to customize and make your own framework

flat hamlet
#

like two readers is usually exactly as fast as one reader (assuming you have two cores).
However a reader plus a writer is slower.