A problem with go socket server on linux

130 views
Skip to first unread message

eimu gray

unread,
Dec 5, 2013, 7:40:34 AM12/5/13
to golan...@googlegroups.com
HI guys:
 I'm a new to GO.I wish to make a a high concurrency socket server using Go, that's my code:

package main
import (
    "fmt"
    "os"
    "net"
    "time"
    "runtime"
)
var sem = make(chan int, 1000)
func init() {
    for i := 0; i < 1000; i++ {
        sem <- 1
    }
}

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())

    service := ":10000"
    tcpAddr,err := net.ResolveTCPAddr("tcp4", service)
    if err != nil {
        fmt.Println("error ResolveTcpAddr:", err.Error())
        os.Exit(-1)
    }

    listener, err := net.ListenTCP("tcp", tcpAddr)
    if err != nil {
        fmt.Println("error listenTcp:", err.Error())
        os.Exit(-1)
    }

    defer listener.Close()

    for {
        conn, err := listener.Accept()
        if err != nil {
            fmt.Println("Error accept:", err.Error())
            os.Exit(-1)
        }
        <- sem
        go HandleRequest(conn)
    }
}

func HandleRequest(conn net.Conn) {
    defer func(){
        sem<-1
    }()
    defer conn.Close()
    buf := make([]byte, 1024)
    for {
        conn.SetReadDeadline(time.Now().Add(10 * time.Second))
        read_len, err := conn.Read(buf)
        if err != nil {
            fmt.Println("error read:", err.Error())
            break
        }
        if read_len == 0 {
            fmt.Println("read 0 bytes:", err.Error())
            break
        }
        if _, err = conn.Write([]byte("ok")); err != nil {
            fmt.Println("error write:", err.Error())
            break
        }
        buf = make([]byte, 1024)
    }
}

for every request , the server makes a goroutine to handle it.
then I did  the Load Tetsing by LoadRunner.
the problem is:
1 start running the test script . the  LR's Trans/sec line always fell to bottom  rapidly  after  7 minutes(see below), and LR show errors that connection reset
2 "netstat -ntu|grep 10000|grep ESTABLISHED|wc -l" always show 30, and  after the errors  , it change  less
3 try to stace  related threads, they stay in the "FUTEX_WAIT" status.

is the problem clear? did I make something wrong in the code or what should do for the problem? Is there wonderful library for socket server in go?
the problem is troubling me.


  

Erik Troan

unread,
Dec 5, 2013, 7:55:43 AM12/5/13
to eimu gray, golan...@googlegroups.com
Are you running out of client ports? It sounds like it drops when you're waiting for the port numbers to timeout. Not a go issue, just the way tcp works. This isn't a real world problem because normally you're going to have using ip/port tuple for the clients (since presumably they aren't all coming from a single machine).

Erik


--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dave Cheney

unread,
Dec 5, 2013, 7:58:40 AM12/5/13
to eimu gray, golan...@googlegroups.com
Try moving <- sem here


eimu gray

unread,
Dec 5, 2013, 8:13:36 AM12/5/13
to golan...@googlegroups.com, eimu gray
I thougnt the situation that run out of client ports.
but I execute the command "netstat -ntu|grep 10000|grep ESTABLISHED|wc -l" in terminal on server and client machine, they always show 30 or less than 30(becuause the errors come up).
or I have a wrong understanding abount the knowledge max num of the port? 
It's so trouble!!

在 2013年12月5日星期四UTC+8下午8时55分43秒,Erik Troan写道:

eimu gray

unread,
Dec 5, 2013, 8:15:14 AM12/5/13
to golan...@googlegroups.com, eimu gray
I tried it, but the situation didn't change.

Matt Harden

unread,
Dec 5, 2013, 8:31:21 AM12/5/13
to eimu gray, golang-nuts
Check for sockets sitting in TIME_WAIT status. A large number of those means you are waiting for ephemeral ports to free up. As Erik mentioned, this is the way TCP works. If you can get LoadRunner to connect from several source IP addresses, you can get more concurrent connections. Also check into your settings for Ephemeral Ports, especially on older versions of Windows.
Message has been deleted

eimu gray

unread,
Dec 5, 2013, 8:42:41 AM12/5/13
to golan...@googlegroups.com, eimu gray
Thanks, I'll check them, and hope it can solve my problem so taht I can fall  sleep sweetly

在 2013年12月5日星期四UTC+8下午9时31分21秒,Matt Harden写道:
Reply all
Reply to author
Forward
0 new messages