Skip to content

Instantly share code, notes, and snippets.

@vmihailenco
Created July 31, 2012 08:27
Show Gist options
  • Save vmihailenco/3214930 to your computer and use it in GitHub Desktop.
Save vmihailenco/3214930 to your computer and use it in GitHub Desktop.
redis_proxy.go
package main
import (
"net"
"runtime"
)
func proxy(conn net.Conn) {
buf := make([]byte, 1000)
redisConn, err := net.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
err = redisConn.(*net.TCPConn).SetKeepAlive(true)
if err != nil {
panic(err)
}
for {
n, err := conn.Read(buf)
if err != nil {
panic(err)
}
if n == 0 {
panic("n == 0")
}
n, err = redisConn.Write(buf[:n])
if err != nil {
panic(err)
}
if n == 0 {
panic("n == 0")
}
n, err = redisConn.Read(buf)
if err != nil {
panic(err)
}
if n == 0 {
panic("n == 0")
}
n, err = conn.Write(buf[:n])
if err != nil {
panic(err)
}
if n == 0 {
panic("n == 0")
}
}
conn.Close()
}
func main() {
runtime.GOMAXPROCS(1)
listener, err := net.Listen("tcp", "localhost:8888")
if err != nil {
panic(err)
}
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
go proxy(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment