Skip to content

Instantly share code, notes, and snippets.

@acidsound
Last active October 17, 2019 04:50
Show Gist options
  • Save acidsound/de48bc1315e1dadda577ccdd07bf7392 to your computer and use it in GitHub Desktop.
Save acidsound/de48bc1315e1dadda577ccdd07bf7392 to your computer and use it in GitHub Desktop.
a ping example compatible with the redis-benchmark
package main
import (
"bufio"
"io"
"net"
)
type Server struct {
listener net.Listener
}
var pong = []byte("+PONG\r\n")
func (s *Server) open(conn net.Conn) {
defer conn.Close()
buf := bufio.NewReader(conn)
for {
_, err := buf.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
// other errors
break
}
conn.Write(pong)
}
}
func main() {
ln, err := net.Listen("tcp4", "0.0.0.0:4242")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go func() {
server := Server{ln}
server.open(conn)
}()
}
}
@acidsound
Copy link
Author

redis-benchmark -t PING -p 4242 -q

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