Hi group,

I am going through this page: https://studygolang.com/articles/22820 , It 
claims that the 50 lines of Go code handles 1 million concurrent connections 
from network clients, on 1 single server machine with a 4-Core CPU and 16G 
memory.


Does the package net already utilize IO Multiplexing internally, like epoll 
(epoll_create, epoll_ctl, epoll_wait)? So I can just use package net and gain 
the epoll ability automatically without calling epoll apis manually in Go?


Thanks


Server:
```
package main


import (
    "fmt"
    "net"
    "os"
    "time"
)


var array []byte = make([]byte, 10)


func checkError(err error, info string) (res bool) {


    if err != nil {
        fmt.Println(info + "  " + err.Error())
        return false
    }
    return true
}


func Handler(conn net.Conn) {
    for {
        _, err := conn.Write(array)
        if err != nil {
            return
        }
        time.Sleep(10 * time.Second)
    }
}


func main() {


&nbsp; &nbsp; for i := 0; i < 10; i += 1 {
&nbsp; &nbsp; &nbsp; &nbsp; array[i] = 'a'
&nbsp; &nbsp; }


&nbsp; &nbsp; service := ":8888"
&nbsp; &nbsp; tcpAddr, _ := net.ResolveTCPAddr("tcp4", service)
&nbsp; &nbsp; l, _ := net.ListenTCP("tcp", tcpAddr)


&nbsp; &nbsp; for {
&nbsp; &nbsp; &nbsp; &nbsp; conn, err := l.Accept()
&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("accept error, err=%s\n", 
err.Error())
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; go Handler(conn)
&nbsp; &nbsp; }


}
```


Client:


```
package main


import (
&nbsp; &nbsp; "flag"
&nbsp; &nbsp; "fmt"
&nbsp; &nbsp; "net"
&nbsp; &nbsp; "os"
&nbsp; &nbsp; "time"
)


var RemoteAddr *string
var ConcurNum *int
var LocalAddr *string


func init() {
&nbsp; &nbsp; RemoteAddr = flag.String("remote-ip", "127.0.0.1", "ip addr of 
remote server")
&nbsp; &nbsp; ConcurNum = flag.Int("concurrent-num", 100, "concurrent number of 
client")
&nbsp; &nbsp; LocalAddr = flag.String("local-ip", "0.0.0.0", "ip addr of remote 
server")
}


func consume() {


&nbsp; &nbsp; laddr := &amp;net.TCPAddr{IP: net.ParseIP(*LocalAddr)}


&nbsp; &nbsp; var dialer net.Dialer
&nbsp; &nbsp; dialer.LocalAddr = laddr


&nbsp; &nbsp; conn, err := dialer.Dial("tcp", *RemoteAddr+":8888")
&nbsp; &nbsp; if err != nil {
&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("dial failed:", err)
&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)
&nbsp; &nbsp; }
&nbsp; &nbsp; defer conn.Close()


&nbsp; &nbsp; buffer := make([]byte, 512)


&nbsp; &nbsp; for {
&nbsp; &nbsp; &nbsp; &nbsp; _, err2 := conn.Read(buffer)
&nbsp; &nbsp; &nbsp; &nbsp; if err2 != nil {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Read failed:", err2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; }


&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;fmt.Println("count:", n, "msg:", 
string(buffer))


&nbsp; &nbsp; }


}


func main() {
&nbsp; &nbsp; flag.Parse()
&nbsp; &nbsp; for i := 0; i < *ConcurNum; i++ {
&nbsp; &nbsp; &nbsp; &nbsp; go consume()
&nbsp; &nbsp; }
&nbsp; &nbsp; time.Sleep(3600 * time.Second)
}
```

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/tencent_BEE3D2499F3C9358D7A1CD7C88E0228C6D07%40qq.com.

Reply via email to