-
Notifications
You must be signed in to change notification settings - Fork 68
/
gocache.go
67 lines (54 loc) · 1.27 KB
/
gocache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"github.com/dustin/gomemcached"
"github.com/dustin/gomemcached/server"
)
var port *int = flag.Int("port", 11212, "Port on which to listen")
type chanReq struct {
req *gomemcached.MCRequest
res chan *gomemcached.MCResponse
}
type reqHandler struct {
ch chan chanReq
}
func (rh *reqHandler) HandleMessage(w io.Writer, req *gomemcached.MCRequest) *gomemcached.MCResponse {
cr := chanReq{
req,
make(chan *gomemcached.MCResponse),
}
rh.ch <- cr
return <-cr.res
}
func connectionHandler(s net.Conn, h memcached.RequestHandler) {
// Explicitly ignoring errors since they all result in the
// client getting hung up on and many are common.
_ = memcached.HandleIO(s, h)
}
func waitForConnections(ls net.Listener) {
reqChannel := make(chan chanReq)
go RunServer(reqChannel)
handler := &reqHandler{reqChannel}
log.Printf("Listening on port %d", *port)
for {
s, e := ls.Accept()
if e == nil {
log.Printf("Got a connection from %v", s.RemoteAddr())
go connectionHandler(s, handler)
} else {
log.Printf("Error accepting from %s", ls)
}
}
}
func main() {
flag.Parse()
ls, e := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if e != nil {
log.Fatalf("Got an error: %s", e)
}
waitForConnections(ls)
}