Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Go's HTTP server for production [closed]

Tags:

go

I'm a PHP developer new to Golang. PHP comes with an inbuilt server which is not recommended for production. I read the beautiful book by astaxie on web development in Go and also the golang.org example on writing a web app. They both use the http.ListenAndServe() to create a web server. I just want to know if this server can be used in production or is it just a dummy server like PHP inbuilt server? By production I mean, can it handle huge traffic like an Apache or ngnix server?

like image 855
Akash Kumar Sharma Avatar asked Jun 14 '15 17:06

Akash Kumar Sharma


People also ask

Is Go HTTP server production ready?

Short answer: YES.

How do you stop a go server?

Server provides Shutdown method for graceful shutdown. Run a server and send a request to /shutdown , then the server stops.

What can you do with an HTTP server?

The main function of a HTTP server is to store, process and deliver web pages to clients. Pages delivered are usually HTML documents, which may include images, style sheets and scripts in addition to text content.


2 Answers

Short answer: YES.

Longer answer: you can certainly use the built in Web server for production traffic. There's a good chance you've used it today, since Google serves some traffic using it. I know a lot of companies, including the one I work for that use it for production traffic. BTW I don't know of a Web server widely used in Go that does not use the standard web server.

Keep in mind though that you need to tweak things like client timeouts to make it really robust, and perhaps limit incoming connections, etc.

like image 199
Not_a_Golfer Avatar answered Sep 19 '22 21:09

Not_a_Golfer


To add to @Not_a_Golfer's answer: Go's web-server is extremely solid and pretty well tested thus far.

There are, however, reasons why you might put it behind a reverse proxy like nginx, Apache or HAProxy, including:

  • SSL termination (nginx has a lot of additional TLS features that you'll have to partially implement yourself, like easy OCSP stapling support)
  • Proxy caching (serving static responses from cache, or 500 if your Go app crashes)
  • Performant logging
  • SPDY support (although both Go & nginx will probably get HTTP/2 at the same time)
  • Built-in gzip support and options
  • File descriptor caching for static files

I generally prefer to put nginx in front if I have the option, because although you can implement all of those features in Go, there's a bit of wheel re-invention going on. You can get most of the way there with middleware like gorilla/handlers and Go's own crypto/tls lib, and if you're not an ops person and/or want to keep things really thin, then running everything directly from Go is still fine for production.

like image 38
elithrar Avatar answered Sep 19 '22 21:09

elithrar