If it is interesting to you, what defect is in all web applications in the Go language placed on Habré. And if you want to know how to make your Go web application one step closer to production ready, then welcome to cat.
The important difference with Go web programming is that as a result you usually get a program that is a web server. Therefore, you become responsible for what the web server usually answers.
If we compare how Hello World looks like in PHP and Go, we will see
Php
<?php echo “Hello World”; ?>
Go
package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
Taken from the Go Language
wiki .
')
Similar code is used in the following articles on Habré:
Web development on Go ,
Writing a web terminal emulator on Go, using Websocket ,
Goblog: Homemade static blog engine for Go ,
Writing your Web application on Go .
I myself, in the article
My Certificate Authority - in 5 OpenSSL commands , cited a similar example on Go.
Close on topic:
Go Language. A small client-server application ,
TCP / IP proxy on Go .
An important difference and advantage of Go is how Go works with the network and can hold many open connections, for example, to implement long polling.
In the article
C10k (Problem 10,000 connections) in different languages / platforms, Go showed the second result after Erlang, opening 9775 connections from 10,000.
The second advantage of Go is the simplicity and readability of the code, ease of learning, which makes it often the No. 1 choice when developing applications in which you need to work with many open connections.
Error Lack of all these examples.
The flaw is that Timeout (Deadline) for connections is 0 by default (i.e. timeout is not set at all). If the client does not send a packet stating that the connection is closed, then such a connection will hang forever. This will block goroutine in idle mode until the end of the web server’s life. The number of open connection resource is usually limited. By default, in Linux, an application can open 1024 files (a TCP connection is equal to a file).
This means - the server created on Go as follows
http.ListenAndServe(":8080", nil)
Will gradually accumulate not closed compounds and choke them in a day, month or year.
Therefore, it is important to install Timeouts, which are installed by default in regular web servers.
for example
The second
drawback parameter, which in a regular web server is often indicated is the maximum number of connections that the web server is ready to open. If this restriction is reached, the web server begins to sacrifice keep-alive connections or give an error and close the connections.
Go does not do this by default, and it’s not at all obvious how to limit the number of connections in Go or count the number of connections or, moreover, control this process.
To figure out how to manage connections, you need to look into the source code of
func (* Server) ListenAndServe and
func (* Server) ListenAndServeTLS , then we will see that both functions use the function
func (* Server) Serve .
This function takes the
net.Listener interface as an argument.
This is what we can implement to limit and control connections.
An example of a wrapper around a
net.Listener interface is
LimitListener
Having passed on articles on Habré with a tag
[go] I did not find examples in which Timeout, DeadLine were installed or the number of connections was controlled.
If we consider that Go was created as a super-parallel, network language, then the implementation of a correct and well-thought-out strategy for working with connections is a mandatory element of the Go application.