Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple echo server testing a few interesting Go language features, goroutines and channels.
// $ 6g echo.go && 6l -o echo echo.6
// $ ./echo
//
// ~ in another terminal ~
//
// $ nc localhost 3540
package main
import (
"net"
"bufio"
"strconv"
"fmt"
)
const PORT = 3540
func main() {
server, err := net.Listen("tcp", ":" + strconv.Itoa(PORT))
if server == nil {
panic("couldn't start listening: " + err.String())
}
conns := clientConns(server)
for {
go handleConn(<-conns)
}
}
func clientConns(listener net.Listener) chan net.Conn {
ch := make(chan net.Conn)
i := 0
go func() {
for {
client, err := listener.Accept()
if client == nil {
fmt.Printf("couldn't accept: " + err.String())
continue
}
i++
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
ch <- client
}
}()
return ch
}
func handleConn(client net.Conn) {
b := bufio.NewReader(client)
for {
line, err := b.ReadBytes('\n')
if err != nil { // EOF, or worse
break
}
client.Write(line)
}
}
@dumbtrev

This comment has been minimized.

Show comment
Hide comment
@dumbtrev

dumbtrev Apr 5, 2015

this is good stuff

dumbtrev commented Apr 5, 2015

this is good stuff

@Opa-

This comment has been minimized.

Show comment
Hide comment
@Opa-

Opa- Jun 8, 2015

Wow, thanks man !

Opa- commented Jun 8, 2015

Wow, thanks man !

@JoelSimonoff

This comment has been minimized.

Show comment
Hide comment
@JoelSimonoff

JoelSimonoff commented Jul 10, 2015

Cool Thanks

@nathalia252

This comment has been minimized.

Show comment
Hide comment
@nathalia252

nathalia252 Mar 30, 2016

thank you so much!

nathalia252 commented Mar 30, 2016

thank you so much!

@lonnietc

This comment has been minimized.

Show comment
Hide comment
@lonnietc

lonnietc Sep 23, 2016

Solved it... Thanks

lonnietc commented Sep 23, 2016

Solved it... Thanks

@barryz

This comment has been minimized.

Show comment
Hide comment
@barryz

barryz Oct 23, 2016

why use a unbuffered channel ?

barryz commented Oct 23, 2016

why use a unbuffered channel ?

@yerden

This comment has been minimized.

Show comment
Hide comment
@yerden

yerden Feb 19, 2017

one can just do simple copy if there's no need to check for new line:

import "io"
...
func handleConn(client net.Conn) {
   io.Copy(client, client);
}

yerden commented Feb 19, 2017

one can just do simple copy if there's no need to check for new line:

import "io"
...
func handleConn(client net.Conn) {
   io.Copy(client, client);
}
@tmjvonboss

This comment has been minimized.

Show comment
Hide comment
@tmjvonboss

tmjvonboss Sep 19, 2018

So at some point Golang's CPU limit for running Goroutines at the same time got raised to the numbers of cores present in your machine.
That would make this server only run two Goroutines at the same time in the best case scenario...
I have seen another example that has a Goroutine per connection, but that sounds like a RAM eater if the amount of connections scales up.
What would be best for the sake of efficiency, a Goroutine per connection or a channel and one Goroutine per part of the server or perhaps the example above but with more Goroutines handling connections?
Let's assume a 8 core machine.
I'm pretty new to Golang but perhaps a more experienced person could weigh in on my dilemma

tmjvonboss commented Sep 19, 2018

So at some point Golang's CPU limit for running Goroutines at the same time got raised to the numbers of cores present in your machine.
That would make this server only run two Goroutines at the same time in the best case scenario...
I have seen another example that has a Goroutine per connection, but that sounds like a RAM eater if the amount of connections scales up.
What would be best for the sake of efficiency, a Goroutine per connection or a channel and one Goroutine per part of the server or perhaps the example above but with more Goroutines handling connections?
Let's assume a 8 core machine.
I'm pretty new to Golang but perhaps a more experienced person could weigh in on my dilemma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment