-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (50 loc) · 1.06 KB
/
server.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
package memcached
import (
"bufio"
"log"
"net"
"github.com/bradberger/gocache/cache"
)
// Server defines parameters for running a memcache server.
type Server struct {
Logger Logger
Cache cache.Cache
Addr string
}
// ListenAndServe listens on the configured network address
// and then calls Serve to handle memcache requests
func (srv *Server) ListenAndServe() error {
ln, e := net.Listen("tcp", srv.Addr)
if e != nil {
return e
}
return srv.Serve(ln)
}
// Serve accepts incoming connections on the Listener l, creating a new service goroutine for each.
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
for {
rw, e := l.Accept()
if e != nil {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
log.Printf("memcached: error: %v", e)
continue
}
return e
}
sess, err := srv.newSession(rw)
if err != nil {
continue
}
go sess.serve()
}
}
func (srv *Server) newSession(rwc net.Conn) (s *session, err error) {
s = &session{
srv: srv,
rwc: rwc,
br: bufio.NewReader(rwc),
bw: bufio.NewWriter(rwc),
}
return
}