Skip to content

Instantly share code, notes, and snippets.

@glaslos
Created July 2, 2019 20:00
Show Gist options
  • Save glaslos/d55ee43131cb983bd65df8c02ed32a7b to your computer and use it in GitHub Desktop.
Save glaslos/d55ee43131cb983bd65df8c02ed32a7b to your computer and use it in GitHub Desktop.
package glutton
import (
"context"
"net"
"strings"
"go.uber.org/zap"
)
// HandleMemcache handles a Memcache connection
func (g *Glutton) HandleMemcache(ctx context.Context, conn net.Conn) error {
var err error
defer func() {
err = conn.Close()
if err != nil {
g.logger.Error("Error", zap.String("handler", "memcache"), zap.Error(err))
}
}()
buffer := make([]byte, 1024)
for {
g.updateConnectionTimeout(ctx, conn)
n, err := conn.Read(buffer)
if err == nil || n > 0 {
parts := strings.Split(string(buffer), "\r\n")
head := parts[0]
headParts := strings.Split(head, " ")
switch headParts[0] {
case "set":
case "get":
conn.Write([]byte("VALUE key flag 6\r\nbanana\r\nEND\r\n"))
}
} else {
break
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment