Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"flag"
"net"
"os"
"time"
"github.com/singchia/go-hammer/circulinker"
)
func main() {
var addr string
flag.StringVar(&addr, "addr", ":1202", "listen addr")
flag.Parse()
l, err := net.Listen("tcp", addr)
if err != nil {
os.Exit(1)
}
defer l.Close()
connMap := make(map[net.Conn]circulinker.CircuID)
cl := circulinker.NewCirculinker()
for i := 0; i < 100; i++ {
go func() {
buf := make([]byte, 1024)
for {
time.Sleep(time.Microsecond * 50)
if len(connMap) == 0 {
continue
}
cl.Rightshift()
conn := cl.RetrieveCur().(net.Conn)
conn.SetReadDeadline(time.Now().Add(time.Microsecond * 50))
len, err := conn.Read(buf)
E, ok := err.(net.Error)
if ok && E.Timeout() {
continue
}
if err != nil {
conn.Close()
cl.Delete(connMap[conn])
delete(connMap, conn)
continue
}
conn.Write(append([]byte(addr+" "), buf[:len]...))
}
}()
}
for {
conn, err := l.Accept()
if err != nil {
os.Exit(1)
}
id := cl.Add(conn)
connMap[conn] = id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment