FAQ
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.

<https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>



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

Search Discussions

  • Erik Troan at Dec 5, 2013 at 12:55 pm
    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

    On Thu, Dec 5, 2013 at 7:40 AM, eimu gray wrote:

    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.


    <https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>



    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu gray at Dec 5, 2013 at 1:13 pm
    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写道:
    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


    On Thu, Dec 5, 2013 at 7:40 AM, eimu gray <eimu...@gmail.com <javascript:>
    wrote:
    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.


    <https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>



    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Harden at Dec 5, 2013 at 1:31 pm
    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<http://en.wikipedia.org/wiki/Ephemeral_port>,
    especially on older versions of Windows.

    On Thu, Dec 5, 2013 at 7:13 AM, eimu gray wrote:

    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写道:
    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

    On Thu, Dec 5, 2013 at 7:40 AM, eimu gray wrote:

    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.


    <https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>



    --
    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.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu gray at Dec 5, 2013 at 1:42 pm
    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写道:
    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<http://en.wikipedia.org/wiki/Ephemeral_port>,
    especially on older versions of Windows.


    On Thu, Dec 5, 2013 at 7:13 AM, eimu gray <eimu...@gmail.com <javascript:>
    wrote:
    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写道:
    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

    On Thu, Dec 5, 2013 at 7:40 AM, eimu gray wrote:

    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.


    <https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>



    --
    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.
    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at Dec 5, 2013 at 12:58 pm

    On 5 Dec 2013, at 23:40, eimu gray wrote:

    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 {
    Try moving <- sem here

    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.





    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu gray at Dec 5, 2013 at 1:15 pm
    I tried it, but the situation didn't change.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu gray at Dec 5, 2013 at 1:42 pm
    在 2013年12月5日星期四UTC+8下午8时40分34秒,eimu gray写道:
    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.


    <https://lh4.googleusercontent.com/-dmZ9Og9z35E/UqBz89UcjaI/AAAAAAAAAEg/TxQFWyCmKiI/s1600/image001.png>


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu Gray at Dec 5, 2013 at 4:19 pm
    Hi:
        I'm a new to GO,Recently I wish to make a high concurrency socket server
    using Go on my Linux server. Here's my source 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, I start a routine to handle it.

    the problem is:
    1 I use loadrunner to do the load testing, but always after seven minutes
    , the Trans/Sec line fall to bottom rapidly(see below).
    2 the LR error say the connect reset, but the 'netstat -ntu|grep
    10000|grep ESTABLISHED | wc -l' show 30 always
    3 I strace in htop, the threads are all FUTEX_WAIT.

    is it clear? did I do something wrong, or What I can do now? is there
    wonderful library(go) for socket server?
    the problem trouble me.

    <https://lh4.googleusercontent.com/-9eSUSRNoRo0/UqBSslCJzbI/AAAAAAAAAEQ/ilWZ4ykq8Kc/s1600/image001.png>



    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Dec 5, 2013 at 6:43 pm

    On Dec 5, 2013 11:19 AM, wrote:
    Hi:

    I'm a new to GO,Recently I wish to make a high concurrency socket
    server using Go on my Linux server. Here's my source 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
    }
    you should checj read_len before looking at err.
    if _, err = conn.Write([]byte("ok")); err != nil {
    fmt.Println("error write:", err.Error())
    break
    }
    buf = make([]byte, 1024)
    why not reuse the old buf? you're producing garbage very fast otherwise.
    }
    }

    for every request, I start a routine to handle it.

    the problem is:
    1 I use loadrunner to do the load testing, but always after seven
    minutes , the Trans/Sec line fall to bottom rapidly(see below).
    2 the LR error say the connect reset, but the 'netstat -ntu|grep
    10000|grep ESTABLISHED | wc -l' show 30 always
    3 I strace in htop, the threads are all FUTEX_WAIT.

    is it clear? did I do something wrong, or What I can do now? is there
    wonderful library(go) for socket server?
    the problem trouble me.
    you should take a look at various profiling options for Go, and use them to
    diagnose the problem.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eimu gray at Dec 5, 2013 at 7:00 pm
    thanks, I'll take it

    在 2013年12月6日星期五UTC+8上午2时43分15秒,minux写道:
    On Dec 5, 2013 11:19 AM, <eimu...@gmail.com <javascript:>> wrote:

    Hi:

    I'm a new to GO,Recently I wish to make a high concurrency socket
    server using Go on my Linux server. Here's my source 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
    }
    you should checj read_len before looking at err.
    if _, err = conn.Write([]byte("ok")); err != nil {
    fmt.Println("error write:", err.Error())
    break
    }
    buf = make([]byte, 1024)
    why not reuse the old buf? you're producing garbage very fast otherwise.
    }
    }

    for every request, I start a routine to handle it.

    the problem is:
    1 I use loadrunner to do the load testing, but always after seven
    minutes , the Trans/Sec line fall to bottom rapidly(see below).
    2 the LR error say the connect reset, but the 'netstat -ntu|grep
    10000|grep ESTABLISHED | wc -l' show 30 always
    3 I strace in htop, the threads are all FUTEX_WAIT.

    is it clear? did I do something wrong, or What I can do now? is there
    wonderful library(go) for socket server?
    the problem trouble me.
    you should take a look at various profiling options for Go, and use them
    to diagnose the problem.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 5, '13 at 12:40p
activeDec 5, '13 at 7:00p
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase