Last active
August 29, 2015 14:04
-
-
Save cyfdecyf/f9c14c48b802b3bf5dfd to your computer and use it in GitHub Desktop.
Too many open file error in net.Listener.Accept loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
Observe different behavior of net.Listener.Accept on Linux and OS X. | |
Steps: | |
1. Build this program on Linux or OS X. | |
2. Execute "ulimit -Sn 5" to limit the number of open file. (You may need to | |
change the number to 6 or other values.) | |
3. Run the program. | |
4. Execute "nc 127.0.0.1 2345" several times. | |
On OS X, each connection will generate one failure log. | |
On Linux, each connection will generate hundreds or thousands of logs. | |
*/ | |
import ( | |
"fmt" | |
"net" | |
"os" | |
) | |
func main() { | |
ln, err := net.Listen("tcp", "127.0.0.1:2345") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
errcnt := 0 | |
for { | |
_, err := ln.Accept() | |
if err != nil { | |
errcnt++ | |
fmt.Println(err, errcnt) | |
continue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment